Пример #1
0
 def test_migration_file_is_created(self):
     MigrationHelper.create_migration_file(self.file_system_test_migrations_path, self.migration_file_name)
     path_to_file = join(self.file_system_test_migrations_path, self.migration_file_name)
     self.assertTrue(exists(path_to_file))
     with open(path_to_file, 'r') as file_descriptor:
         content = file_descriptor.read()
     self.assertEqual(MigrationHelper.get_empty_migration_file_content(), content)
Пример #2
0
    def migrate(self, package=None, migration_number=None):
        """
        Migrates given package or config packages. Usage:
            migrate(package='package_a') - forwards to latest available migration
            migrate(package='package_a', migration_number=42) - if migration number is greater than current
        applied migration migrates forward to 42 migration, else backward.
            migrate() - migrates all packages found in config 'packages' section to latest available migrations
        :param package: package to search migrations in, if not provided tries to get all packages from
        'packages' config section. If found applies migration to all of them.
        :param migration_number: number of migration to apply. If number is behind of current migration
        system migrates back to given number, else migrates forward. If none is provided migrates to
        latest available.
        :return: None
        :raises InconsistentParamsException: raises when:
        1. package or 'packages' section are not provided
        2. package is not provided but migration_number is given
        3. given migration_number in package is given for migrate is equal to current applied
        4. incorrect migration number is given
        :raises NoMigrationsFoundToApply: raises when in the given package there are no migration to apply
        :raises IncorrectMigrationFile: raises when migration file has no forward or backward function
        """

        packages, migration_number = self._prepare_migration_data(package, migration_number)

        self._create_migration_history_table_if_not_exists()

        migration_direction = None
        for package_for_migrate in packages:
            current_migration_number = DatabaseHelper.get_latest_migration_number(package_for_migrate)
            if not migration_direction:
                migration_direction = MigrationHelper.get_migration_direction(
                    package_for_migrate, current_migration_number, migration_number
                )
                if migration_direction is None:
                    raise InconsistentParamsException('Current migration number matches given one')

            migration_data = FileSystemHelper.get_migrations_list(package_for_migrate)
            numbers_to_apply = MigrationHelper.get_migrations_numbers_to_apply(
                migration_data.keys(),
                current_migration_number,
                migration_number,
                migration_direction
            )

            if not numbers_to_apply:
                if package is not None:
                    raise NoMigrationsFoundToApply('No new migrations found in package %s' % package_for_migrate)
                else:
                    stdout.write('No new migrations found in package %s. Skipping.\n' % package_for_migrate)
                    continue

            for migration_number_to_apply in numbers_to_apply:
                file_name = migration_data[migration_number_to_apply]['file_name']
                migration = Migration(
                    py_package=package_for_migrate,
                    py_module_name=file_name
                )
                migration.migrate(migration_direction)
Пример #3
0
 def test_migration_file_is_created(self):
     MigrationHelper.create_migration_file(
         self.file_system_test_migrations_path, self.migration_file_name)
     path_to_file = join(self.file_system_test_migrations_path,
                         self.migration_file_name)
     self.assertTrue(exists(path_to_file))
     with open(path_to_file, 'r') as file_descriptor:
         content = file_descriptor.read()
     self.assertEqual(MigrationHelper.get_empty_migration_file_content(),
                      content)
Пример #4
0
 def test_bad_files_are_ignored(self):
     bad_file_names = (
         'abc.py',
         '0001_test.txt',
     )
     for file_name in bad_file_names:
         MigrationHelper.create_migration_file(
             self.file_system_test_migrations_path, file_name)
     result = FileSystemHelper.get_migrations_list(
         self.python_path_to_test_package)
     self.assertEqual(len(result), len(self.migration_file_names))
Пример #5
0
 def create(py_package, name):
     """
     Creates new migration and binds current instance to result module
     :param name: new migration name given by user. Example: initial
     :return:
     """
     current_migration_number = FileSystemHelper.get_file_system_latest_migration_number(
         py_package)
     fs_migration_directory = FileSystemHelper.get_package_migrations_directory(
         py_package)
     fs_file_name = MigrationHelper.generate_migration_name(
         name, current_migration_number + 1)
     MigrationHelper.create_migration_file(fs_migration_directory,
                                           fs_file_name)
     return Migration(py_package, fs_file_name.rstrip('.py'))
Пример #6
0
 def test_empty_migration_file_content(self):
     self.assertEqual(
         MigrationHelper.get_empty_migration_file_content(),
         MigrationHelper.MIGRATION_TEMPLATE % (
             MigrationHelper.PASS_LINE,
             MigrationHelper.PASS_LINE,
         ))
