diff --git a/piccolo/apps/migrations/commands/base.py b/piccolo/apps/migrations/commands/base.py index 5a4e9615d..6078feec0 100644 --- a/piccolo/apps/migrations/commands/base.py +++ b/piccolo/apps/migrations/commands/base.py @@ -31,7 +31,7 @@ async def create_migration_table(self) -> bool: return False def get_migration_modules( - self, folder_path: str + self, folder_path: Optional[str] = None ) -> dict[str, MigrationModule]: """ Imports the migration modules in the given folder path, and returns @@ -40,7 +40,8 @@ def get_migration_modules( """ migration_modules = {} - sys.path.insert(0, folder_path) + if folder_path: + sys.path.insert(0, folder_path) folder_contents = os.listdir(folder_path) excluded = ("__init__.py",) diff --git a/piccolo/apps/migrations/commands/new.py b/piccolo/apps/migrations/commands/new.py index 172de96ed..e7fb2d7ed 100644 --- a/piccolo/apps/migrations/commands/new.py +++ b/piccolo/apps/migrations/commands/new.py @@ -98,6 +98,9 @@ def _generate_migration_meta(app_config: AppConfig) -> NewMigrationMeta: filename = f"{cleaned_app_name}_{cleaned_id}" + if app_config.resolved_migrations_folder_path is None: + raise ValueError("Migrations disabled in this app!") + path = os.path.join( app_config.resolved_migrations_folder_path, f"{filename}.py" ) @@ -257,7 +260,10 @@ async def new( app_config = Finder().get_app_config(app_name=app_name) - _create_migrations_folder(app_config.resolved_migrations_folder_path) + if app_config.resolved_migrations_folder_path: + _create_migrations_folder( + app_config.resolved_migrations_folder_path + ) try: await _create_new_migration( diff --git a/piccolo/conf/apps.py b/piccolo/conf/apps.py index 983e31e04..45575953c 100644 --- a/piccolo/conf/apps.py +++ b/piccolo/conf/apps.py @@ -182,13 +182,13 @@ class AppConfig: """ app_name: str - migrations_folder_path: Union[str, pathlib.Path] + migrations_folder_path: Union[str, pathlib.Path, None] table_classes: list[type[Table]] = field(default_factory=list) migration_dependencies: list[str] = field(default_factory=list) commands: list[Union[Callable, Command]] = field(default_factory=list) @property - def resolved_migrations_folder_path(self) -> str: + def resolved_migrations_folder_path(self) -> Optional[str]: return ( str(self.migrations_folder_path) if isinstance(self.migrations_folder_path, pathlib.Path) diff --git a/tests/apps/migrations/auto/integration/test_migrations.py b/tests/apps/migrations/auto/integration/test_migrations.py index a065d9588..c02fbffeb 100644 --- a/tests/apps/migrations/auto/integration/test_migrations.py +++ b/tests/apps/migrations/auto/integration/test_migrations.py @@ -148,10 +148,11 @@ def _test_migrations( migrations_folder_path = app_config.resolved_migrations_folder_path - if os.path.exists(migrations_folder_path): - shutil.rmtree(migrations_folder_path) + if migrations_folder_path: + if os.path.exists(migrations_folder_path): + shutil.rmtree(migrations_folder_path) - _create_migrations_folder(migrations_folder_path) + _create_migrations_folder(migrations_folder_path) for table_snapshot in table_snapshots: app_config.table_classes = table_snapshot diff --git a/tests/apps/migrations/commands/test_new.py b/tests/apps/migrations/commands/test_new.py index da47877c1..f763d1a33 100644 --- a/tests/apps/migrations/commands/test_new.py +++ b/tests/apps/migrations/commands/test_new.py @@ -61,6 +61,23 @@ def test_auto(self, print_: MagicMock): ], ) + @engines_only("postgres", "cockroach") + def test_auto_migration_folder_is_none(self): + """ + Test disabling migrations per app + """ + app_config = AppConfig( + app_name="music", + migrations_folder_path=None, + table_classes=[Manager], + ) + with self.assertRaises(ValueError) as manager: + run_sync(_create_new_migration(app_config, auto=True)) + + self.assertEqual( + manager.exception.__str__(), "Migrations disabled in this app!" + ) + @engines_only("postgres") @patch("piccolo.apps.migrations.commands.new.print") def test_auto_all(self, print_: MagicMock): diff --git a/tests/conf/test_apps.py b/tests/conf/test_apps.py index 5054bcf90..90a93b37c 100644 --- a/tests/conf/test_apps.py +++ b/tests/conf/test_apps.py @@ -85,6 +85,13 @@ def test_get_table_with_name(self): class TestAppConfig(TestCase): + def test_migrations_folder_path_is_none(self): + """ + Make sure that ``migrations_folder_path`` argument can be ``None``. + """ + config = AppConfig(app_name="music", migrations_folder_path=None) + self.assertEqual(config.resolved_migrations_folder_path, None) + def test_pathlib(self): """ Make sure a ``pathlib.Path`` instance can be passed in as a