Skip to content

Commit

Permalink
Create subscriptions table in discovery DB (#4254)
Browse files Browse the repository at this point in the history
* Migration

* Add indexes

* Model

* Add server default for timestamp in model

* Linter

* isort

* black

* Typo
  • Loading branch information
michellebrier committed Nov 2, 2022
1 parent 8c0b865 commit d42c197
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""create_subscriptions_table
Revision ID: 03dbd1b775c5
Revises: 42d5afb85d42
Create Date: 2022-11-02 03:16:19.526141
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '03dbd1b775c5'
down_revision = '42d5afb85d42'
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
"subscriptions",
sa.Column("blockhash", sa.String()),
sa.Column("blocknumber", sa.Integer(), index=True),
sa.Column("subscriber_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False, index=True),
sa.Column("is_current", sa.Boolean(), nullable=False),
sa.Column("is_delete", sa.Boolean(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()),
sa.Column("txhash", sa.String(), nullable=False, server_default=""),
sa.ForeignKeyConstraint(
["blockhash"],
["blocks.blockhash"],
),
sa.ForeignKeyConstraint(
["blocknumber"],
["blocks.number"],
),
sa.PrimaryKeyConstraint(
"subscriber_id", "user_id", "is_current", "txhash"
),
)


def downgrade():
op.drop_table("subscriptions")
31 changes: 31 additions & 0 deletions discovery-provider/src/models/social/subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, text
from sqlalchemy.orm import relationship
from src.models.base import Base
from src.models.model_utils import RepresentableMixin


class Subscription(Base, RepresentableMixin):
__tablename__ = "subscriptions"

blockhash = Column(ForeignKey("blocks.blockhash")) # type: ignore
blocknumber = Column(ForeignKey("blocks.number"), index=True) # type: ignore
subscriber_id = Column(Integer, primary_key=True, nullable=False)
user_id = Column(Integer, primary_key=True, nullable=False, index=True)
is_current = Column(Boolean, primary_key=True, nullable=False)
is_delete = Column(Boolean, nullable=False)
created_at = Column(
DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")
)
txhash = Column(
String,
primary_key=True,
nullable=False,
server_default=text("''::character varying"),
)

block = relationship( # type: ignore
"Block", primaryjoin="Subscription.blockhash == Block.blockhash"
)
block1 = relationship( # type: ignore
"Block", primaryjoin="Subscription.blocknumber == Block.number"
)

0 comments on commit d42c197

Please sign in to comment.