Fix flask model to support sqlalchemy-migrate

This commit is contained in:
jokajak 2015-08-02 14:01:02 -04:00
parent 35960ad2df
commit 3a1188a779
5 changed files with 124 additions and 10 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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)

View File

@ -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()

View File

@ -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',
],