Пример #7
0
 def create_squashed(py_package, name, migration_number, forward_content,
                     backward_content):
     """
     Creates squashed migration and binds current instance to result module
     :param name: new migration name given by user. Example: initial
     :param migration_number: first squashed migration number
     :param forward_content: forward content of all squashed migrations
     :param backward_content: backward content of all squashed migrations
     :return:
     """
     if name is None:
         name = '%04d_squashed.py' % migration_number
     else:
         name = MigrationHelper.generate_migration_name(
             name, migration_number)
     fs_migration_directory = FileSystemHelper.get_package_migrations_directory(
         py_package)
     fs_file_path = path.join(fs_migration_directory, name)
     with open(fs_file_path, 'w') as file_descriptor:
         file_descriptor.write(MigrationHelper.MIGRATION_TEMPLATE % (
             forward_content,
             backward_content,
         ))
     return Migration(py_package, name.rstrip('.py'))
Пример #8
0
    def migrate(self, package=None, migration_number=None):
        """
        Migrates given package or config packages. Usage:
            migrate(package='package_a') - forwards to latest available migration
            migrate(package='package_a', migration_number=42) - if migration number is greater than current
        applied migration migrates forward to 42 migration, else backward.
            migrate() - migrates all packages found in config 'packages' section to latest available migrations
        :param package: package to search migrations in, if not provided tries to get all packages from
        'packages' config section. If found applies migration to all of them.
        :param migration_number: number of migration to apply. If number is behind of current migration
        system migrates back to given number, else migrates forward. If none is provided migrates to
        latest available.
        :return: None
        :raises InconsistentParamsException: raises when:
        1. package or 'packages' section are not provided
        2. package is not provided but migration_number is given
        3. given migration_number in package is given for migrate is equal to current applied
        4. incorrect migration number is given
        :raises NoMigrationsFoundToApply: raises when in the given package there are no migration to apply
        :raises IncorrectMigrationFile: raises when migration file has no forward or backward function
        """

        packages, migration_number = self._prepare_migration_data(
            package, migration_number)

        self._create_migration_history_table_if_not_exists()

        migration_direction = None
        for package_for_migrate in packages:
            current_migration_number = DatabaseHelper.get_latest_migration_number(
                package_for_migrate)
            if not migration_direction:
                migration_direction = MigrationHelper.get_migration_direction(
                    package_for_migrate, current_migration_number,
                    migration_number)
                if migration_direction is None:
                    raise InconsistentParamsException(
                        'Current migration number matches given one')

            migration_data = FileSystemHelper.get_migrations_list(
                package_for_migrate)
            numbers_to_apply = MigrationHelper.get_migrations_numbers_to_apply(
                migration_data.keys(), current_migration_number,
                migration_number, migration_direction)

            if not numbers_to_apply:
                if package is not None:
                    raise NoMigrationsFoundToApply(
                        'No new migrations found in package %s' %
                        package_for_migrate)
                else:
                    stdout.write(
                        'No new migrations found in package %s. Skipping.\n' %
                        package_for_migrate)
                    continue

            for migration_number_to_apply in numbers_to_apply:
                file_name = migration_data[migration_number_to_apply][
                    'file_name']
                migration = Migration(py_package=package_for_migrate,
                                      py_module_name=file_name)
                migration.migrate(migration_direction)
Пример #9
0
 def setUp(self):
     path = FileSystemHelper.get_package_migrations_directory(self.python_path_to_test_package)
     MigrationHelper.create_migration_file(path, self.migration_name)
Пример #10
0
 def test_bad_files_are_ignored(self):
     bad_file_names = ('abc.py', '0001_test.txt', )
     for file_name in bad_file_names:
         MigrationHelper.create_migration_file(self.file_system_test_migrations_path, file_name)
     result = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
     self.assertEqual(len(result), len(self.migration_file_names))
Пример #11
0
 def setUp(self):
     self.migrations_path = FileSystemHelper.get_package_migrations_directory(self.python_path_to_test_package)
     for name in self.migration_file_names:
         MigrationHelper.create_migration_file(self.migrations_path, name)
Пример #12
0
 def test_empty_migration_file_content(self):
     self.assertEqual(
         MigrationHelper.get_empty_migration_file_content(),
         MigrationHelper.MIGRATION_TEMPLATE % (MigrationHelper.PASS_LINE, MigrationHelper.PASS_LINE, ))
Пример #13
0
 def test_positive_case(self):
     name = 'abc'
     self.assertEqual(
         MigrationHelper.generate_migration_name(name),
         '%s_%s.py' % (MigrationHelper.MIGRATION_NAME_TEMPLATE % 1, name)
     )
Пример #14
0
 def setUp(self):
     self.migrations_path = FileSystemHelper.get_package_migrations_directory(
         self.python_path_to_test_package)
     for name in self.migration_file_names:
         MigrationHelper.create_migration_file(self.migrations_path, name)
Пример #15
0
 def test_positive_case(self):
     name = 'abc'
     self.assertEqual(
         MigrationHelper.generate_migration_name(name),
         '%s_%s.py' % (MigrationHelper.MIGRATION_NAME_TEMPLATE % 1, name))
Пример #16
0
 def setUp(self):
     path = FileSystemHelper.get_package_migrations_directory(
         self.python_path_to_test_package)
     MigrationHelper.create_migration_file(path, self.migration_name)