Add transction for transfor label table.

pull/313/head
Andrey Dolgolev 2021-10-12 17:04:11 +03:00
rodzic 63b2f68f29
commit 4a0d9682aa
2 zmienionych plików z 120 dodań i 0 usunięć

Wyświetl plik

@ -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 ###

Wyświetl plik

@ -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
)