Ejemplo n.º 1
0
    def test_content(self):
        sample_string = "it is a migration step!"
        version = StepVersion('0.0.1', content=sample_string)
        self.assertEqual(version.content, sample_string)

        another_sample_string = "it is another migration step!"
        version.content = another_sample_string
        self.assertEqual(version.content, another_sample_string)
Ejemplo n.º 2
0
    def test_list_versions(self):
        assert_versions = [
            StepVersion('4.0.1'),
            StepVersion('4.0.1.2'),
            StepVersion('4.5.0'),
            StepVersion('4.10.0'),
        ]
        versions = self.repo.list_versions()

        self.assertEqual(versions, assert_versions)
Ejemplo n.º 3
0
 def get_current_version(self):
     self.begin()
     version_string = self.run_sql_statement(
         'SELECT version FROM %s where application = %s ORDER BY timestamp LIMIT 1;'
         % (self.history_table, self.arg_str), (self.migration.application))
     self.end()
     return StepVersion(version_string)
Ejemplo n.º 4
0
    def test_run_migration_downgrade(self):
        repo = FileRepository('repository', 'app2')
        self.sqlupdater.migration = repo.get_migration(
            version_from=StepVersion('4.10.0'),
            version_to=StepVersion('4.0.1'))

        self.sqlupdater.run_migration()

        self.assertEqual(self.sqlupdater.output_sql, (
            "BEGIN;\n"
            "DROP INDEX test_first_idx;\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.10.0');\n"
            "DROP TABLE test_second;\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.5.0');\n"
            "DROP TABLE test_first;\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.0.1.2');\n"
            "COMMIT;\n"))
Ejemplo n.º 5
0
    def test_run_migration_upgrade(self):
        repo = FileRepository('repository', 'app2')
        self.sqlupdater.migration = repo.get_migration(
            version_from=StepVersion('4.0.1'),
            version_to=StepVersion('4.10.0'))

        self.sqlupdater.run_migration()

        self.assertEqual(
            self.sqlupdater.output_sql, "BEGIN;\n"
            "CREATE TABLE test_first (a INTEGER, b VARCHAR);\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.0.1.2');\n"
            "CREATE TABLE test_second (a INTEGER, b VARCHAR);\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.5.0');\n"
            "CREATE INDEX test_first_idx ON test_first (b);\n"
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.10.0');\n"
            "COMMIT;\n")
Ejemplo n.º 6
0
    def test_set_version(self):
        self.sqlupdater.migration = Migration('app2')

        self.sqlupdater.set_version(StepVersion('4.0.1'))

        self.assertEqual(
            self.sqlupdater.output_sql,
            "INSERT INTO public.dbupgrade_history (application,version) VALUES ('app2','4.0.1');\n"
        )
Ejemplo n.º 7
0
    def test_exception(self):
        migration = Migration('myApp')
        version_one = StepVersion('4.0.1', 'one')

        with self.assertRaises(MigrationException):
            migration.append_step('toto')
        with self.assertRaises(MigrationException):
            migration.append_step(version_one)
            migration.append_step(version_one)
Ejemplo n.º 8
0
    def path_to_version(self, version_from=None, version_to=None):

        # check if version_from exists
        if version_from:
            found = [item for item in self.versions if version_from == item]
            if len(found) == 0:
                raise RepositoryException(
                    'Version %s does not exists in repository' %
                    str(version_from))
        else:
            version_from = StepVersion('0')

        # check if version_to exists
        if version_to:
            found = [item for item in self.versions if version_to == item]
            if len(found) == 0:
                raise RepositoryException(
                    'Version %s does not exists in repository' %
                    str(version_from))
        else:
            version_to = StepVersion('Infinite')

        # it is a downgrade!
        downgrade = False
        if version_from > version_to:
            tmp = version_from
            version_from = version_to
            version_to = tmp

            downgrade = True

        versions_path = []

        for step in self.versions:

            if version_from < step <= version_to:
                versions_path.append(step)

        if downgrade:
            versions_path.reverse()

        return versions_path
Ejemplo n.º 9
0
    def test_get_migration(self):
        # Upgrade

        steps = self.repo.get_migration(version_from=StepVersion('4.0.1'), version_to=StepVersion('4.10.0'))
        self.assertEquals(steps.content_steps(),
                          (
                              'CREATE TABLE test_first (a INTEGER, b VARCHAR);',
                              'CREATE TABLE test_second (a INTEGER, b VARCHAR);',
                              'CREATE INDEX test_first_idx ON test_first (b);',
                          )
        )

        # Downgrade
        steps = self.repo.get_migration(version_from=StepVersion('4.10.0'), version_to=StepVersion('4.0.1'))
        self.assertEquals(steps.content_steps(),
                          (
                              'DROP INDEX test_first_idx;',
                              'DROP TABLE test_second;',
                              'DROP TABLE test_first;',
                          )
        )
