def db_scope_fn(app, request): """ Session-wide test database """ migrations_path = Path(__file__).parent.parent.joinpath("migrations") with app.app_context(): _db.init_app(current_app) repo = DatabaseMigrationRepository(_db, "migrations") migrator = Migrator(repo, _db) if not migrator.repository_exists(): repo.create_repository() migrator.rollback(migrations_path) migrator.run(migrations_path)
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 notes = [] for migration in migrator.get_repository().get_ran(): for migration_directory in migration_list: try: migrator.reset(migration_directory) except QueryException as e: raise e except FileNotFoundError: pass if migrator.get_notes(): notes += migrator.get_notes() # Show notes from the migrator self.line('') for note in notes: if not ('Nothing to rollback.' in note): self.line(note) if not notes: self.info('Nothing to rollback')
def db(app): from orator.migrations import Migrator, DatabaseMigrationRepository db = app.extensions.db repository = DatabaseMigrationRepository(db, 'migrations') migrator = Migrator(repository, db) if not migrator.repository_exists(): repository.create_repository() path = os.path.join(app.root_path, 'db/migrations') migrator.run(path) yield db migrator.rollback(path)
def migrate_in_memory(database_manager: DatabaseManager): """ 参考: https://github.com/sdispater/orator/issues/159#issuecomment-288464538 """ migration_repository = DatabaseMigrationRepository(database_manager, 'migrations') migrator = Migrator(migration_repository, database_manager) if not migrator.repository_exists(): migration_repository.create_repository() migrations_path = Path(__file__).parents[4] / 'migrations' migrator.run(migrations_path)
def pytest_runtest_teardown(): from personalwebpageapi.models import db migrations_path = f'{os.getcwd()}/migrations' repository = DatabaseMigrationRepository( db, 'migrations', ) migrator = Migrator(repository, db) if not migrator.repository_exists(): repository.create_repository() migrator.set_connection(db.get_default_connection()) migrator.reset(migrations_path)
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)
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)
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 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)