Exemple #1
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)
Exemple #2
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
        )
 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'))