Beispiel #1
0
    def test_migrations_are_run_up_when_outstanding_migrations_exist(self):
        resolver = flexmock(DatabaseManager)
        resolver.should_receive('connection').and_return(None)

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        g = flexmock(glob)
        g.should_receive('glob').with_args(os.path.join(
            os.getcwd(), '*_*.py')).and_return([
                os.path.join(os.getcwd(), '2_bar.py'),
                os.path.join(os.getcwd(), '1_foo.py'),
                os.path.join(os.getcwd(), '3_baz.py')
            ])

        migrator.get_repository().should_receive('get_ran').once().and_return(
            ['1_foo'])
        migrator.get_repository().should_receive(
            'get_next_batch_number').once().and_return(1)
        migrator.get_repository().should_receive('log').once().with_args(
            '2_bar', 1)
        migrator.get_repository().should_receive('log').once().with_args(
            '3_baz', 1)
        bar_mock = flexmock(MigrationStub())
        bar_mock.should_receive('up').once()
        baz_mock = flexmock(MigrationStub())
        baz_mock.should_receive('up').once()
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), '2_bar').once().and_return(bar_mock)
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), '3_baz').once().and_return(baz_mock)

        migrator.run(os.getcwd())
Beispiel #2
0
    def test_last_batch_of_migrations_can_be_rolled_back(self):
        resolver = flexmock(DatabaseManager)
        resolver.should_receive('connection').and_return(None)

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        foo_migration = MigrationStub('foo')
        bar_migration = MigrationStub('bar')
        migrator.get_repository().should_receive('get_last').once().and_return(
            [foo_migration, bar_migration])

        bar_mock = flexmock(MigrationStub())
        bar_mock.should_receive('down').once()
        foo_mock = flexmock(MigrationStub())
        foo_mock.should_receive('down').once()
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), 'bar').once().and_return(bar_mock)
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), 'foo').once().and_return(foo_mock)

        migrator.get_repository().should_receive('delete').once().with_args(
            bar_migration)
        migrator.get_repository().should_receive('delete').once().with_args(
            foo_migration)

        migrator.rollback(os.getcwd())
Beispiel #3
0
    def test_rollback_migration_can_be_pretended(self):
        resolver_mock = flexmock(DatabaseManager)
        resolver_mock.should_receive('connection').and_return({})
        resolver = flexmock(DatabaseManager({}))
        connection = flexmock(Connection(None))
        connection.should_receive('pretend').replace_with(
            lambda callback: callback(None))
        resolver.should_receive('connection').with_args(None).and_return(
            connection)

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        foo_migration = MigrationStub('foo')
        bar_migration = MigrationStub('bar')
        migrator.get_repository().should_receive('get_last').once().and_return(
            [foo_migration, bar_migration])

        bar_mock = flexmock(MigrationStub())
        bar_mock.should_receive('down').once()
        foo_mock = flexmock(MigrationStub())
        foo_mock.should_receive('down').once()
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), 'bar').once().and_return(bar_mock)
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), 'foo').once().and_return(foo_mock)

        migrator.rollback(os.getcwd(), True)
Beispiel #4
0
    def execute(self, i, o):
        """
        Executes the command.

        :type i: cleo.inputs.input.Input
        :type o: cleo.outputs.output.Output
        """
        super(MigrateCommand, self).execute(i, o)

        dialog = self.get_helper('dialog')
        confirm = dialog.ask_confirmation(
            o,
            '<question>Are you sure you want to proceed with the migration?</question> ',
            False)
        if not confirm:
            return

        database = i.get_option('database')
        repository = DatabaseMigrationRepository(self._resolver, 'migrations')

        migrator = Migrator(repository, self._resolver)

        self._prepare_database(migrator, database, i, o)

        pretend = i.get_option('pretend')

        path = i.get_option('path')

        if path is None:
            path = self._get_migration_path()

        migrator.run(path, pretend)

        for note in migrator.get_notes():
            o.writeln(note)
    def test_get_next_batch_number_returns_last_batch_number_plus_one(self):
        resolver_mock = flexmock(DatabaseManager)
        resolver_mock.should_receive('connection').and_return(None)
        resolver = flexmock(resolver_mock({}))
        repo = flexmock(DatabaseMigrationRepository(resolver, 'migrations'))
        repo.should_receive('get_last_batch_number').and_return(1)

        self.assertEqual(2, repo.get_next_batch_number())
Beispiel #6
0
    def test_nothing_is_rolled_back_when_nothing_in_repository(self):
        resolver = flexmock(DatabaseManager)
        resolver.should_receive('connection').and_return(None)

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        migrator.get_repository().should_receive('get_last').once().and_return(
            [])

        migrator.rollback(os.getcwd())
