Exemple #1
0
    def handle(self):
        """
        Executes the command.
        """
        if not self.confirm_to_proceed(
            "<question>Are you sure you want to rollback the last 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.rollback(path, pretend)

        for note in migrator.get_notes():
            self.line(note)
    def fire(self):
        """
        Executes the command.
        """
        dialog = self.get_helper('dialog')
        confirm = dialog.ask_confirmation(
            self.output,
            '<question>Are you sure you want to rollback the last 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.rollback(path, pretend)

        for note in migrator.get_notes():
            self.line(note)
    def fire(self):
        """
        Executes the command.
        """
        dialog = self.get_helper('dialog')
        confirm = dialog.ask_confirmation(
            self.output,
            '<question>Are you sure you want to rollback the last 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.rollback(path, pretend)

        for note in migrator.get_notes():
            self.line(note)
Exemple #4
0
    def handle(self):
        """
        Executes the command.
        """
        prompt_msg = ('<question>Are you sure you want to rollback '
                      'the last migration?:</question> ')
        if not self.confirm_to_proceed(prompt_msg):
            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.rollback(path, pretend)

        for note in migrator.get_notes():
            self.line(note)
Exemple #5
0
    def handle(self):
        """
        Executes the command.
        """
        confirm = self.confirm(
            '<question>Are you sure you want to rollback the last migration?</question> ',
            True
        )
        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.rollback(path, pretend)

        for note in migrator.get_notes():
            self.line(note)
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)
Exemple #7
0
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)
Exemple #8
0
    def handle(self):
        """
        Executes the command.
        """
        confirm = self.confirm(
            '<question>Are you sure you want to reset all of the migrations?</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 = bool(self.option('pretend'))

        path = self.option('path')

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

        while True:
            count = migrator.rollback(path, pretend)

            for note in migrator.get_notes():
                self.line(note)

            if count == 0:
                break
    def fire(self):
        """
        Executes the command.
        """
        dialog = self.get_helper("dialog")
        confirm = dialog.ask_confirmation(
            self.output, "<question>Are you sure you want to reset all of the migrations?</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 = bool(self.option("pretend"))

        path = self.option("path")

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

        while True:
            count = migrator.rollback(path, pretend)

            for note in migrator.get_notes():
                self.line(note)

            if count == 0:
                break
Exemple #10
0
    def handle(self):
        """
        Executes the command.
        """
        confirm = self.confirm(
            '<question>Are you sure you want to reset all of the migrations?</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 = bool(self.option('pretend'))

        path = self.option('path')

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

        while True:
            count = migrator.rollback(path, pretend)

            for note in migrator.get_notes():
                self.line(note)

            if count == 0:
                break
Exemple #11
0
    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
Exemple #12
0
    def execute(self, i, o):
        """
        Executes the command.

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

        dialog = self.get_helper('dialog')
        confirm = dialog.ask_confirmation(
            o,
            '<question>Are you sure you want to rollback the last 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.rollback(path, pretend)

        for note in migrator.get_notes():
            o.writeln(note)
Exemple #13
0
class Migrations(HasColoredCommands):
    def __init__(self, connection=None):
        self._ran = []
        self._notes = []
        from config import database
        database_dict = database.DB

        self.repository = DatabaseMigrationRepository(database.DB,
                                                      'migrations')
        self.migrator = Migrator(self.repository, database.DB)
        if not connection or connection == 'default':
            connection = database.DATABASES['default']
        self.migrator.set_connection(connection)
        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 run(self):
        for directory in self.migration_directories:
            try:
                if len(self.migration_directories) > 1:
                    self.info('Migrating: {}'.format(directory))
                self.migrator.run(directory)
                self._ran.append(self.repository.get_ran())
                self._notes = self.migrator._notes
            except Exception as e:
                self.danger(str(e))

        return self

    def rollback(self):
        for directory in self.migration_directories:
            try:
                if len(self.migration_directories) > 1:
                    self.info('Migrating: {}'.format(directory))
                self.migrator.rollback(directory)
                self._ran.append(self.repository.get_ran())
                self._notes = self.migrator._notes
            except Exception as e:
                self.danger(str(e))

        return self

    def refresh(self):
        self.run()
        self.rollback()

    def reset(self):
        for directory in self.migration_directories:
            try:
                if len(self.migration_directories) > 1:
                    self.info('Migrating: {}'.format(directory))
                self.migrator.reset(directory)
                self._ran.append(self.repository.get_ran())
                self._notes = self.migrator._notes
            except Exception as e:
                self.danger(str(e))

        return self

    def ran(self):
        return self._ran