def main(argv=None, prog=None, **kwargs): current_path = os.path.dirname(__file__) gdw_alembic_config_file = os.path.join(current_path, 'alembic.ini') argv = ['-c', gdw_alembic_config_file] argv.extend(sys.argv[1:]) config.main(argv, prog, **kwargs) return 0
def app(): app = Sanic("books_review_test") app.config.update({ "DB_HOST": os.getenv("DB_HOST", "0.0.0.0"), "DB_PORT": os.getenv("DB_PORT", 5432), "DB_USER": os.getenv("DB_USER", "postgres"), "DB_PASSWORD": os.getenv("DB_PASS", "postgres"), "DB_DATABASE": "_".join([os.getenv("DB_NAME", "postgres"), "test"]), "DB_KWARGS": dict(max_inactive_connection_lifetime=59.0, ), "DB_ECHO": True }) for blueprint in get_blueprints(): app.blueprint(blueprint) db.init_app(app) main(["--raiseerr", "upgrade", "head"]) yield app main(["--raiseerr", "downgrade", "base"])
def client(): main(["--raiseerr", "upgrade", "head"]) with TestClient(app()) as client: yield client main(["--raiseerr", "downgrade", "base"])
def run(): def _open_environment(argv): if len(argv) < 2: print 'usage: %s </path/to/trac/env> [alembic options]' % argv[0] sys.exit(2) return open_environment(argv[1]) def _temporary_alembic_config(env, script_location): alembic_ini = tempfile.NamedTemporaryFile() alembic_ini.write('[alembic]\n' + \ 'script_location = %s\n' % script_location + \ 'sqlalchemy.url = %s\n' % TracAlchemy(env).sqlalchemy_connection_uri()) alembic_ini.flush() return alembic_ini argv = sys.argv AlembicMigrator.disable_upgrade_check = True env = _open_environment(argv) # monkey patch so we don't need the 'env.py' file engine = TracAlchemy(env).engine() ScriptDirectory.run_env = lambda self: online_migration(engine) plugins = AlembicMigrator(env).plugins if len(plugins) == 0: return # TODO: support more than one alembic plugin by changing the table name plugin = plugins[0] alembic_ini = _temporary_alembic_config(env, plugin.script_location()) alembic_arguments = ['--config=' + alembic_ini.name] + argv[2:] alembic_config.main(alembic_arguments)
def client(): from appointment.main import db, get_app main(["--raiseerr", "upgrade", "head"]) with TestClient(get_app()) as client: yield client
def revision(app, message): main(argv=[ '-c', path.join(path.dirname(__file__), 'alembic.ini'), '--name', app, 'revision', '-m', message, '--autogenerate' ])
def history(app): main(argv=[ '-c', path.join(path.dirname(__file__), 'alembic.ini'), '--name', app, 'history', '--verbose', ])
def module_db(): """make sure that the db is only initialized and teared down once per module.""" main(["--raiseerr", "upgrade", "head"]) yield main(["--raiseerr", "downgrade", "base"])
def setup_db(): engine = create_engine(DB_URL) alembic_args = [ '--raiseerr', 'upgrade', 'head', ] alembic_config.main(argv=alembic_args) return engine
def create_db_with_migrations(quiet=False): from ggrc.app import db from alembic.config import main import logging if quiet: logging.disable(logging.INFO) main(["-c", "migrations/alembic.ini", "upgrade", "head"]) if quiet: logging.disable(logging.NOTSET)
def client(): from core.application import get_app main(["--raiseerr", "upgrade", "head"]) with TestClient(get_app()) as client: yield client main(["--raiseerr", "downgrade", "base"])
def db_drop(): # db teardown - drop all config.main([ '-c', alembic_cfg, '-n', schema_name, '-x', 'dbconf={0}'.format(db_conf), '-x', 'adapter={0}'.format(db_adapter), 'downgrade', 'base' ]) # drop db incl. alembic tables drop_database(alchemy_url)
def client(): from gino_fastapi_demo.main import db, get_app main(["--raiseerr", "upgrade", "head"]) with TestClient(get_app()) as _client: yield _client main(["--raiseerr", "downgrade", "base"])
def client(): from sh.main import db, get_app main(["--raiseerr", "upgrade", "head"]) with TestClient(get_app()) as client: yield client main(["--raiseerr", "downgrade", "base"])
def app(): from yaaccu.app import create_app app = create_app() main(["--raiseerr", "upgrade", "head"]) yield app main(["--raiseerr", "downgrade", "base"])
def client(): main(["--raiseerr", "upgrade", "head"]) test_app = get_app(db, settings.TEST_DATABASE_URL) encoded_jwt = jwt.encode({"sub": "username"}, settings.SECRET_KEY, algorithm=settings.ALGORITHM) with TestClient(test_app) as client: token = f"bearer {encoded_jwt}" client.headers.update({"Authorization": token}) yield client main(["--raiseerr", "downgrade", "base"])
def main(): """Loads migration configuration and executes an upgrade.""" args = parse_arguments() argv = [ '-c', args.config_file, '-x', 'db_type=' + args.db_type, '-x', 'db_user='******'-x', 'db_password='******'-x', 'db_host=' + args.db_host, '-x', 'db_name=' + args.db_name, 'upgrade', args.revision ] config.main(argv)
def drop_db_with_migrations(quiet=False): from ggrc.app import db from alembic.config import main import logging if quiet: logging.disable(logging.INFO) main(["-c", "migrations/alembic.ini", "downgrade", "base"]) if quiet: logging.disable(logging.NOTSET) # Finally, clean up alembic_version itself db.session.execute('DROP TABLE alembic_version')
def client(): """Set up a clean database before running a test Run all migrations before test and downgrade afterwards.""" from app.main import app main(["--raiseerr", "upgrade", "head"]) app.dependency_overrides[is_admin] = is_admin_mocked app.dependency_overrides[is_service_account] = is_service_account_mocked with TestClient(app) as client: yield client app.dependency_overrides = {} main(["--raiseerr", "downgrade", "base"])
def migrate(): allembic_config = Config("alembic.ini") script = ScriptDirectory.from_config(allembic_config) if script.get_current_head() != get_current_revision(): print("Detect new Migrations!!!") backup_database() print("Start migrations.") sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', 'migrate.py') sys.argv.append('upgrade') sys.argv.append('head') main() print("Finish migrations.") else: print("Not detected migrations.")
def alembic(): " Alembic migration utils. " from flask import current_app from alembic.config import main from os import path as op global ARGV config = op.join(op.dirname(__file__), 'migrate', 'develop.ini' if current_app.debug else 'production.ini') ARGV = ['-c', config] + ARGV main(ARGV)
def reset_database(): conn = db.engine.connect() meta = sqlalchemy.MetaData(bind=conn, reflect=True) trans = conn.begin() # drop all tables meta.drop_all() trans.commit() # rerun the migrations alembic_config.main(argv=["upgrade", "head"]) # seed the permission sets seed_roles()
def setup_module(): global client if os.path.exists(test_db_path): os.unlink(test_db_path) os.environ["LINKA_MASTER_KEY"] = MASTER_KEY os.environ["DATABASE_URL"] = f"sqlite:///./{test_db_path}" config.main(argv=["upgrade", "head"]) from app.db import db from app import models headers["X-API-Key"] = asyncio.run(models.APIKey.create_new_key( db, "test")) from app import service client = TestClient(service.app)
def client(): """Set up a clean database before running a test Run all migrations before test and downgrade afterwards.""" from app.main import app main(["--raiseerr", "upgrade", "head"]) app.dependency_overrides[is_admin] = is_admin_mocked app.dependency_overrides[is_service_account] = is_service_account_mocked app.dependency_overrides[get_api_key] = get_api_key_mocked with TestClient(app) as client: yield client # Clean up created assets/versions/datasets so teardown doesn't break datasets_resp = client.get("/datasets") for ds in datasets_resp.json()["data"]: ds_id = ds["dataset"] if ds.get("versions") is not None: for version in ds["versions"]: assets_resp = client.get( f"/dataset/{ds_id}/{version}/assets") for asset in assets_resp.json()["data"]: print(f"DELETING ASSET {asset['asset_id']}") try: _ = client.delete( f"/dataset/{ds_id}/{version}/{asset['asset_id']}" ) except Exception as ex: print( f"Exception deleting asset {asset['asset_id']}: {ex}" ) try: _ = client.delete(f"/dataset/{ds_id}/{version}") except Exception as ex: print(f"Exception deleting version {version}: {ex}") try: _ = client.delete(f"/dataset/{ds_id}") except Exception as ex: print(f"Exception deleting dataset {ds_id}: {ex}") app.dependency_overrides = {} main(["--raiseerr", "downgrade", "base"])
def db_setup(alembic_cfg, schema_name, db_conf, db_adapter, alchemy_url, request): # run all db migrations config.main([ '-c', alembic_cfg, '-n', schema_name, '-x', 'dbconf={0}'.format(db_conf), '-x', 'adapter={0}'.format(db_adapter), 'upgrade', 'head' ]) def db_drop(): # db teardown - drop all config.main([ '-c', alembic_cfg, '-n', schema_name, '-x', 'dbconf={0}'.format(db_conf), '-x', 'adapter={0}'.format(db_adapter), 'downgrade', 'base' ]) # drop db incl. alembic tables drop_database(alchemy_url) request.addfinalizer(db_drop)
def run_alembic(args: List[str]) -> None: """ Forward arguments to alembic """ from alembic.config import main dbkey = "ERT_STORAGE_DATABASE_URL" dburl = os.getenv(dbkey) if dburl is None: sys.exit( f"Environment variable '{dbkey}' not set.\n" "It needs to point to a PostgreSQL server for alembic to work.") if not dburl.startswith("postgresql"): sys.exit( f"Environment variable '{dbkey}' does not point to a postgresql database.\n" "Only PostgreSQL is supported for alembic migrations at the moment.\n" f"Its value is: {dburl}") argv = [ "-c", os.path.join(os.path.dirname(__file__), "_alembic", "alembic.ini"), *args, ] try: main(argv=argv, prog="ert-storage alembic") except FileNotFoundError as exc: if os.path.basename(exc.filename) == "script.py.mako": sys.exit( f"\nAlembic could not find 'script.py.mako' in location:\n" f"\n{exc.filename}\n\n" "This is most likely because you've installed ert-storage without --edit mode\n" "Reinstall ert-storage with: pip install -e <path to ert-storage>" ) else: raise sys.exit(0)
def migrate_command(): """Migration commands - read more at https://alembic.readthedocs.org/en/latest/index.html""" import app.models argv = sys.argv[2:] main(argv=argv)
def db_migration(): ini_path = settings.PROJECT_ROOT_DIR / "alembic.ini" main(["--raiseerr", f"-c{ini_path}", "upgrade", "head"])
def generate_migration(self): logger.info("Generating new migrations") main(["--raiseerr", "revision", "--autogenerate"])
if options.redis_db is not None: redis_session_config['db_no'] = options.redis_db site_cache_config['db_no'] = options.redis_db # Windows10 关闭端口占用 # netstat -ano | findstr "8888" # taskkill -F -PID 6832 if __name__ == '__main__': if len(sys.argv) >= 2: if sys.argv[1] == 'upgradedb': # Alembic 是 Sqlalchemy 的作者实现的一个数据库版本化管理工具, # 它可以对基于Sqlalchemy的Model与数据库之间的历史关系进行版本化的维护。 # 更新数据库结构,初次获取或更新版本后调用一次python main.py upgradedb即可 from alembic.config import main # Alembic的控制台运行功能 main("upgrade head".split(' '), 'alembic') exit(0) # 加载命令行配置 parse_command_line() # 加载日志管理配置 log_config.init(config['port'], config['log_console'], config['log_file'], config['log_file_path'], config['log_level']) # 创建application try: logger.info("server start") application = Application() application.listen(config['port']) # 全局注册application config['application'] = application loop = tornado.ioloop.IOLoop.current()
"This is most likely because you've installed ert-storage without --edit mode\n" "Reinstall ert-storage with: pip install -e <path to ert-storage>" ) else: raise sys.exit(0) def print_usage() -> None: sys.exit( "Usage: ert-storage [alembic...]\n\n" "If alembic is given as the first argument, forward the rest of the\n" "arguments to alembic. Otherwise start ERT Storage in development mode." ) def main(args: Optional[List[str]] = None) -> None: if args is None: args = sys.argv[1:] if len(args) > 0: if args[0] == "alembic": run_alembic(args[1:]) else: print_usage() run_server() if __name__ == "__main__": main()
def alembic(*args): new_args = ['-c', 'alembic/alembic.ini'] new_args.extend(args) sys.exit(main(argv=new_args))