Beispiel #7
0
    def execute(self, i, o):
        """
        Executes the command.

        :type i: cleo.inputs.input.Input
        :type o: cleo.outputs.output.Output
        """
        super(StatusCommand, self).execute(i, o)

        database = i.get_option('database')
        repository = DatabaseMigrationRepository(self._resolver, 'migrations')

        migrator = Migrator(repository, self._resolver)

        if not migrator.repository_exists():
            return o.writeln('<error>No migrations found</error>')

        self._prepare_database(migrator, database, i, o)

        path = i.get_option('path')

        if path is None:
            path = self._get_migration_path()

        ran = migrator.get_repository().get_ran()

        migrations = []
        for migration in migrator._get_migration_files(path):
            if migration in ran:
                migrations.append(
                    ['<fg=cyan>%s</>' % migration, '<info>Yes</info>'])
            else:
                migrations.append(
                    ['<fg=cyan>%s</>' % migration, '<fg=red>No</>'])

        if migrations:
            table = self.get_helper('table')
            table.set_headers(['Migration', 'Ran?'])
            table.set_rows(migrations)
            table.render(o)
        else:
            return o.writeln('<error>No migrations found</error>')

        for note in migrator.get_notes():
            o.writeln(note)
Beispiel #8
0
    def test_nothing_is_done_when_no_migrations_outstanding(self):
        resolver_mock = flexmock(DatabaseManager)
        resolver_mock.should_receive('connection').and_return(None)
        resolver = flexmock(DatabaseManager({}))

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        g = flexmock(glob)
        g.should_receive('glob').with_args(os.path.join(
            os.getcwd(),
            '*_*.py')).and_return([os.path.join(os.getcwd(), '1_foo.py')])

        migrator.get_repository().should_receive('get_ran').once().and_return(
            ['1_foo'])

        migrator.run(os.getcwd())
    def test_get_last_migrations(self):
        resolver_mock = flexmock(DatabaseManager)
        resolver_mock.should_receive('connection').and_return(None)
        resolver = flexmock(resolver_mock({}))
        repo = flexmock(DatabaseMigrationRepository(resolver, 'migrations'))
        connection = flexmock(Connection(None))
        query = flexmock(QueryBuilder(connection, None, None))
        repo.should_receive('get_last_batch_number').and_return(1)
        repo.get_connection_resolver().should_receive('connection').with_args(
            None).and_return(connection)
        repo.get_connection().should_receive('table').once().with_args(
            'migrations').and_return(query)
        query.should_receive('where').once().with_args('batch',
                                                       1).and_return(query)
        query.should_receive('order_by').once().with_args(
            'migration', 'desc').and_return(query)
        query.should_receive('get').once().and_return('foo')

        self.assertEqual('foo', repo.get_last())
Beispiel #10
0
    def execute(self, i, o):
        """
        Executes the command.

        :type i: cleo.inputs.input.Input
        :type o: cleo.outputs.output.Output
        """
        super(InstallCommand, self).execute(i, o)

        database = i.get_option('database')
        repository = DatabaseMigrationRepository(self._resolver, 'migrations')

        repository.set_source(database)
        repository.create_repository()

        o.writeln('<info>Migration table created successfully</info>')
Beispiel #11
0
    def test_up_migration_can_be_pretended(self):
        resolver_mock = flexmock(DatabaseManager)
        resolver_mock.should_receive('connection').and_return({})
        resolver = flexmock(DatabaseManager({}))
        connection = flexmock(Connection(None))
        connection.should_receive('pretend').replace_with(
            lambda callback: callback(None))
        resolver.should_receive('connection').with_args(None).and_return(
            connection)

        migrator = flexmock(
            Migrator(
                flexmock(DatabaseMigrationRepository(resolver, 'migrations')),
                resolver))

        g = flexmock(glob)
        g.should_receive('glob').with_args(os.path.join(
            os.getcwd(), '*_*.py')).and_return([
                os.path.join(os.getcwd(), '2_bar.py'),
                os.path.join(os.getcwd(), '1_foo.py'),
                os.path.join(os.getcwd(), '3_baz.py')
            ])

        migrator.get_repository().should_receive('get_ran').once().and_return(
            ['1_foo'])
        migrator.get_repository().should_receive(
            'get_next_batch_number').once().and_return(1)
        bar_mock = flexmock(MigrationStub())
        bar_mock.should_receive('get_connection').once().and_return(None)
        bar_mock.should_receive('up').once()
        baz_mock = flexmock(MigrationStub())
        baz_mock.should_receive('get_connection').once().and_return(None)
        baz_mock.should_receive('up').once()
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), '2_bar').once().and_return(bar_mock)
        migrator.should_receive('_resolve').with_args(
            os.getcwd(), '3_baz').once().and_return(baz_mock)

        migrator.run(os.getcwd(), True)
 def get_repository(self):
     resolver = flexmock(DatabaseManager)
     resolver.should_receive('connection').and_return(None)
     return DatabaseMigrationRepository(flexmock(resolver({})),
                                        'migrations')