From 5761d228222c43314e6ac81a258c820f53e2b0f4 Mon Sep 17 00:00:00 2001 From: venusbb Date: Tue, 28 Nov 2017 15:25:15 +0000 Subject: [PATCH] Migration to create new database table service_callback_api --- app/models.py | 34 ++++++++++++ ...o.py => 0145_add_notification_reply_to.py} | 0 .../versions/0146_add_service_callback_api.py | 55 +++++++++++++++++++ 3 files changed, 89 insertions(+) rename migrations/versions/{0144_add_notification_reply_to.py => 0145_add_notification_reply_to.py} (100%) create mode 100644 migrations/versions/0146_add_service_callback_api.py diff --git a/app/models.py b/app/models.py index a95c09a6f..fed090c24 100644 --- a/app/models.py +++ b/app/models.py @@ -466,6 +466,40 @@ class ServiceInboundApi(db.Model, Versioned): } +class ServiceCallbackApi(db.Model, Versioned): + __tablename__ = 'service_callback_api' + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False, unique=True) + service = db.relationship('Service', backref='service_callback_api') + url = db.Column(db.String(), nullable=False) + _bearer_token = db.Column("bearer_token", db.String(), nullable=False) + created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) + updated_at = db.Column(db.DateTime, nullable=True) + updated_by = db.relationship('User') + updated_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) + + @property + def bearer_token(self): + if self._bearer_token: + return encryption.decrypt(self._bearer_token) + return None + + @bearer_token.setter + def bearer_token(self, bearer_token): + if bearer_token: + self._bearer_token = encryption.encrypt(str(bearer_token)) + + def serialize(self): + return { + "id": str(self.id), + "service_id": str(self.service_id), + "url": self.url, + "updated_by_id": str(self.updated_by_id), + "created_at": self.created_at.strftime(DATETIME_FORMAT), + "updated_at": self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None + } + + class ApiKey(db.Model, Versioned): __tablename__ = 'api_keys' diff --git a/migrations/versions/0144_add_notification_reply_to.py b/migrations/versions/0145_add_notification_reply_to.py similarity index 100% rename from migrations/versions/0144_add_notification_reply_to.py rename to migrations/versions/0145_add_notification_reply_to.py diff --git a/migrations/versions/0146_add_service_callback_api.py b/migrations/versions/0146_add_service_callback_api.py new file mode 100644 index 000000000..07368d3f8 --- /dev/null +++ b/migrations/versions/0146_add_service_callback_api.py @@ -0,0 +1,55 @@ +""" + +Revision ID: 0146_add_service_callback_api +Revises: 0145_add_notification_reply_to +Create Date: 2017-11-28 15:13:48.730554 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0146_add_service_callback_api' +down_revision = '0145_add_notification_reply_to' + + +def upgrade(): + op.create_table('service_callback_api_history', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('url', sa.String(), nullable=False), + sa.Column('bearer_token', sa.String(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('updated_by_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('version', sa.Integer(), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('id', 'version') + ) + op.create_index(op.f('ix_service_callback_api_history_service_id'), 'service_callback_api_history', + ['service_id'], unique=False) + op.create_index(op.f('ix_service_callback_api_history_updated_by_id'), 'service_callback_api_history', + ['updated_by_id'], unique=False) + op.create_table('service_callback_api', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('url', sa.String(), nullable=False), + sa.Column('bearer_token', sa.String(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('updated_by_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('version', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['updated_by_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_service_callback_api_service_id'), 'service_callback_api', ['service_id'], unique=True) + op.create_index(op.f('ix_service_callback_api_updated_by_id'), 'service_callback_api', ['updated_by_id'], unique=False) + + +def downgrade(): + op.drop_index(op.f('ix_service_callback_api_updated_by_id'), table_name='service_callback_api') + op.drop_index(op.f('ix_service_callback_api_service_id'), table_name='service_callback_api') + op.drop_table('service_callback_api') + op.drop_index(op.f('ix_service_callback_api_history_updated_by_id'), table_name='service_callback_api_history') + op.drop_index(op.f('ix_service_callback_api_history_service_id'), table_name='service_callback_api_history') + op.drop_table('service_callback_api_history')