-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create subscriptions table in discovery DB (#4254)
* Migration * Add indexes * Model * Add server default for timestamp in model * Linter * isort * black * Typo
- Loading branch information
1 parent
8c0b865
commit d42c197
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
discovery-provider/alembic/versions/03dbd1b775c5_create_subscriptions_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |