Пример #1
0
async def run_backwards(
    app_name: str,
    migration_id: str = "1",
    auto_agree: bool = False,
    clean: bool = False,
) -> MigrationResult:
    if app_name == "all":
        sorted_app_names = BaseMigrationManager().get_sorted_app_names()
        sorted_app_names.reverse()

        _continue = ("y" if auto_agree else input(
            "You're about to undo the migrations for the following apps:\n"
            f"{sorted_app_names}\n"
            "Are you sure you want to continue?\n"
            "Enter y to continue.\n"))
        if _continue == "y":
            for _app_name in sorted_app_names:
                print(f"Undoing {_app_name}")
                manager = BackwardsMigrationManager(
                    app_name=_app_name,
                    migration_id="all",
                    auto_agree=auto_agree,
                )
                await manager.run()
            return MigrationResult(success=True)
        else:
            return MigrationResult(success=False, message="User cancelled")
    else:
        manager = BackwardsMigrationManager(
            app_name=app_name,
            migration_id=migration_id,
            auto_agree=auto_agree,
            clean=clean,
        )
        return await manager.run()
Пример #2
0
async def backwards(
    app_name: str,
    migration_id: str = "1",
    auto_agree: bool = False,
    clean: bool = False,
):
    """
    Undo migrations up to a specific migration.

    :param app_name:
        The app to reverse migrations for. Specify a value of 'all' to reverse
        migrations for all apps.
    :param migration_id:
        Migrations will be reversed up to and including this migration_id.
        Specify a value of 'all' to undo all of the migrations. Specify a
        value of '1' to undo the most recent migration.
    :param auto_agree:
        If true, automatically agree to any input prompts.
    :param clean:
        If true, the migration files which have been run backwards are deleted
        from the disk after completing.

    """
    if app_name == "all":
        sorted_app_names = BaseMigrationManager().get_sorted_app_names()
        sorted_app_names.reverse()

        _continue = (
            "y"
            if auto_agree
            else input(
                "You're about to undo the migrations for the following apps:\n"
                f"{sorted_app_names}\n"
                "Are you sure you want to continue?\n"
                "Enter y to continue.\n"
            )
        )
        if _continue == "y":
            for _app_name in sorted_app_names:
                print(f"Undoing {_app_name}")
                manager = BackwardsMigrationManager(
                    app_name=_app_name,
                    migration_id="all",
                    auto_agree=auto_agree,
                )
                await manager.run()
    else:
        manager = BackwardsMigrationManager(
            app_name=app_name,
            migration_id=migration_id,
            auto_agree=auto_agree,
            clean=clean,
        )
        await manager.run()
Пример #3
0
async def forwards(app_name: str,
                   migration_id: str = "all",
                   fake: bool = False):
    """
    Runs any migrations which haven't been run yet.

    :param app_name:
        The name of the app to migrate. Specify a value of 'all' to run
        migrations for all apps.
    :param migration_id:
        Migrations will be ran up to and including this migration_id.
        Specify a value of 'all' to run all of the migrations. Specify a
        value of '1' to just run the next migration.
    :param fake:
        If set, will record the migrations as being run without actually
        running them.
    """
    if app_name == "all":
        sorted_app_names = BaseMigrationManager().get_sorted_app_names()
        for _app_name in sorted_app_names:
            print(f"\nMigrating {_app_name}")
            print("------------------------------------------------")
            manager = ForwardsMigrationManager(app_name=_app_name,
                                               migration_id="all",
                                               fake=fake)
            await manager.run()
    else:
        manager = ForwardsMigrationManager(app_name=app_name,
                                           migration_id=migration_id,
                                           fake=fake)
        await manager.run()
Пример #4
0
async def run_forwards(app_name: str,
                       migration_id: str = "all",
                       fake: bool = False) -> MigrationResult:
    """
    Run the migrations. This function can be used to programatically run
    migrations - for example, in a unit test.
    """
    if app_name == "all":
        sorted_app_names = BaseMigrationManager().get_sorted_app_names()
        for _app_name in sorted_app_names:
            print(f"\nMigrating {_app_name}")
            print("------------------------------------------------")
            manager = ForwardsMigrationManager(app_name=_app_name,
                                               migration_id="all",
                                               fake=fake)
            response = await manager.run()
            if not response.success:
                return response

        return MigrationResult(success=True)

    else:
        manager = ForwardsMigrationManager(app_name=app_name,
                                           migration_id=migration_id,
                                           fake=fake)
        return await manager.run()
Пример #5
0
async def run_backwards(
    app_name: str,
    migration_id: str = "1",
    auto_agree: bool = False,
    clean: bool = False,
) -> MigrationResult:
    if app_name == "all":
        sorted_app_names = BaseMigrationManager().get_sorted_app_names()
        sorted_app_names.reverse()

        names = [f"'{name}'" for name in sorted_app_names]
        _continue = (
            "y"
            if auto_agree
            else input(
                "You are about to undo the migrations for the following "
                "apps:\n"
                f"{', '.join(names)}\n"
                "Are you sure you want to continue? [y/N] "
            )
        )

        if _continue in "yY":
            for _app_name in sorted_app_names:
                print(f"\n{_app_name.upper():^64}")
                print("-" * 64)
                manager = BackwardsMigrationManager(
                    app_name=_app_name,
                    migration_id="all",
                    auto_agree=auto_agree,
                )
                await manager.run()
            return MigrationResult(success=True)
        else:
            return MigrationResult(success=False, message="user cancelled")
    else:
        manager = BackwardsMigrationManager(
            app_name=app_name,
            migration_id=migration_id,
            auto_agree=auto_agree,
            clean=clean,
        )
        return await manager.run()
Пример #6
0
    def test_get_table_from_snaphot(self, get_app_config):
        """
        Test the get_table_from_snaphot method.
        """
        get_app_config.return_value = AppConfig(
            app_name="music",
            migrations_folder_path=os.path.join(os.path.dirname(__file__),
                                                "test_migrations"),
        )

        table = run_sync(BaseMigrationManager().get_table_from_snaphot(
            app_name="music", table_class_name="Band"))
        self.assertTrue(table.class_name == "Band")
Пример #7
0
    def test_get_migration_modules(self, get_app_config):
        get_app_config.return_value = AppConfig(
            app_name="music",
            migrations_folder_path=os.path.join(os.path.dirname(__file__),
                                                "test_migrations"),
        )

        migration_managers = run_sync(
            BaseMigrationManager().get_migration_managers(app_name="music"))

        self.assertTrue(len(migration_managers) == 1)
        self.assertTrue(
            migration_managers[0].migration_id == "2020-03-31T20:38:22")
Пример #8
0
 def test_create_migration_table(self):
     self.assertTrue(
         run_sync(BaseMigrationManager().create_migration_table()))