Esempio n. 1
0
 def test_create(self):
     self.assertFalse(
         FileSystemHelper.get_migrations_list(
             self.python_path_to_test_package))
     create(self.create_args)
     migrations_list = FileSystemHelper.get_migrations_list(
         self.python_path_to_test_package)
     self.assertTrue(migrations_list)
Esempio n. 2
0
 def __init__(self, py_package, py_module_name=None):
     self.py_package = py_package
     self.fs_migration_directory = FileSystemHelper.get_package_migrations_directory(
         py_package)
     self.py_module_name = py_module_name.rstrip('.py')
     self.fs_file_name = '%s.py' % py_module_name
     self.py_module, self.py_module_name = FileSystemHelper.get_migration_python_path_and_name(
         py_module_name, py_package)
     self.module = import_module(self.py_module)
Esempio n. 3
0
    def test_simple_squash(self):
        for name in ('test1', 'test2', 'test3'):
            self.api.create(self.python_path_to_test_package, name)

        migrations = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
        self.assertEqual(len(migrations), 3)
        self.api.squash(self.python_path_to_test_package, begin_from=1)
        migrations = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
        self.assertEqual(len(migrations), 1)
        self.assertTrue(migrations.get(1) is not None)
Esempio n. 4
0
    def squash(self, package, begin_from=1, name=None):
        """
        Squashes several migrations into one. Command reads all not applied migrations
        in package migration directory and appends content of forward and backward
        function into result functions. Squash also renames squashed migration with
        'squashed_' prefix.
        :param package: path to package which migrations should be squashed
        :param begin_from: migration number to begin squash from. Should be not less than 1
        :param name: squashed migration name
        """
        result_forward_content = ''
        result_backward_content = ''

        self._create_migration_history_table_if_not_exists()

        current_migration_number = DatabaseHelper.get_latest_migration_number(package)
        last_file_system_migration_number = FileSystemHelper.get_file_system_latest_migration_number(package)

        if begin_from:
            begin_from = int(begin_from)

        if begin_from <= current_migration_number or current_migration_number > last_file_system_migration_number:
            raise InconsistentParamsException(
                'Can squash only migrations which are not applied. Current applied migration number is %s'
                % current_migration_number
            )

        if begin_from < 1:
            raise InconsistentParamsException(
                'begin_from should not be less than 1'
            )

        migration_data = FileSystemHelper.get_migrations_list(package)
        ordered_keys = sorted(migration_data.keys())

        for key in ordered_keys[begin_from:]:
            file_name = migration_data[key]['file_name']
            file_path = migration_data[key]['file_path']
            stdout.write('Squashing migration %s...' % file_name)
            file_forward_content, file_backward_content = FileSystemHelper.get_migration_file_content(file_path)
            result_forward_content += file_forward_content
            result_backward_content += file_backward_content
            new_file_name = 'squashed_%s' % file_name
            new_file_path = path.join(migration_data[key]['file_directory'], new_file_name)
            rename(migration_data[key]['file_path'], new_file_path)

        first_migration_number = ordered_keys[0]

        Migration.create_squashed(
            py_package=package,
            name=name,
            migration_number=first_migration_number,
            forward_content=result_forward_content,
            backward_content=result_backward_content
        )
Esempio n. 5
0
    def squash(self, package, begin_from=1, name=None):
        """
        Squashes several migrations into one. Command reads all not applied migrations
        in package migration directory and appends content of forward and backward
        function into result functions. Squash also renames squashed migration with
        'squashed_' prefix.
        :param package: path to package which migrations should be squashed
        :param begin_from: migration number to begin squash from. Should be not less than 1
        :param name: squashed migration name
        """
        result_forward_content = ''
        result_backward_content = ''

        self._create_migration_history_table_if_not_exists()

        current_migration_number = DatabaseHelper.get_latest_migration_number(
            package)
        last_file_system_migration_number = FileSystemHelper.get_file_system_latest_migration_number(
            package)

        if begin_from:
            begin_from = int(begin_from)

        if begin_from <= current_migration_number or current_migration_number > last_file_system_migration_number:
            raise InconsistentParamsException(
                'Can squash only migrations which are not applied. Current applied migration number is %s'
                % current_migration_number)

        if begin_from < 1:
            raise InconsistentParamsException(
                'begin_from should not be less than 1')

        migration_data = FileSystemHelper.get_migrations_list(package)
        ordered_keys = sorted(migration_data.keys())

        for key in ordered_keys[begin_from:]:
            file_name = migration_data[key]['file_name']
            file_path = migration_data[key]['file_path']
            stdout.write('Squashing migration %s...' % file_name)
            file_forward_content, file_backward_content = FileSystemHelper.get_migration_file_content(
                file_path)
            result_forward_content += file_forward_content
            result_backward_content += file_backward_content
            new_file_name = 'squashed_%s' % file_name
            new_file_path = path.join(migration_data[key]['file_directory'],
                                      new_file_name)
            rename(migration_data[key]['file_path'], new_file_path)

        first_migration_number = ordered_keys[0]

        Migration.create_squashed(py_package=package,
                                  name=name,
                                  migration_number=first_migration_number,
                                  forward_content=result_forward_content,
                                  backward_content=result_backward_content)