Ejemplo n.º 10
0
    def __init__(self, path, application):
        self.repository_path = path

        self.application = application

        # a list of StepVersion objects
        self.versions = []
        application_path = os.path.join(self.repository_path, self.application)
        for version_string in os.listdir(application_path):
            current_version = StepVersion(version_string)
            self.versions.append(current_version)

        self.versions.sort()
Ejemplo n.º 11
0
    def test_run_migration_upgrade(self):
        repo = FileRepository('repository', 'app2')
        self.sqlupdater.migration = repo.get_migration(
            version_from=StepVersion('4.0.1'),
            version_to=StepVersion('4.10.0'))

        self.sqlupdater.run_migration()

        self.sqlupdater.migration = repo.get_migration(
            version_from=StepVersion('4.10.0'),
            version_to=StepVersion('4.0.1'))

        self.sqlupdater.run_migration()

        self.assertEqual(self.sqlupdater.schema_dump(),
                         ('BEGIN TRANSACTION;\n'
                          'CREATE TABLE dbupgrade_history (\n'
                          '    application  VARCHAR(90) NOT NULL,\n'
                          '    version VARCHAR(90) NOT NULL,\n'
                          '    timestamp DATE NOT NULL DEFAULT CURRENT_DATE\n'
                          ');\n'
                          'COMMIT;\n'))
Ejemplo n.º 12
0
    def test_order(self):
        version_one = StepVersion('4.0.1', 'one')
        version_two = StepVersion('4.0.1.2', 'two')
        version_three = StepVersion('4.5.0', 'three')

        assert_migration = [
            StepVersion('4.0.1'),
            StepVersion('4.0.1.2'),
            StepVersion('4.5.0'),
        ]

        migration = Migration('myApp')
        migration.append_step(version_one)
        migration.append_step(version_two)
        migration.append_step(version_three)

        # assert keys are still sorted
        self.assertEqual(migration.steps, sorted(assert_migration))
Ejemplo n.º 13
0
    def test_path_to_version(self):
        with self.assertRaises(RepositoryException):
            self.repo.path_to_version(version_from=StepVersion('7.0.0'))

        with self.assertRaises(RepositoryException):
            self.repo.path_to_version(version_to=StepVersion('7.0.0'))

        self.assertEqual(
            self.repo.path_to_version(version_from=StepVersion('4.5.0'), version_to=StepVersion('4.5.0')),
            []
        )

        self.assertEqual(
            self.repo.path_to_version(),
            [
                StepVersion('4.0.1'),
                StepVersion('4.0.1.2'),
                StepVersion('4.5.0'),
                StepVersion('4.10.0'),
            ]
        )

        # Upgrade
        self.assertEqual(
            self.repo.path_to_version(version_to=StepVersion('4.5.0')),
            [
                StepVersion('4.0.1'),
                StepVersion('4.0.1.2'),
                StepVersion('4.5.0'),
            ]
        )

        self.assertEqual(
            self.repo.path_to_version(version_from=StepVersion('4.0.1.2')),
            [
                StepVersion('4.5.0'),
                StepVersion('4.10.0'),
            ]
        )

        self.assertEqual(
            self.repo.path_to_version(version_from=StepVersion('4.0.1.2'), version_to=StepVersion('4.5.0')),
            [
                StepVersion('4.5.0'),
            ]
        )

        # Downgrade
        self.assertEqual(
            self.repo.path_to_version(version_from=StepVersion('4.5.0'), version_to=StepVersion('4.0.1.2')),
            [
                StepVersion('4.5.0'),
            ]
        )

        self.assertEqual(
            self.repo.path_to_version(version_from=StepVersion('4.10.0'), version_to=StepVersion('4.0.1')),
            [
                StepVersion('4.10.0'),
                StepVersion('4.5.0'),
                StepVersion('4.0.1.2'),

            ]
        )
Ejemplo n.º 14
0
 def get_current_version(self):
     """
     Return the version of the current component to update
     :return: StepVersion
     """
     return StepVersion(0)
Ejemplo n.º 15
0
    def test_equal(self):
        version = StepVersion('4.0.1')
        same_version = StepVersion('4.0.1')

        self.assertEqual(version, same_version)
Ejemplo n.º 16
0
    def test_greater(self):
        version = StepVersion('0.0.1')
        upper_version = StepVersion('0.1.0')

        self.assertLess(version, upper_version)