本文整理汇总了Python中alembic.migration.MigrationContext.configure方法的典型用法代码示例。如果您正苦于以下问题:Python MigrationContext.configure方法的具体用法?Python MigrationContext.configure怎么用?Python MigrationContext.configure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alembic.migration.MigrationContext
的用法示例。
在下文中一共展示了MigrationContext.configure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_filters
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def run_filters(self, object_, name, type_, reflected, compare_to):
"""Run the context's object filters and return True if the targets
should be part of the autogenerate operation.
This method should be run for every kind of object encountered within
an autogenerate operation, giving the environment the chance
to filter what objects should be included in the comparison.
The filters here are produced directly via the
:paramref:`.EnvironmentContext.configure.include_object`
and :paramref:`.EnvironmentContext.configure.include_symbol`
functions, if present.
"""
for fn in self._object_filters:
if not fn(object_, name, type_, reflected, compare_to):
return False
else:
return True
示例2: check_migrations
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [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)
示例3: __init__
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def __init__(self, registry):
self.withoutautomigration = registry.withoutautomigration
self.conn = registry.connection()
self.loaded_views = registry.loaded_views
self.metadata = registry.declarativebase.metadata
self.ddl_compiler = self.conn.dialect.ddl_compiler(
self.conn.dialect, None)
opts = {
'include_schemas': True,
'compare_server_default': True,
'render_item': self.render_item,
'compare_type': self.compare_type,
}
self.context = MigrationContext.configure(self.conn, opts=opts)
self.operation = Operations(self.context)
self.reinit_all = Configuration.get('reinit_all', False)
self.reinit_tables = Configuration.get('reinit_tables', False)
self.reinit_columns = Configuration.get('reinit_columns', False)
self.reinit_indexes = Configuration.get('reinit_indexes', False)
self.reinit_constraints = Configuration.get(
'reinit_constraints', False)
示例4: setUp
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def setUp(self):
self.conn = conn = self.bind.connect()
ctx_opts = {
"compare_type": True,
"compare_server_default": True,
"target_metadata": self.m2,
"upgrade_token": "upgrades",
"downgrade_token": "downgrades",
"alembic_module_prefix": "op.",
"sqlalchemy_module_prefix": "sa.",
"include_object": _default_object_filters,
}
if self.configure_opts:
ctx_opts.update(self.configure_opts)
self.context = context = MigrationContext.configure(
connection=conn, opts=ctx_opts
)
self.autogen_context = api.AutogenContext(context, self.m2)
示例5: setUp
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def setUp(self):
self.conn = config.db.connect()
self.conn.execute(
text(
"""
create table foo(
id integer primary key,
data varchar(50),
x integer
)
"""
)
)
context = MigrationContext.configure(self.conn)
self.op = op.Operations(context)
self.t1 = table("foo", column("id"), column("data"), column("x"))
示例6: test_render_server_default_context_passed
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def test_render_server_default_context_passed(self):
uo = ops.UpgradeOps(
ops=[
ops.CreateTableOp(
"sometable",
[Column("x", types.DateTime(), server_default=func.now())],
)
]
)
context = MigrationContext.configure(dialect_name="sqlite")
eq_ignore_whitespace(
autogenerate.render_python_code(uo, migration_context=context),
"# ### commands auto generated by Alembic - please adjust! ###\n"
" op.create_table('sometable',\n"
" sa.Column('x', sa.DateTime(), "
"server_default=sa.text(!U'(CURRENT_TIMESTAMP)'), nullable=True)\n"
" )\n"
" # ### end Alembic commands ###",
)
示例7: setUp
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def setUp(self):
convention = {
"ix": "ix_%(custom)s_%(column_0_label)s",
"uq": "uq_%(custom)s_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(custom)s_%(table_name)s",
"fk": "fk_%(custom)s_%(table_name)s_"
"%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(custom)s_%(table_name)s",
"custom": lambda const, table: "ct",
}
self.metadata = MetaData(naming_convention=convention)
ctx_opts = {
"sqlalchemy_module_prefix": "sa.",
"alembic_module_prefix": "op.",
"target_metadata": MetaData(),
}
context = MigrationContext.configure(
dialect_name="postgresql", opts=ctx_opts
)
self.autogen_context = api.AutogenContext(context)
示例8: create
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [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()
示例9: configure_db
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [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)
示例10: fix
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def fix(self):
"""Uses Alembic batch operations to alter the column datatype in the
table.
"""
context = MigrationContext.configure(self.parent.engine.connect())
op = Operations(context)
for table in self.parent.base.metadata.sorted_tables:
if table.name == self.table:
for column in table.columns:
if column.name == self.column['name']:
with op.batch_alter_table(table.name) as batch_op:
batch_op.alter_column(column.name,
type_=column.type)
return
示例11: drop_airflow_models
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def drop_airflow_models(connection):
"""
Drops all airflow models.
@param connection:
@return: None
"""
from airflow.models.base import Base
# Drop connection and chart - those tables have been deleted and in case you
# run resetdb on schema with chart or users table will fail
chart = Table('chart', Base.metadata)
chart.drop(settings.engine, checkfirst=True)
user = Table('user', Base.metadata)
user.drop(settings.engine, checkfirst=True)
users = Table('users', Base.metadata)
users.drop(settings.engine, checkfirst=True)
dag_stats = Table('dag_stats', Base.metadata)
dag_stats.drop(settings.engine, checkfirst=True)
Base.metadata.drop_all(connection)
# we remove the Tables here so that if resetdb is run metadata does not keep the old tables.
Base.metadata.remove(dag_stats)
Base.metadata.remove(users)
Base.metadata.remove(user)
Base.metadata.remove(chart)
# alembic adds significant import time, so we import it lazily
# noinspection PyUnresolvedReferences
from alembic.migration import MigrationContext
migration_ctx = MigrationContext.configure(connection)
# noinspection PyProtectedMember
version = migration_ctx._version # pylint: disable=protected-access
if version.exists(connection):
version.drop(connection)
示例12: current_db_revision
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def current_db_revision():
ctx = MigrationContext.configure(db.engine.connect())
return ctx.get_current_revision()
示例13: render_python_code
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def render_python_code(
up_or_down_op,
sqlalchemy_module_prefix="sa.",
alembic_module_prefix="op.",
render_as_batch=False,
imports=(),
render_item=None,
migration_context=None,
):
"""Render Python code given an :class:`.UpgradeOps` or
:class:`.DowngradeOps` object.
This is a convenience function that can be used to test the
autogenerate output of a user-defined :class:`.MigrationScript` structure.
"""
opts = {
"sqlalchemy_module_prefix": sqlalchemy_module_prefix,
"alembic_module_prefix": alembic_module_prefix,
"render_item": render_item,
"render_as_batch": render_as_batch,
}
if migration_context is None:
from ..runtime.migration import MigrationContext
from sqlalchemy.engine.default import DefaultDialect
migration_context = MigrationContext.configure(
dialect=DefaultDialect()
)
autogen_context = AutogenContext(migration_context, opts=opts)
autogen_context.imports = set(imports)
return render._indent(
render._render_cmd_body(up_or_down_op, autogen_context)
)
示例14: setup_class
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def setup_class(cls):
cls.bind = config.db
staging_env()
cls.migration_context = MigrationContext.configure(
connection=cls.bind.connect(),
opts={"compare_type": True, "compare_server_default": True},
)
示例15: setUp
# 需要导入模块: from alembic.migration import MigrationContext [as 别名]
# 或者: from alembic.migration.MigrationContext import configure [as 别名]
def setUp(self):
ctx_opts = {
"sqlalchemy_module_prefix": "sa.",
"alembic_module_prefix": "op.",
"target_metadata": MetaData(),
"user_module_prefix": None,
}
context = MigrationContext.configure(
dialect_name="custom_dialect", opts=ctx_opts
)
self.autogen_context = api.AutogenContext(context)