Skip to content

Instantly share code, notes, and snippets.

@aleiphoenix aleiphoenix/db.py
Created Apr 1, 2015

Embed
What would you like to do?
SQLAlchemy: change table schema, on the fly
# -*- coding: utf-8; -*-
from tigger import app
app.ready()
from tigger import db
def decorate_timestamps():
from datetime import datetime
from sqlalchemy import TIMESTAMP, Column, text
default_create_time_col = \
Column('create_time', TIMESTAMP,
nullable=False, default=datetime.now,
server_default='0000-00-00 00:00:00')
default_update_time_col = \
Column('update_time', TIMESTAMP,
nullable=False, default=datetime.now,
server_default=text('CURRENT_TIMESTAMP on '\
'update CURRENT_TIMESTAMP'))
tables = dict(db.metadata.tables)
for i in tables:
table = tables[i]
for col in table.columns:
if 'update_time' == col.name:
table.columns.replace(default_update_time_col)
if 'create_time' == col.name:
table.columns.replace(default_create_time_col)
with app.app_context(), db.use_bind('master'):
from tigger import models
db.drop_all()
if 'mysql' in db.engine.url.drivername.lower():
pass
decorate_timestamps()
db.create_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.