diff --git a/db/alembic/versions/42ca451a9c33_fix_indexes.py b/db/alembic/versions/42ca451a9c33_fix_indexes.py new file mode 100644 index 00000000..54968514 --- /dev/null +++ b/db/alembic/versions/42ca451a9c33_fix_indexes.py @@ -0,0 +1,51 @@ +"""fix indexes + +Revision ID: 42ca451a9c33 +Revises: 2fa541f6f6fc +Create Date: 2022-06-07 13:30:40.746341 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "42ca451a9c33" +down_revision = "2fa541f6f6fc" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index( + "ix_polygon_labels_address_label_label_data_type_and_name", + table_name="polygon_labels", + ) + op.create_index( + "ix_polygon_labels_address_block_number", + "polygon_labels", + ["address", "block_number"], + unique=False, + ) + op.create_index( + "ix_polygon_labels_address_block_timestamp", + "polygon_labels", + ["address", "block_timestamp"], + unique=False, + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.execute( + """ + CREATE INDEX ix_polygon_labels_address_label_label_data_type_and_name ON polygon_labels USING BTREE (address,label,(label_data->>'type'),(label_data->>'name')); + """ + ) + op.drop_index( + "ix_polygon_labels_address_block_timestamp", table_name="polygon_labels" + ) + op.drop_index("ix_polygon_labels_address_block_number", table_name="polygon_labels") + # ### end Alembic commands ### diff --git a/db/moonstreamdb/models.py b/db/moonstreamdb/models.py index 79aee8ab..3cd1c538 100644 --- a/db/moonstreamdb/models.py +++ b/db/moonstreamdb/models.py @@ -5,6 +5,7 @@ from sqlalchemy import ( BigInteger, Column, DateTime, + Index, Integer, ForeignKey, MetaData, @@ -132,21 +133,9 @@ class EthereumLabel(Base): # type: ignore nullable=False, ) label = Column(VARCHAR(256), nullable=False, index=True) - block_number = Column( - BigInteger, - nullable=True, - index=True, - ) - address = Column( - VARCHAR(256), - nullable=True, - index=True, - ) - transaction_hash = Column( - VARCHAR(256), - nullable=True, - index=True, - ) + block_number = Column(BigInteger, nullable=True, index=True,) + address = Column(VARCHAR(256), nullable=True, index=True,) + transaction_hash = Column(VARCHAR(256), nullable=True, index=True,) label_data = Column(JSONB, nullable=True) block_timestamp = Column(BigInteger, index=True) log_index = Column(Integer, nullable=True) @@ -230,6 +219,21 @@ class PolygonLabel(Base): # type: ignore __tablename__ = "polygon_labels" + __table_args__ = ( + Index( + "ix_polygon_labels_address_block_number", + "address", + "block_number", + unique=False, + ), + Index( + "ix_polygon_labels_address_block_timestamp", + "address", + "block_timestamp", + unique=False, + ), + ) + id = Column( UUID(as_uuid=True), primary_key=True, @@ -238,21 +242,9 @@ class PolygonLabel(Base): # type: ignore nullable=False, ) label = Column(VARCHAR(256), nullable=False, index=True) - block_number = Column( - BigInteger, - nullable=True, - index=True, - ) - address = Column( - VARCHAR(256), - nullable=True, - index=True, - ) - transaction_hash = Column( - VARCHAR(256), - nullable=True, - index=True, - ) + block_number = Column(BigInteger, nullable=True, index=True,) + address = Column(VARCHAR(256), nullable=True, index=True,) + transaction_hash = Column(VARCHAR(256), nullable=True, index=True,) label_data = Column(JSONB, nullable=True) block_timestamp = Column(BigInteger, index=True) log_index = Column(Integer, nullable=True) @@ -260,15 +252,6 @@ class PolygonLabel(Base): # type: ignore DateTime(timezone=True), server_default=utcnow(), nullable=False ) - # Undescribed indexes - """ - Migration: alembic\versions\5f5b8f19570f_added_index_for_address_type_and_name_.py - - Index: "ix_polygon_labels_address_label_label_data_type_and_name" created manually. - By fields: (address, label, (label_data->>'type'), (label_data->>'name')) - Reason: https://github.com/sqlalchemy/alembic/issues/469#issuecomment-441887478 - """ - class XDaiBlock(Base): # type: ignore __tablename__ = "xdai_blocks" @@ -357,21 +340,9 @@ class XDaiLabel(Base): # type: ignore nullable=False, ) label = Column(VARCHAR(256), nullable=False, index=True) - block_number = Column( - BigInteger, - nullable=True, - index=True, - ) - address = Column( - VARCHAR(256), - nullable=True, - index=True, - ) - transaction_hash = Column( - VARCHAR(256), - nullable=True, - index=True, - ) + block_number = Column(BigInteger, nullable=True, index=True,) + address = Column(VARCHAR(256), nullable=True, index=True,) + transaction_hash = Column(VARCHAR(256), nullable=True, index=True,) label_data = Column(JSONB, nullable=True) block_timestamp = Column(BigInteger, index=True) log_index = Column(Integer, nullable=True)