From 3a1188a779dbb9e63aaee48499fe16b9b29c5489 Mon Sep 17 00:00:00 2001 From: jokajak Date: Sun, 2 Aug 2015 14:01:02 -0400 Subject: [PATCH] Fix flask model to support sqlalchemy-migrate --- backend/flask/src/clipperz/models.py | 32 ++++-- backend/flask/src/db_create.py | 2 +- backend/flask/src/db_migrate.py | 2 +- .../db_repository/versions/001_migration.py | 97 +++++++++++++++++++ backend/flask/src/setup.py | 1 + 5 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 backend/flask/src/db_repository/versions/001_migration.py diff --git a/backend/flask/src/clipperz/models.py b/backend/flask/src/clipperz/models.py index 9737f37..8de66a1 100644 --- a/backend/flask/src/clipperz/models.py +++ b/backend/flask/src/clipperz/models.py @@ -59,9 +59,9 @@ class RecordVersion(db.Model): previous_version_key = db.Column(db.String()) previous_version_id = db.Column(db.Integer(), db.ForeignKey('record_version.id')) - creation_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) - update_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) - access_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) + creation_date = db.Column(db.DateTime()) + update_date = db.Column(db.DateTime()) + access_date = db.Column(db.DateTime()) record_id = db.Column(db.Integer(), db.ForeignKey('record.id'), @@ -72,6 +72,10 @@ class RecordVersion(db.Model): order_by=id, cascade='all,delete')) + def __init__(self): + """Initialize a record version.""" + self.creation_date = datetime.datetime.utcnow() + def update(self, someData): app.logger.debug(someData) recordVersionData = someData['currentRecordVersion'] @@ -93,15 +97,19 @@ class Record(db.Model): data = db.Column(db.Text()) api_version = db.Column(db.String()) version = db.Column(db.Integer(), default=0) - creation_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) - update_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) - access_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) + creation_date = db.Column(db.DateTime()) + update_date = db.Column(db.DateTime()) + access_date = db.Column(db.DateTime()) current_record_version = db.relationship( 'RecordVersion', uselist=False, cascade='save-update, merge, delete, delete-orphan') + def __init__(self): + """Initialize a record.""" + self.creation_date = datetime.datetime.utcnow() + def update(self, data, record_version): self.reference = data['reference'] self.data = data['data'] @@ -125,10 +133,14 @@ class OneTimePassword(db.Model): key_checksum = db.Column(db.String()) data = db.Column(db.Text()) version = db.Column(db.String()) - creation_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) + creation_date = db.Column(db.DateTime()) request_date = db.Column(db.DateTime()) usage_date = db.Column(db.DateTime()) + def __init__(self): + """Initialize a OneTimePassword.""" + self.creation_date = datetime.datetime.utcnow() + def update(self, someParameters, aStatus): self.reference = someParameters['reference'] self.key_value = someParameters['key'] @@ -148,4 +160,8 @@ class OneTimePassword(db.Model): class Session(db.Model): id = db.Column(db.Integer(), primary_key=True) sessionId = db.Column(db.String()) - access_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow) + access_date = db.Column(db.DateTime()) + + def __init__(self): + """Initialize a session.""" + self.access_date = datetime.datetime.utcnow() diff --git a/backend/flask/src/db_create.py b/backend/flask/src/db_create.py index cd6ce69..2d62abb 100644 --- a/backend/flask/src/db_create.py +++ b/backend/flask/src/db_create.py @@ -2,7 +2,7 @@ from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO -from app import db +from clipperz import db import os.path db.create_all() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): diff --git a/backend/flask/src/db_migrate.py b/backend/flask/src/db_migrate.py index 03f65d1..2eab93d 100644 --- a/backend/flask/src/db_migrate.py +++ b/backend/flask/src/db_migrate.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import imp from migrate.versioning import api -from app import db +from clipperz import db from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) diff --git a/backend/flask/src/db_repository/versions/001_migration.py b/backend/flask/src/db_repository/versions/001_migration.py new file mode 100644 index 0000000..53f50c9 --- /dev/null +++ b/backend/flask/src/db_repository/versions/001_migration.py @@ -0,0 +1,97 @@ +from sqlalchemy import * +from migrate import * + + +from migrate.changeset import schema +pre_meta = MetaData() +post_meta = MetaData() +one_time_password = Table('one_time_password', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('user_id', Integer), + Column('status', String), + Column('reference', String), + Column('key_value', String), + Column('key_checksum', String), + Column('data', Text), + Column('version', String), + Column('creation_date', DateTime), + Column('request_date', DateTime), + Column('usage_date', DateTime), +) + +record = Table('record', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('user_id', Integer), + Column('reference', String), + Column('data', Text), + Column('api_version', String), + Column('version', Integer, default=ColumnDefault(0)), + Column('creation_date', DateTime), + Column('update_date', DateTime), + Column('access_date', DateTime), +) + +record_version = Table('record_version', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('reference', String), + Column('header', Text), + Column('data', Text), + Column('api_version', String), + Column('version', Integer), + Column('previous_version_key', String), + Column('previous_version_id', Integer), + Column('creation_date', DateTime), + Column('update_date', DateTime), + Column('access_date', DateTime), + Column('record_id', Integer, nullable=False), +) + +session = Table('session', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('sessionId', String), + Column('access_date', DateTime), +) + +sessions = Table('sessions', post_meta, + Column('key', String(length=250), primary_key=True, nullable=False), + Column('value', LargeBinary, nullable=False), +) + +user = Table('user', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('username', String), + Column('srp_s', String(length=128)), + Column('srp_v', String(length=128)), + Column('header', Text), + Column('statistics', Text), + Column('auth_version', String), + Column('version', String), + Column('lock', String), + Column('offline_saved', Boolean, default=ColumnDefault(False)), + Column('update_date', DateTime), +) + + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; bind + # migrate_engine to your metadata + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine + post_meta.tables['one_time_password'].create() + post_meta.tables['record'].create() + post_meta.tables['record_version'].create() + post_meta.tables['session'].create() + post_meta.tables['sessions'].create() + post_meta.tables['user'].create() + + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine + post_meta.tables['one_time_password'].drop() + post_meta.tables['record'].drop() + post_meta.tables['record_version'].drop() + post_meta.tables['session'].drop() + post_meta.tables['sessions'].drop() + post_meta.tables['user'].drop() diff --git a/backend/flask/src/setup.py b/backend/flask/src/setup.py index affb5e5..f024a18 100644 --- a/backend/flask/src/setup.py +++ b/backend/flask/src/setup.py @@ -45,6 +45,7 @@ setup( install_requires=['Flask>=0.10.1', 'Flask-SQLAlchemy>=1.0', 'SQLAlchemy>=0.8.2', + 'SQLAlchemy-migrate', 'Flask-Login', 'Flask-KVSession', ],