Esempio n. 6
0
    def test_simple_squash(self):
        for name in ('test1', 'test2', 'test3'):
            self.api.create(self.python_path_to_test_package, name)

        migrations = FileSystemHelper.get_migrations_list(
            self.python_path_to_test_package)
        self.assertEqual(len(migrations), 3)
        self.api.squash(self.python_path_to_test_package, begin_from=1)
        migrations = FileSystemHelper.get_migrations_list(
            self.python_path_to_test_package)
        self.assertEqual(len(migrations), 1)
        self.assertTrue(migrations.get(1) is not None)
Esempio n. 7
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'))
Esempio n. 8
0
 def test_directory_created(self):
     path = FileSystemHelper.get_package_migrations_directory(
         self.python_path_to_test_package
     )
     self.assertTrue(exists(self.file_system_test_migrations_path))
     self.assertTrue(exists(self.file_system_path_to_init_py_in_migrations_directory))
     self.assertEqual(path, self.file_system_test_migrations_path)
Esempio n. 9
0
 def test_directory_created(self):
     path = FileSystemHelper.get_package_migrations_directory(
         self.python_path_to_test_package)
     self.assertTrue(exists(self.file_system_test_migrations_path))
     self.assertTrue(
         exists(self.file_system_path_to_init_py_in_migrations_directory))
     self.assertEqual(path, self.file_system_test_migrations_path)
Esempio n. 10
0
 def test_correct_result(self):
     path, name = FileSystemHelper.get_migration_python_path_and_name(
         self.migration_name, self.python_path_to_test_package)
     self.assertEqual(name, self.migration_name.replace('.py', ''))
     self.assertEqual(
         path, '.'.join(
             (self.python_path_to_test_package, 'migrations', name)))
Esempio n. 11
0
 def test_created_next(self):
     self.api.create(self.python_path_to_test_package,
                     'test_migration_name')
     self.api.create(self.python_path_to_test_package,
                     'test_migration_name')
     migration_list = FileSystemHelper.get_migrations_list(
         self.python_path_to_test_package)
     self.assertEqual(len(migration_list), 2)
Esempio n. 12
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)
Esempio n. 13
0
 def test_create_migration(self):
     self.api.create(self.python_path_to_test_package,
                     'test_migration_name')
     self.assertTrue(exists(self.file_system_test_migrations_path))
     self.assertTrue(
         exists(self.file_system_path_to_init_py_in_migrations_directory))
     migration_list = FileSystemHelper.get_migrations_list(
         self.python_path_to_test_package)
     self.assertEqual(len(migration_list), 1)
Esempio n. 14
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))
Esempio n. 15
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'))
Esempio n. 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)
Esempio n. 17
0
 def test_create(self):
     self.assertFalse(FileSystemHelper.get_migrations_list(self.python_path_to_test_package))
     create(self.create_args)
     migrations_list = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
     self.assertTrue(migrations_list)
Esempio n. 18
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)
Esempio n. 19
0
 def test_created_next(self):
     self.api.create(self.python_path_to_test_package, 'test_migration_name')
     self.api.create(self.python_path_to_test_package, 'test_migration_name')
     migration_list = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
     self.assertEqual(len(migration_list), 2)
Esempio n. 20
0
 def setUp(self):
     FileSystemHelper.get_package_migrations_directory(
         self.python_path_to_test_package)
Esempio n. 21
0
 def test_correct_result(self):
     path, name = FileSystemHelper.get_migration_python_path_and_name(
         self.migration_name, self.python_path_to_test_package
     )
     self.assertEqual(name, self.migration_name.replace('.py', ''))
     self.assertEqual(path, '.'.join((self.python_path_to_test_package, 'migrations', name)))
Esempio n. 22
0
 def test_create_migration(self):
     self.api.create(self.python_path_to_test_package, 'test_migration_name')
     self.assertTrue(exists(self.file_system_test_migrations_path))
     self.assertTrue(exists(self.file_system_path_to_init_py_in_migrations_directory))
     migration_list = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
     self.assertEqual(len(migration_list), 1)
Esempio n. 23
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))
Esempio n. 24
0
 def setUp(self):
     path = FileSystemHelper.get_package_migrations_directory(self.python_path_to_test_package)
     MigrationHelper.create_migration_file(path, self.migration_name)
Esempio n. 25
0
 def test_all_files_found(self):
     result = FileSystemHelper.get_migrations_list(self.python_path_to_test_package)
     self.assertTrue(len(result), len(self.migration_file_names))
Esempio n. 26
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)
Esempio n. 27
0
 def setUp(self):
     FileSystemHelper.get_package_migrations_directory(self.python_path_to_test_package)
Esempio n. 28
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)
Esempio n. 29
0
 def test_all_files_found(self):
     result = FileSystemHelper.get_migrations_list(
         self.python_path_to_test_package)
     self.assertTrue(len(result), len(self.migration_file_names))