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())
def test_migrations_are_run_up_directly_if_transactional_is_false(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive('transaction').never() resolver.should_receive('connection').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(), '[0-9]*_*.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.transactional = False bar_mock.set_connection(connection) bar_mock.should_receive('up').once() baz_mock = flexmock(MigrationStub()) baz_mock.transactional = False baz_mock.set_connection(connection) 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())
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(), '[0-9]*_*.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 handle(self): confirm = self.confirm( '<question>Are you sure you want to proceed with the migration?</question> ', False) if not confirm: return database = self.option('database') repository = DatabaseMigrationRepository(self.resolver, 'migrations') migrator = Migrator(repository, self.resolver) self._prepare_database(migrator, database) pretend = self.option('pretend') path = self.option('path') if path is None: path = self._get_migration_path() migrator.run(path, pretend) for note in migrator.get_notes(): self.line(note) # If the "seed" option has been given, we will rerun the database seed task # to repopulate the database. if self.option('seed'): options = [('-n', True)] if database: options.append(('--database', database)) if self.get_definition().has_option('config'): options.append(('--config', self.option('config'))) if self.option('seed-path'): options.append(('--path', self.option('seed-path'))) self.call('db:seed', options)
def handle(self): """ Executes the command. """ database = self.option('database') self.resolver.set_default_connection(database) repository = DatabaseMigrationRepository(self.resolver, 'migrations') migrator = Migrator(repository, self.resolver) if not migrator.repository_exists(): return self.error('No migrations found') self._prepare_database(migrator, database) path = self.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</>']) else: migrations.append( ['<fg=cyan>%s</>' % migration, '<fg=red>No</>']) if migrations: table = self.table(['Migration', 'Ran?'], migrations) table.render() else: return self.error('No migrations found') for note in migrator.get_notes(): self.line(note)
def test_migrations_are_run_up_when_outstanding_migrations_exist(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive("transaction").twice().and_return(connection) resolver.should_receive("connection").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(), "[0-9]*_*.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.set_connection(connection) bar_mock.should_receive("up").once() baz_mock = flexmock(MigrationStub()) baz_mock.set_connection(connection) 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())
def handle(self): sys.path.append(os.getcwd()) try: add_venv_site_packages() from wsgi import container except ImportError: self.comment( 'This command must be ran inside of the root of a Masonite project directory' ) # Get any migration files from the Service Container migration_directory = ['databases/migrations'] for key, value in container.providers.items(): if 'MigrationDirectory' in key: migration_directory.append(value) # Load in the Orator migration system from orator.migrations import Migrator, DatabaseMigrationRepository from config import database repository = DatabaseMigrationRepository(database.DB, 'migrations') migrator = Migrator(repository, database.DB) if not migrator.repository_exists(): repository.create_repository() # Create a new list of migrations with the correct file path instead migration_list = [] for migration in migrator.get_repository().get_ran(): for directory in migration_directory: if os.path.exists(os.path.join(directory, migration + '.py')): migration_list.append(os.path.join(os.getcwd(), directory)) break # Rollback the migrations for migration in migration_list: try: migrator.rollback(migration) for note in migrator.get_notes(): self.line(note) except Exception: pass
def test_last_batch_of_migrations_can_be_rolled_back_directly_if_transactional_is_false( self): import orator.migrations.migrator as migrator d = flexmock(migrator) resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive('transaction').never() resolver.should_receive('connection').and_return(connection) d.should_receive('dump').with_args(connection).and_return('') 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.transactional = False bar_mock.set_connection(connection) bar_mock.should_receive('down').once() foo_mock = flexmock(MigrationStub()) foo_mock.transactional = False foo_mock.set_connection(connection) 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())
def __init__(self): self._ran = [] self._notes = [] from config import database self.repository = DatabaseMigrationRepository(database.DB, 'migrations') self.migrator = Migrator(self.repository, database.DB) if not self.repository.repository_exists(): self.repository.create_repository() from wsgi import container self.migration_directories = ['databases/migrations'] for key, value in container.providers.items(): if isinstance(key, str) and 'MigrationDirectory' in key: self.migration_directories.append(value) try: add_venv_site_packages() except ImportError: self.comment( 'This command must be ran inside of the root of a Masonite project directory')
def handle(self): if not self.confirm_to_proceed( "<question>Are you sure you want to proceed with the migration?</question> " ): return database = self.option("database") repository = DatabaseMigrationRepository(self.resolver, "migrations") migrator = Migrator(repository, self.resolver) self._prepare_database(migrator, database) pretend = self.option("pretend") path = self.option("path") if path is None: path = self._get_migration_path() migrator.run(path, pretend) for note in migrator.get_notes(): self.line(note) # If the "seed" option has been given, we will rerun the database seed task # to repopulate the database. if self.option("seed"): options = [("--force", self.option("force"))] if database: options.append(("--database", database)) if self.get_definition().has_option("config"): options.append(("--config", self.option("config"))) if self.option("seed-path"): options.append(("--path", self.option("seed-path"))) self.call("db:seed", options)
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(), '[0-9]*_*.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 execute(self, i, o): """ Executes the command. :type i: cleo.inputs.input.Input :type o: cleo.outputs.output.Output """ super(ResetCommand, self).execute(i, o) dialog = self.get_helper('dialog') confirm = dialog.ask_confirmation( o, '<question>Are you sure you want to reset all of the migrations?</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 = bool(i.get_option('pretend')) path = i.get_option('path') if path is None: path = self._get_migration_path() while True: count = migrator.rollback(path, pretend) for note in migrator.get_notes(): o.writeln(note) if count == 0: break
def test_last_batch_of_migrations_can_be_rolled_back_directly_if_transactional_is_false( self, ): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive("transaction").never() resolver.should_receive("connection").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.transactional = False bar_mock.set_connection(connection) bar_mock.should_receive("down").once() foo_mock = flexmock(MigrationStub()) foo_mock.transactional = False foo_mock.set_connection(connection) 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())
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("get_logged_queries").twice().and_return([]) 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(), "[0-9]*_*.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(connection) bar_mock.should_receive("up").once() baz_mock = flexmock(MigrationStub()) baz_mock.should_receive("get_connection").once().and_return(connection) 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")