本文整理汇总了Python中alembic.script.ScriptDirectory.from_config方法的典型用法代码示例。如果您正苦于以下问题:Python ScriptDirectory.from_config方法的具体用法?Python ScriptDirectory.from_config怎么用?Python ScriptDirectory.from_config使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alembic.script.ScriptDirectory
的用法示例。
在下文中一共展示了ScriptDirectory.from_config方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_migrations
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def check_migrations(timeout):
"""
Function to wait for all airflow migrations to complete.
@param timeout:
@return:
"""
from alembic.runtime.migration import MigrationContext
from alembic.script import ScriptDirectory
config = _get_alembic_config()
script_ = ScriptDirectory.from_config(config)
with settings.engine.connect() as connection:
context = MigrationContext.configure(connection)
ticker = 0
while True:
source_heads = set(script_.get_heads())
db_heads = set(context.get_current_heads())
if source_heads == db_heads:
break
if ticker >= timeout:
raise TimeoutError("There are still unapplied migrations after {} "
"seconds.".format(ticker))
ticker += 1
time.sleep(1)
log.info('Waiting for migrations... %s second(s)', ticker)
示例2: test_env_emits_warning
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def test_env_emits_warning(self):
msg = (
"File %s loaded twice! ignoring. "
"Please ensure version_locations is unique."
% (
os.path.realpath(
os.path.join(
_get_staging_directory(),
"model1",
"%s_model1.py" % self.model1,
)
)
)
)
with assertions.expect_warnings(msg, regex=False):
script = ScriptDirectory.from_config(self.cfg)
script.revision_map.heads
eq_(
[rev.revision for rev in script.walk_revisions()],
[self.model1, self.model2, self.model3],
)
示例3: test_script_location_muliple
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def test_script_location_muliple(self):
config = _multi_dir_testing_config()
script = ScriptDirectory.from_config(config)
def normpath(path):
return path.replace("/", ":NORM:")
normpath = mock.Mock(side_effect=normpath)
with mock.patch("os.path.normpath", normpath):
eq_(
script._version_locations,
[
os.path.abspath(
os.path.join(_get_staging_directory(), "model1/")
).replace("/", ":NORM:"),
os.path.abspath(
os.path.join(_get_staging_directory(), "model2/")
).replace("/", ":NORM:"),
os.path.abspath(
os.path.join(_get_staging_directory(), "model3/")
).replace("/", ":NORM:"),
],
)
示例4: create
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def create(force=False):
"""Create tables if the database has not been configured yet."""
# Fail if there's an alembic version set
engine = db.get_engine(flask.current_app)
conn = engine.connect()
context = MigrationContext.configure(conn)
current_rev = context.get_current_revision()
alembic_config = flask.current_app.extensions['migrate'].migrate.get_config(
directory=migrate_path)
script = ScriptDirectory.from_config(alembic_config)
latest_rev = script.get_current_head()
if current_rev == latest_rev and not force:
print(u"You need to run 'evesrp -c config.py db migrate' to "
u"migrate to the latest database schema.")
else:
db.create_all()
if current_rev is None:
stamp()
示例5: run_alembic_migration
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def run_alembic_migration(db_uri, log_handler=None, setup_app=True):
if log_handler:
logging.getLogger(migration_name).addHandler(log_handler)
config = Config()
config.set_main_option("script_location", "data:migrations")
config.set_main_option("db_uri", db_uri)
if setup_app:
config.set_main_option("alembic_setup_app", "True")
else:
config.set_main_option("alembic_setup_app", "")
script = ScriptDirectory.from_config(config)
def fn(rev, context):
return script._upgrade_revs("head", rev)
with EnvironmentContext(config, script, fn=fn, destination_rev="head"):
script.run_env()
示例6: test_sqlalchemy_store_detects_schema_mismatch
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def test_sqlalchemy_store_detects_schema_mismatch(
tmpdir, db_url): # pylint: disable=unused-argument
def _assert_invalid_schema(engine):
with pytest.raises(MlflowException) as ex:
_verify_schema(engine)
assert ex.message.contains("Detected out-of-date database schema.")
# Initialize an empty database & verify that we detect a schema mismatch
engine = sqlalchemy.create_engine(db_url)
_assert_invalid_schema(engine)
# Create legacy tables, verify schema is still out of date
InitialBase.metadata.create_all(engine)
_assert_invalid_schema(engine)
# Run each migration. Until the last one, schema should be out of date
config = _get_alembic_config(db_url)
script = ScriptDirectory.from_config(config)
revisions = list(script.walk_revisions())
revisions.reverse()
for rev in revisions[:-1]:
command.upgrade(config, rev.revision)
_assert_invalid_schema(engine)
# Run migrations, schema verification should now pass
invoke_cli_runner(mlflow.db.commands, ['upgrade', db_url])
_verify_schema(engine)
示例7: show
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def show(cli_ctx, alembic_config):
'''Show the current schema information.'''
with cli_ctx.logger:
alembic_cfg = Config(alembic_config)
sa_url = alembic_cfg.get_main_option('sqlalchemy.url')
engine = sa.create_engine(sa_url)
with engine.begin() as connection:
context = MigrationContext.configure(connection)
current_rev = context.get_current_revision()
script = ScriptDirectory.from_config(alembic_cfg)
heads = script.get_heads()
head_rev = heads[0] if len(heads) > 0 else None
print(f'Current database revision: {current_rev}')
print(f'The head revision of available migrations: {head_rev}')
示例8: configure_db
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def configure_db(app):
"""
0.10 is the first version of ARA that ships with a stable database schema.
We can identify a database that originates from before this by checking if
there is an alembic revision available.
If there is no alembic revision available, assume we are running the first
revision which contains the latest state of the database prior to this.
"""
db.init_app(app)
log = logging.getLogger(app.logger_name)
if app.config.get('ARA_AUTOCREATE_DATABASE'):
with app.app_context():
migrations = app.config['DB_MIGRATIONS']
flask_migrate.Migrate(app, db, directory=migrations)
config = app.extensions['migrate'].migrate.get_config(migrations)
# Verify if the database tables have been created at all
inspector = Inspector.from_engine(db.engine)
if len(inspector.get_table_names()) == 0:
log.info('Initializing new DB from scratch')
flask_migrate.upgrade(directory=migrations)
# Get current alembic head revision
script = ScriptDirectory.from_config(config)
head = script.get_current_head()
# Get current revision, if available
connection = db.engine.connect()
context = MigrationContext.configure(connection)
current = context.get_current_revision()
if not current:
log.info('Unstable DB schema, stamping original revision')
flask_migrate.stamp(directory=migrations,
revision='da9459a1f71c')
if head != current:
log.info('DB schema out of date, upgrading')
flask_migrate.upgrade(directory=migrations)
示例9: from_config
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def from_config(cls, config):
"""Produce a new :class:`.ScriptDirectory` given a :class:`.Config`
instance.
The :class:`.Config` need only have the ``script_location`` key
present.
"""
script_location = config.get_main_option('script_location')
if script_location is None:
raise util.CommandError("No 'script_location' key "
"found in configuration.")
truncate_slug_length = config.get_main_option("truncate_slug_length")
if truncate_slug_length is not None:
truncate_slug_length = int(truncate_slug_length)
version_locations = config.get_main_option("version_locations")
if version_locations:
version_locations = _split_on_space_comma.split(version_locations)
return ScriptDirectory(
util.coerce_resource_to_filename(script_location),
file_template=config.get_main_option(
'file_template',
_default_file_template),
truncate_slug_length=truncate_slug_length,
sourceless=config.get_main_option("sourceless") == "true",
output_encoding=config.get_main_option("output_encoding", "utf-8"),
version_locations=version_locations
)
示例10: init_db
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def init_db():
"""
Initialize the database; call
:py:meth:`sqlalchemy.schema.MetaData.create_all` on the metadata object.
"""
logger.debug('Initializing database')
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
alembic_ini = pkg_resources.resource_filename(
pkg_resources.Requirement.parse('biweeklybudget'),
'biweeklybudget/alembic/alembic.ini'
)
topdir = os.path.abspath(
os.path.join(os.path.dirname(alembic_ini), '..', '..')
)
logger.debug('Alembic configuration: %s', alembic_ini)
with in_directory(topdir):
alembic_config = Config(alembic_ini)
script = ScriptDirectory.from_config(alembic_config)
curr_rev = _alembic_get_current_rev(alembic_config, script)
head_rev = script.get_revision("head").revision
if curr_rev is None:
# alembic not initialized at all; stamp with current version
logger.warning(
'Alembic not setup; creating all models and stamping'
)
logger.debug('Creating all models')
Base.metadata.create_all(engine)
command.stamp(alembic_config, "head")
logger.debug("DB stamped at %s", head_rev)
elif curr_rev != head_rev:
logger.warning("Alembic head is %s but this DB is at %s; "
"running migrations", head_rev, curr_rev)
command.upgrade(alembic_config, "head")
logger.info("Migrations complete")
else:
logger.debug('Alembic is at the correct head version (%s)',
curr_rev)
logger.debug('Done initializing DB')
init_event_listeners(db_session, engine)
示例11: _get_current_rev
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def _get_current_rev(self, alembic_conf, engine):
script = ScriptDirectory.from_config(alembic_conf)
with engine.connect() as conn:
with EnvironmentContext(alembic_conf, script) as env_context:
env_context.configure(conn, version_table="alembic_version")
migration_context = env_context.get_context()
revision = migration_context.get_current_revision()
return revision
示例12: _rev_list
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def _rev_list(self, alembic_conf):
script = ScriptDirectory.from_config(alembic_conf)
revs = []
for sc in script.walk_revisions(base="base", head="heads"):
revs.append({
'down_revision': sc.down_revision,
'revision': sc.revision,
'doc': sc.doc
})
revs.reverse()
return revs
示例13: test_only_single_head_revision_in_migrations
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def test_only_single_head_revision_in_migrations(self):
config = Config()
config.set_main_option("script_location", "airflow:migrations")
script = ScriptDirectory.from_config(config)
# This will raise if there are multiple heads
# To resolve, use the command `alembic merge`
script.get_current_head()
示例14: check_current_db_revision
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def check_current_db_revision():
current_rev = current_db_revision()
if current_rev is None:
return False
config = db.alembic_config
script = ScriptDirectory.from_config(config)
module_rev = script.get_current_head()
if current_rev != module_rev:
return False
return True
示例15: from_config
# 需要导入模块: from alembic.script import ScriptDirectory [as 别名]
# 或者: from alembic.script.ScriptDirectory import from_config [as 别名]
def from_config(cls, config):
"""Produce a new :class:`.ScriptDirectory` given a :class:`.Config`
instance.
The :class:`.Config` need only have the ``script_location`` key
present.
"""
script_location = config.get_main_option("script_location")
if script_location is None:
raise util.CommandError(
"No 'script_location' key " "found in configuration."
)
truncate_slug_length = config.get_main_option("truncate_slug_length")
if truncate_slug_length is not None:
truncate_slug_length = int(truncate_slug_length)
version_locations = config.get_main_option("version_locations")
if version_locations:
version_locations = _split_on_space_comma.split(version_locations)
return ScriptDirectory(
util.coerce_resource_to_filename(script_location),
file_template=config.get_main_option(
"file_template", _default_file_template
),
truncate_slug_length=truncate_slug_length,
sourceless=config.get_main_option("sourceless") == "true",
output_encoding=config.get_main_option("output_encoding", "utf-8"),
version_locations=version_locations,
timezone=config.get_main_option("timezone"),
hook_config=config.get_section("post_write_hooks", {}),
)