|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import time |
| 5 | +import typing as t |
| 6 | +from unittest import TestCase |
| 7 | + |
| 8 | +from piccolo.conf.apps import AppConfig |
| 9 | +from piccolo.columns.column_types import Varchar |
| 10 | +from piccolo.apps.migrations.commands.new import ( |
| 11 | + _create_new_migration, |
| 12 | + _create_migrations_folder, |
| 13 | +) |
| 14 | +from piccolo.apps.migrations.commands.forwards import ForwardsMigrationManager |
| 15 | +from piccolo.table import Table, create_table_class |
| 16 | +from piccolo.utils.sync import run_sync |
| 17 | + |
| 18 | + |
| 19 | +class TestMigrations(TestCase): |
| 20 | + def tearDown(self): |
| 21 | + create_table_class("MyTable").alter().drop_table(if_exists=True) |
| 22 | + |
| 23 | + def run_migrations(self, app_config: AppConfig): |
| 24 | + manager = ForwardsMigrationManager(app_name=app_config.app_name) |
| 25 | + run_sync(manager.create_migration_table()) |
| 26 | + run_sync(manager.run_migrations(app_config=app_config)) |
| 27 | + |
| 28 | + def _test_migrations(self, table_1: t.Type[Table], table_2: t.Type[Table]): |
| 29 | + temp_directory_path = tempfile.gettempdir() |
| 30 | + migrations_folder_path = os.path.join( |
| 31 | + temp_directory_path, "piccolo_migrations" |
| 32 | + ) |
| 33 | + |
| 34 | + if os.path.exists(migrations_folder_path): |
| 35 | + shutil.rmtree(migrations_folder_path) |
| 36 | + |
| 37 | + _create_migrations_folder(migrations_folder_path) |
| 38 | + |
| 39 | + app_config = AppConfig( |
| 40 | + app_name="test_app", |
| 41 | + migrations_folder_path=migrations_folder_path, |
| 42 | + table_classes=[table_1], |
| 43 | + ) |
| 44 | + |
| 45 | + meta = run_sync( |
| 46 | + _create_new_migration(app_config=app_config, auto=True) |
| 47 | + ) |
| 48 | + self.assertTrue(os.path.exists(meta.migration_path)) |
| 49 | + self.run_migrations(app_config=app_config) |
| 50 | + |
| 51 | + ####################################################################### |
| 52 | + |
| 53 | + # It's kind of absurd sleeping for 1 microsecond, but it guarantees |
| 54 | + # the migration IDs will be unique, and just in case computers and / or |
| 55 | + # Python get insanely fast in the future :) |
| 56 | + time.sleep(1e-6) |
| 57 | + |
| 58 | + ####################################################################### |
| 59 | + |
| 60 | + app_config.table_classes = [table_2] |
| 61 | + meta = run_sync( |
| 62 | + _create_new_migration(app_config=app_config, auto=True) |
| 63 | + ) |
| 64 | + self.assertTrue(os.path.exists(meta.migration_path)) |
| 65 | + self.run_migrations(app_config=app_config) |
| 66 | + |
| 67 | + # TODO - check the migrations ran correctly |
| 68 | + |
| 69 | + def test_add_column(self): |
| 70 | + table_1 = create_table_class("MyTable") |
| 71 | + table_2 = create_table_class( |
| 72 | + "MyTable", class_members={"name": Varchar()} |
| 73 | + ) |
| 74 | + |
| 75 | + self._test_migrations(table_1=table_1, table_2=table_2) |
0 commit comments