broken auth WIP

This commit is contained in:
2020-05-09 20:36:14 -05:00
parent 119d4a0052
commit 246ef00540
7 changed files with 122 additions and 13 deletions

View File

@ -8,6 +8,7 @@ from psycopg2 import pool
from flask import current_app
from flask import g
from capsulflask.model import Model
def init_app(app):
databaseUrl = urlparse(app.config['DATABASE_URL'])
@ -30,14 +31,14 @@ def init_app(app):
with open(join(schemaMigrationsPath, filename), 'rb') as file:
schemaMigrations[key] = file.read().decode("utf8")
db = app.config['PSYCOPG2_CONNECTION_POOL'].getconn()
connection = app.config['PSYCOPG2_CONNECTION_POOL'].getconn()
hasSchemaVersionTable = False
actionWasTaken = False
schemaVersion = 0
desiredSchemaVersion = 2
cursor = db.cursor()
cursor = connection.cursor()
cursor.execute("""
SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema = '{}'
@ -52,7 +53,7 @@ def init_app(app):
print("no table named schemaversion found in the {} schema. running migration 01_up".format(app.config['DATABASE_SCHEMA']))
try:
cursor.execute(schemaMigrations["01_up"])
db.commit()
connection.commit()
except:
print("unable to create the schemaversion table because: {}".format(my_exec_info_message(sys.exc_info())))
exit(1)
@ -74,7 +75,7 @@ def init_app(app):
))
try:
cursor.execute(schemaMigrations[migrationKey])
db.commit()
connection.commit()
except KeyError:
print("missing schema migration script: {}_xyz.sql".format(migrationKey))
exit(1)
@ -97,7 +98,7 @@ def init_app(app):
cursor.close()
app.config['PSYCOPG2_CONNECTION_POOL'].putconn(db)
app.config['PSYCOPG2_CONNECTION_POOL'].putconn(connection)
print("{} current schemaVersion: \"{}\"".format(
("schema migration completed." if actionWasTaken else "schema is already up to date. "), schemaVersion
@ -106,17 +107,20 @@ def init_app(app):
app.teardown_appcontext(close_db)
def get_db():
if 'db' not in g:
g.db = current_app.config['PSYCOPG2_CONNECTION_POOL'].getconn()
return g.db
def get_model():
if 'model' not in g:
connection = current_app.config['PSYCOPG2_CONNECTION_POOL'].getconn()
cursor = connection.cursor()
g.model = Model(connection, cursor)
return g.model
def close_db(e=None):
db = g.pop("db", None)
model = g.pop("model", None)
if db is not None:
current_app.config['PSYCOPG2_CONNECTION_POOL'].putconn(db)
if model is not None:
model.cursor.close()
current_app.config['PSYCOPG2_CONNECTION_POOL'].putconn(model.connection)
def my_exec_info_message(exec_info):
return "{}: {}".format(".".join([exec_info[0].__module__, exec_info[0].__name__]), exec_info[1])