def check(self): response = None if not exists(const.db_path): self.upgrade() response = 'database created' else: db_session = DBSession() context = MigrationContext.configure(db_session.bind) cur_rev = context.get_current_revision() config_filepath = joinpath(dirname(self.script_location), 'alembic.ini') config = AlConfig(file_=config_filepath) config.set_main_option('script_location', self.script_location) script = ScriptDirectory.from_config(config) head_rev = script.get_current_head() if cur_rev != head_rev: self.backup() self.upgrade() response = 'database upgraded' self.insert_data() return response
def upgrade(self, script_location=None, db_path=None, dest_rev=None): script_location = script_location or self.script_location db_path = db_path or const.db_path dest_rev = dest_rev or 'head' if not exists(dirname(db_path)): makedirs(dirname(db_path)) sa_url = 'sqlite:///' + db_path config_filepath = joinpath(dirname(script_location), 'alembic.ini') config = AlConfig(file_=config_filepath) config.set_main_option('script_location', script_location) config.set_main_option('sqlalchemy.url', sa_url) script = ScriptDirectory.from_config(config) warnings.filterwarnings('ignore', category=UserWarning, module='.*alembic.*') def upgrade(rev, context): return script._upgrade_revs(dest_rev, rev) with EnvironmentContext(config, script, fn=upgrade, as_sql=False, starting_rev=None, destination_rev=dest_rev, tag=None): script.run_env()
def create_revision(message): """Create a database migration using alembic.""" config = get_config() alembic_cnf = AlConfig(config.PROJECT_ROOT + "/migrations/alembic.ini") alembic_cnf.set_main_option("script_location", config.PROJECT_ROOT + "/migrations") al_command.revision(alembic_cnf, message=message, autogenerate=True)
def database_upgrade(revision): """Upgrade database to given revision.""" config = get_config() alembic_cnf = AlConfig(config.PROJECT_ROOT + "/migrations/alembic.ini") alembic_cnf.set_main_option("script_location", config.PROJECT_ROOT + "/migrations") al_command.upgrade(alembic_cnf, revision)
def create_tables(): """Create all database tables for first time setup.""" click.echo("creating all tables") create_all_tables() config = get_config() alembic_cnf = AlConfig(config.PROJECT_ROOT + "/migrations/alembic.ini") alembic_cnf.set_main_option("script_location", config.PROJECT_ROOT + "/migrations") click.echo("stamping alembic head") al_command.stamp(alembic_cnf, "head") click.echo("done")
def init(): global engine, Session, session engine = sqla.create_engine(config.db_uri, echo=False) try: dialect = structures.Dialect.from_engine(engine) except ValueError: raise UnsupportedDBDialectError(engine.dialect.name) if dialect == structures.Dialect.SQLITE: event.listen(engine, "connect", load_extensions) al_cfg = AlConfig() al_cfg.set_main_option("script_location", "utmo:alembic") al_cfg.set_main_option("sqlalchemy.url", config.db_uri) alembic_cmd.upgrade(al_cfg, "head") Base.metadata.create_all(bind=engine) Session = sessionmaker(bind=engine) session = Session()
def init_rucio_database(echo=True, tests=False): """ Applies the schema to the database. Run this command once to build the database. """ rucio_cfg_file = os.environ["RUCIO_HOME"]+"/etc/rucio.cfg" alembic_cfg_file = os.environ["RUCIO_HOME"]+"/etc/alembic.ini" alembic_script_location = os.environ["RUCIO_HOME"]+"/rucio/db/sqla/migrate_repo" # Apply database schema to the provided endpoint print("Rucio configuration file: ", rucio_cfg_file) print("Alembic.ini configuration file: ", alembic_cfg_file) print("Applying the Rucio database schema to database endpoint: "+config_get('database', 'default')+"... ", end='', flush=True) engine = get_temp_engine(echo=echo) register_models(engine) print("done") # Put the database under version control print("Stamping databse version in alembic... ", end='', flush=True) alembic_cfg = AlConfig(alembic_cfg_file) alembic_cfg.set_main_option("script_location", alembic_script_location) command.stamp(alembic_cfg, "head") print("done")