Skip to content
Snippets Groups Projects
Commit de19511e authored by Arie Peterson's avatar Arie Peterson
Browse files

Reset database migrations for old versions

parent d137a4cc
No related branches found
No related tags found
1 merge request!86Draft: Resolve "Make it easier to add an app"
Pipeline #36323 passed with stages
in 5 minutes and 54 seconds
from flask import Flask, jsonify from flask import Flask, jsonify
from flask_cors import CORS from flask_cors import CORS
from flask_jwt_extended import JWTManager from flask_jwt_extended import JWTManager
from flask_migrate import Migrate import flask_migrate
from jsonschema.exceptions import ValidationError from jsonschema.exceptions import ValidationError
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
...@@ -35,6 +35,8 @@ from helpers import ( ...@@ -35,6 +35,8 @@ from helpers import (
import cluster_config import cluster_config
from config import * from config import *
import logging import logging
import migration_reset
import sys
# Configure logging. # Configure logging.
from logging.config import dictConfig from logging.config import dictConfig
...@@ -64,8 +66,20 @@ app.logger.setLevel(logging.INFO) ...@@ -64,8 +66,20 @@ app.logger.setLevel(logging.INFO)
app.logger.info("Starting dashboard backend.") app.logger.info("Starting dashboard backend.")
cors = CORS(app) cors = CORS(app)
Migrate(app, db)
db.init_app(app) db.init_app(app)
with app.app_context():
# We have reset the alembic migration history at Stackspin version 2.2.
# This checks whether we need to prepare the database to follow that
# change.
migration_reset.reset()
flask_migrate.Migrate(app, db)
try:
with app.app_context():
flask_migrate.upgrade()
except Exception as e:
app.logger.info(f"upgrade failed: {type(e)}: {e}")
sys.exit(2)
# We need this app context in order to talk the database, which is managed by # We need this app context in order to talk the database, which is managed by
# flask-sqlalchemy, which assumes a flask app context. # flask-sqlalchemy, which assumes a flask app context.
......
from sqlalchemy import exc
from database import db
import logging
# We "reset" the alembic version history for Stackspin 2.2, to clean up our old
# mess of database migrations a bit, and in particular to make the transition
# easier to moving the source of truth for some of the data (list of apps) out
# of the database and into configmaps. This function deals with older clusters
# that have to be led through this transition. To determine if we need to do
# anything, we look at the `alembic_version` value in the database. If it's a
# legacy version, we delete the table so the alembic migration will view the
# database as "empty" and perform all new migrations on it. The new initial
# migration will have to handle that case specially, by checking if any tables
# already exist, and not do anything in that case.
def reset():
logging.info("Checking if alembic version needs to be reset.")
version = None
try:
result = db.session.execute("select version_num from alembic_version")
for row in result:
version = row[0]
except exc.ProgrammingError:
# We assume this means the alembic_version table doesn't exist, which
# is expected for new clusters.
pass
logging.info(f"alembic version: {version}")
legacy_versions = ["fc0892d07771", "3fa0c38ea1ac", "e08df0bef76f", "b514cca2d47b", "5f462d2d9d25", "27761560bbcb"]
if version in legacy_versions:
logging.info("This is an old version: resetting.")
db.session.execute("drop table alembic_version")
else:
logging.info("This is not a known legacy version: not resetting.")
...@@ -8,43 +8,3 @@ ...@@ -8,43 +8,3 @@
# the 'revision' command, regardless of autogenerate # the 'revision' command, regardless of autogenerate
# revision_environment = false # revision_environment = false
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic,flask_migrate
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
...@@ -13,7 +13,9 @@ config = context.config ...@@ -13,7 +13,9 @@ config = context.config
# 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.
fileConfig(config.config_file_name) # We commented this out, because we want to configure logging in the app
# itself, not here.
# fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env') logger = logging.getLogger('alembic.env')
# add your model's MetaData object here # add your model's MetaData object here
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment