diff --git a/migrations/versions/253e9e2dae2d_init.py b/migrations/versions/253e9e2dae2d_init.py index 6316ff6..ab6fc9b 100644 --- a/migrations/versions/253e9e2dae2d_init.py +++ b/migrations/versions/253e9e2dae2d_init.py @@ -17,7 +17,6 @@ depends_on = None def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### op.create_table('users', sa.Column('display_name', sa.String(), nullable=True), sa.Column('id', sa.Integer(), nullable=False), @@ -41,12 +40,9 @@ def upgrade(): sa.ForeignKeyConstraint(['user_id'], ['users.id'], name=op.f('fk_sessions_user_id_users')), sa.PrimaryKeyConstraint('token', name=op.f('pk_sessions')) ) - # ### end Alembic commands ### def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### op.drop_table('sessions') op.drop_table('account') op.drop_table('users') - # ### end Alembic commands ### diff --git a/migrations/versions/da57b5eb3df7_add_timestamps.py b/migrations/versions/da57b5eb3df7_add_timestamps.py new file mode 100644 index 0000000..91b2f9c --- /dev/null +++ b/migrations/versions/da57b5eb3df7_add_timestamps.py @@ -0,0 +1,34 @@ +"""add timestamps + +Revision ID: da57b5eb3df7 +Revises: 253e9e2dae2d +Create Date: 2017-07-25 10:09:23.233340 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'da57b5eb3df7' +down_revision = '253e9e2dae2d' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('account', sa.Column('created_at', sa.DateTime(), nullable=False)) + op.add_column('account', sa.Column('updated_at', sa.DateTime(), nullable=False)) + op.add_column('sessions', sa.Column('created_at', sa.DateTime(), nullable=False)) + op.add_column('sessions', sa.Column('updated_at', sa.DateTime(), nullable=False)) + op.add_column('users', sa.Column('created_at', sa.DateTime(), nullable=False)) + op.add_column('users', sa.Column('updated_at', sa.DateTime(), nullable=False)) + + +def downgrade(): + op.drop_column('users', 'updated_at') + op.drop_column('users', 'created_at') + op.drop_column('sessions', 'updated_at') + op.drop_column('sessions', 'created_at') + op.drop_column('account', 'updated_at') + op.drop_column('account', 'created_at') diff --git a/model.py b/model.py index dab4f3e..05a4941 100644 --- a/model.py +++ b/model.py @@ -1,16 +1,18 @@ -import datetime +from datetime import datetime from app import db -model = db.Model +class TimestampMixin(object): + created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) -class User(db.Model): +class User(db.Model, TimestampMixin): __tablename__ = 'users' display_name = db.Column(db.String) id = db.Column(db.Integer, primary_key=True) -class Account(db.Model): +class Account(db.Model, TimestampMixin): user = db.relationship(User) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) @@ -26,7 +28,7 @@ class Account(db.Model): policy_keep_latest = db.Column(db.Integer) policy_delete_every = db.Column(db.Interval) -class Session(db.Model): +class Session(db.Model, TimestampMixin): __tablename__ = 'sessions' user = db.relationship(User)