Fix main app migration pathway
This commit is contained in:
parent
561522c6d3
commit
aff1a08a83
4 changed files with 23 additions and 8 deletions
|
@ -9,6 +9,7 @@ from util.ipresolver import NoopIPResolver
|
||||||
|
|
||||||
from config_app._init_config import ROOT_DIR
|
from config_app._init_config import ROOT_DIR
|
||||||
from config_app.config_util.config import get_config_provider
|
from config_app.config_util.config import get_config_provider
|
||||||
|
from util.security.instancekeys import InstanceKeys
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -35,5 +36,6 @@ else:
|
||||||
config_provider.update_app_config(app.config)
|
config_provider.update_app_config(app.config)
|
||||||
superusers = SuperUserManager(app)
|
superusers = SuperUserManager(app)
|
||||||
ip_resolver = NoopIPResolver()
|
ip_resolver = NoopIPResolver()
|
||||||
|
instance_keys = InstanceKeys(app)
|
||||||
|
|
||||||
model.config.app_config = app.config
|
model.config.app_config = app.config
|
||||||
|
|
|
@ -7,7 +7,7 @@ from flask import abort, request
|
||||||
|
|
||||||
from config_app.config_endpoints.api.suconfig_models_pre_oci import pre_oci_model as model
|
from config_app.config_endpoints.api.suconfig_models_pre_oci import pre_oci_model as model
|
||||||
from config_app.config_endpoints.api import resource, ApiResource, nickname, validate_json_request
|
from config_app.config_endpoints.api import resource, ApiResource, nickname, validate_json_request
|
||||||
from config_app.c_app import app, config_provider, superusers, OVERRIDE_CONFIG_DIRECTORY, ip_resolver
|
from config_app.c_app import app, config_provider, superusers, OVERRIDE_CONFIG_DIRECTORY, ip_resolver, instance_keys
|
||||||
|
|
||||||
from auth.auth_context import get_authenticated_user
|
from auth.auth_context import get_authenticated_user
|
||||||
from data.users import get_federated_service_name, get_users_handler
|
from data.users import get_federated_service_name, get_users_handler
|
||||||
|
@ -181,7 +181,7 @@ class SuperUserSetupDatabase(ApiResource):
|
||||||
log_handler = _AlembicLogHandler()
|
log_handler = _AlembicLogHandler()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
run_alembic_migration(db_uri, log_handler)
|
run_alembic_migration(db_uri, log_handler, setup_app=False)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
return {
|
return {
|
||||||
'error': str(ex)
|
'error': str(ex)
|
||||||
|
@ -296,8 +296,9 @@ class SuperUserConfigValidate(ApiResource):
|
||||||
if not config_provider.config_exists():
|
if not config_provider.config_exists():
|
||||||
config = request.get_json()['config']
|
config = request.get_json()['config']
|
||||||
validator_context = ValidatorContext.from_app(app, config, request.get_json().get('password', ''),
|
validator_context = ValidatorContext.from_app(app, config, request.get_json().get('password', ''),
|
||||||
ip_resolver=ip_resolver,
|
instance_keys=instance_keys,
|
||||||
config_provider=config_provider)
|
ip_resolver=ip_resolver,
|
||||||
|
config_provider=config_provider)
|
||||||
return validate_service_for_config(service, validator_context)
|
return validate_service_for_config(service, validator_context)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,13 @@ from release import GIT_HEAD, REGION, SERVICE
|
||||||
from util.morecollections import AttrDict
|
from util.morecollections import AttrDict
|
||||||
|
|
||||||
config = context.config
|
config = context.config
|
||||||
DB_URI = config.get_main_option('db_uri')
|
DB_URI = config.get_main_option('db_uri', 'sqlite:///test/data/test.db')
|
||||||
|
|
||||||
|
if config.get_main_option('alembic_setup_app', 'True') == 'True':
|
||||||
|
# needed for db connections
|
||||||
|
from app import app
|
||||||
|
DB_URI = app.config['DB_URI']
|
||||||
|
|
||||||
config.set_main_option('sqlalchemy.url', unquote(DB_URI))
|
config.set_main_option('sqlalchemy.url', unquote(DB_URI))
|
||||||
# Interpret the config file for Python logging.
|
# Interpret the config file for Python logging.
|
||||||
# This line sets up loggers basically.
|
# This line sets up loggers basically.
|
||||||
|
@ -68,7 +74,7 @@ def run_migrations_offline():
|
||||||
context.configure(url=url, target_metadata=target_metadata, transactional_ddl=True)
|
context.configure(url=url, target_metadata=target_metadata, transactional_ddl=True)
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations(tables=tables)
|
context.run_migrations(tables=tables, tester=get_tester())
|
||||||
|
|
||||||
def run_migrations_online():
|
def run_migrations_online():
|
||||||
"""Run migrations in 'online' mode.
|
"""Run migrations in 'online' mode.
|
||||||
|
@ -97,7 +103,7 @@ def run_migrations_online():
|
||||||
try:
|
try:
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
try:
|
try:
|
||||||
context.run_migrations(tables=tables)
|
context.run_migrations(tables=tables, tester=get_tester())
|
||||||
except (CommandError, ResolutionError) as ex:
|
except (CommandError, ResolutionError) as ex:
|
||||||
if 'No such revision' not in str(ex):
|
if 'No such revision' not in str(ex):
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -5,13 +5,19 @@ from alembic.script import ScriptDirectory
|
||||||
from alembic.environment import EnvironmentContext
|
from alembic.environment import EnvironmentContext
|
||||||
from alembic.migration import __name__ as migration_name
|
from alembic.migration import __name__ as migration_name
|
||||||
|
|
||||||
def run_alembic_migration(db_uri, log_handler=None):
|
def run_alembic_migration(db_uri, log_handler=None, setup_app=True):
|
||||||
if log_handler:
|
if log_handler:
|
||||||
logging.getLogger(migration_name).addHandler(log_handler)
|
logging.getLogger(migration_name).addHandler(log_handler)
|
||||||
|
|
||||||
config = Config()
|
config = Config()
|
||||||
config.set_main_option("script_location", "data:migrations")
|
config.set_main_option("script_location", "data:migrations")
|
||||||
config.set_main_option("db_uri", db_uri)
|
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)
|
script = ScriptDirectory.from_config(config)
|
||||||
|
|
||||||
def fn(rev, context):
|
def fn(rev, context):
|
||||||
|
|
Reference in a new issue