From 4a0d9682aaaa242fde7115ab9fd8a4588c02b0d0 Mon Sep 17 00:00:00 2001 From: Andrey Dolgolev Date: Tue, 12 Oct 2021 17:04:11 +0300 Subject: [PATCH] Add transction for transfor label table. --- ...f_add_log_index_block_number_timestamp_.py | 117 ++++++++++++++++++ db/moonstreamdb/models.py | 3 + 2 files changed, 120 insertions(+) create mode 100644 db/alembic/versions/f1e8cf50a3ff_add_log_index_block_number_timestamp_.py diff --git a/db/alembic/versions/f1e8cf50a3ff_add_log_index_block_number_timestamp_.py b/db/alembic/versions/f1e8cf50a3ff_add_log_index_block_number_timestamp_.py new file mode 100644 index 00000000..6e86461e --- /dev/null +++ b/db/alembic/versions/f1e8cf50a3ff_add_log_index_block_number_timestamp_.py @@ -0,0 +1,117 @@ +"""Add log_index, block_number , timestamp remove address_id and add address just as text field + +Revision ID: f1e8cf50a3ff +Revises: d6ed4b1b43d5 +Create Date: 2021-10-12 15:35:49.477610 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "f1e8cf50a3ff" +down_revision = "d6ed4b1b43d5" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "ethereum_labels", sa.Column("address", sa.VARCHAR(length=256), nullable=True) + ) + op.add_column( + "ethereum_labels", sa.Column("block_number", sa.BigInteger(), nullable=True) + ) + op.add_column( + "ethereum_labels", + sa.Column("log_index", postgresql.JSONB(astext_type=sa.Text()), nullable=True), + ) + op.add_column( + "ethereum_labels", + sa.Column("transaction_timestamp", sa.BigInteger(), nullable=True), + ) + op.create_index( + op.f("ix_ethereum_labels_address"), "ethereum_labels", ["address"], unique=False + ) + op.create_index( + op.f("ix_ethereum_labels_block_number"), + "ethereum_labels", + ["block_number"], + unique=False, + ) + op.create_index( + op.f("ix_ethereum_labels_transaction_timestamp"), + "ethereum_labels", + ["transaction_timestamp"], + unique=False, + ) + op.execute( + """INSERT INTO ethereum_labels (address) + SELECT con.address as address + FROM ( + select a.address from ethereum_labels as a + left join ethereum_addresses as b + ON a.address_id = b.id + ) as con + """ + ) + op.execute( + """ UPDATE labels + SET + labels.address = address.address + FROM ethereum_labels as labels + left join ethereum_addresses address + ON labels.address_id = address.id + """ + ) + op.execute( + """ UPDATE labels + SET + labels.block_number = blocks.block_number + labels.timestamp = blocks.timestamp + FROM ethereum_labels as labels + left join ethereum_transactions as transactions + ON transactions.hash = labels.transaction_hash + left join ethereum_blocks as blocks + ON blocks.block_number = transactions.block_number; + """ + ) + op.drop_index("ix_ethereum_labels_address_id", table_name="ethereum_labels") + op.drop_constraint( + "fk_ethereum_labels_address_id_ethereum_addresses", + "ethereum_labels", + type_="foreignkey", + ) + op.drop_column("ethereum_labels", "address_id") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "ethereum_labels", + sa.Column("address_id", sa.INTEGER(), autoincrement=False, nullable=True), + ) + op.create_foreign_key( + "fk_ethereum_labels_address_id_ethereum_addresses", + "ethereum_labels", + "ethereum_addresses", + ["address_id"], + ["id"], + ondelete="CASCADE", + ) + op.create_index( + "ix_ethereum_labels_address_id", "ethereum_labels", ["address_id"], unique=False + ) + op.drop_index( + op.f("ix_ethereum_labels_transaction_timestamp"), table_name="ethereum_labels" + ) + op.drop_index(op.f("ix_ethereum_labels_block_number"), table_name="ethereum_labels") + op.drop_index(op.f("ix_ethereum_labels_address"), table_name="ethereum_labels") + op.drop_column("ethereum_labels", "transaction_timestamp") + op.drop_column("ethereum_labels", "log_index") + op.drop_column("ethereum_labels", "block_number") + op.drop_column("ethereum_labels", "address") + # ### end Alembic commands ### diff --git a/db/moonstreamdb/models.py b/db/moonstreamdb/models.py index a3fada13..c7f8f5ab 100644 --- a/db/moonstreamdb/models.py +++ b/db/moonstreamdb/models.py @@ -15,6 +15,7 @@ from sqlalchemy import ( from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.sql import expression from sqlalchemy.ext.compiler import compiles +from sqlalchemy.sql.sqltypes import TIMESTAMP """ Naming conventions doc @@ -160,6 +161,8 @@ class EthereumLabel(Base): # type: ignore index=True, ) label_data = Column(JSONB, nullable=True) + transaction_timestamp = Column(BigInteger, index=True) + log_index = Column(JSONB, nullable=True) created_at = Column( DateTime(timezone=True), server_default=utcnow(), nullable=False )