From 8d027695e2c942ab4a49fdac5da304cd77ca6c02 Mon Sep 17 00:00:00 2001 From: johdanike Date: Fri, 27 Mar 2026 06:03:27 +0100 Subject: [PATCH] feat(db): implement modular PostgreSQL schema for ChainBridge - Created modular SQLAlchemy models (HTLC, SwapOrder, CrossChainSwap, APIKey) - Implemented TimestampMixin for automatic audit logging - Configured Alembic for database migrations within Docker environment - Resolved Docker port conflicts and established venv migration pipeline - Generated and applied initial database schema Solves #21 --- backend/alembic.ini | 115 ++++++++++++++++++ backend/alembic/README | 1 + backend/alembic/env.py | 77 ++++++++++++ backend/alembic/script.py.mako | 26 ++++ ...d755d3af7afc_feat_initial_bridge_schema.py | 105 ++++++++++++++++ backend/app/models/base.py | 14 +-- backend/app/models/htlc.py | 5 +- backend/app/models/order.py | 28 ++--- docker-compose.dev.yml | 2 +- docker-compose.yml | 2 +- 10 files changed, 349 insertions(+), 26 deletions(-) create mode 100644 backend/alembic.ini create mode 100644 backend/alembic/README create mode 100644 backend/alembic/env.py create mode 100644 backend/alembic/script.py.mako create mode 100644 backend/alembic/versions/d755d3af7afc_feat_initial_bridge_schema.py diff --git a/backend/alembic.ini b/backend/alembic.ini new file mode 100644 index 0000000..1ec6d99 --- /dev/null +++ b/backend/alembic.ini @@ -0,0 +1,115 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# Format: postgresql://user:password@localhost:port/dbname +sqlalchemy.url = postgresql://chainbridge:chainbridge_dev@localhost:5433/chainbridge +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/backend/alembic/README b/backend/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/backend/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/backend/alembic/env.py b/backend/alembic/env.py new file mode 100644 index 0000000..164c1ea --- /dev/null +++ b/backend/alembic/env.py @@ -0,0 +1,77 @@ +import sys +from os.path import abspath, dirname +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context +from app.models import Base + +# --- ADD THESE 2 LINES TO FIX THE IMPORTS --- +sys.path.insert(0, dirname(dirname(abspath(__file__)))) +from app.models import Base + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# --- CHANGE THIS LINE TO LINK YOUR MODELS --- +target_metadata = Base.metadata + +# ... leave the rest of the file (the functions) exactly as they are ... + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/backend/alembic/script.py.mako b/backend/alembic/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/backend/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/backend/alembic/versions/d755d3af7afc_feat_initial_bridge_schema.py b/backend/alembic/versions/d755d3af7afc_feat_initial_bridge_schema.py new file mode 100644 index 0000000..4db17fd --- /dev/null +++ b/backend/alembic/versions/d755d3af7afc_feat_initial_bridge_schema.py @@ -0,0 +1,105 @@ +"""feat: initial bridge schema + +Revision ID: d755d3af7afc +Revises: +Create Date: 2026-03-27 06:01:00.988488 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'd755d3af7afc' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('api_keys', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('key', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('owner', sa.String(), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.Column('request_count', sa.Integer(), nullable=True), + sa.Column('last_used_at', sa.DateTime(timezone=True), nullable=True), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_api_keys_created_at'), 'api_keys', ['created_at'], unique=False) + op.create_index(op.f('ix_api_keys_key'), 'api_keys', ['key'], unique=True) + op.create_table('cross_chain_swaps', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('onchain_id', sa.String(), nullable=True), + sa.Column('stellar_htlc_id', sa.String(), nullable=True), + sa.Column('other_chain', sa.String(), nullable=False), + sa.Column('other_chain_tx', sa.String(), nullable=True), + sa.Column('stellar_party', sa.String(), nullable=False), + sa.Column('other_party', sa.String(), nullable=False), + sa.Column('state', sa.String(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_cross_chain_swaps_created_at'), 'cross_chain_swaps', ['created_at'], unique=False) + op.create_index(op.f('ix_cross_chain_swaps_onchain_id'), 'cross_chain_swaps', ['onchain_id'], unique=True) + op.create_table('htlcs', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('onchain_id', sa.String(), nullable=True), + sa.Column('sender', sa.String(), nullable=False), + sa.Column('receiver', sa.String(), nullable=False), + sa.Column('amount', sa.BigInteger(), nullable=False), + sa.Column('hash_lock', sa.String(), nullable=False), + sa.Column('time_lock', sa.BigInteger(), nullable=False), + sa.Column('status', sa.String(), nullable=False), + sa.Column('secret', sa.String(), nullable=True), + sa.Column('hash_algorithm', sa.String(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_htlcs_created_at'), 'htlcs', ['created_at'], unique=False) + op.create_index(op.f('ix_htlcs_onchain_id'), 'htlcs', ['onchain_id'], unique=True) + op.create_index(op.f('ix_htlcs_receiver'), 'htlcs', ['receiver'], unique=False) + op.create_index(op.f('ix_htlcs_sender'), 'htlcs', ['sender'], unique=False) + op.create_table('swap_orders', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('user_address', sa.String(), nullable=False), + sa.Column('source_chain', sa.String(), nullable=False), + sa.Column('target_chain', sa.String(), nullable=False), + sa.Column('source_asset', sa.String(), nullable=False), + sa.Column('target_asset', sa.String(), nullable=False), + sa.Column('amount', sa.BigInteger(), nullable=False), + sa.Column('status', sa.Enum('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED', name='orderstatus'), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_swap_orders_created_at'), 'swap_orders', ['created_at'], unique=False) + op.create_index(op.f('ix_swap_orders_user_address'), 'swap_orders', ['user_address'], unique=False) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_swap_orders_user_address'), table_name='swap_orders') + op.drop_index(op.f('ix_swap_orders_created_at'), table_name='swap_orders') + op.drop_table('swap_orders') + op.drop_index(op.f('ix_htlcs_sender'), table_name='htlcs') + op.drop_index(op.f('ix_htlcs_receiver'), table_name='htlcs') + op.drop_index(op.f('ix_htlcs_onchain_id'), table_name='htlcs') + op.drop_index(op.f('ix_htlcs_created_at'), table_name='htlcs') + op.drop_table('htlcs') + op.drop_index(op.f('ix_cross_chain_swaps_onchain_id'), table_name='cross_chain_swaps') + op.drop_index(op.f('ix_cross_chain_swaps_created_at'), table_name='cross_chain_swaps') + op.drop_table('cross_chain_swaps') + op.drop_index(op.f('ix_api_keys_key'), table_name='api_keys') + op.drop_index(op.f('ix_api_keys_created_at'), table_name='api_keys') + op.drop_table('api_keys') + # ### end Alembic commands ### diff --git a/backend/app/models/base.py b/backend/app/models/base.py index 873e543..f11959c 100644 --- a/backend/app/models/base.py +++ b/backend/app/models/base.py @@ -1,11 +1,11 @@ -from sqlalchemy.orm import DeclarativeBase +import uuid +from sqlalchemy.orm import declarative_base # Changed this line from sqlalchemy import Column, DateTime, func +from sqlalchemy.dialects.postgresql import UUID - -class Base(DeclarativeBase): - pass - +# Use the function call to create the Base +Base = declarative_base() class TimestampMixin: - created_at = Column(DateTime(timezone=True), server_default=func.now()) - updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) + created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True) + updated_at = Column(DateTime(timezone=True), onupdate=func.now()) \ No newline at end of file diff --git a/backend/app/models/htlc.py b/backend/app/models/htlc.py index b3bcd5e..e31beb9 100644 --- a/backend/app/models/htlc.py +++ b/backend/app/models/htlc.py @@ -1,9 +1,8 @@ import uuid -from sqlalchemy import Column, String, BigInteger, DateTime, Enum as SQLEnum +from sqlalchemy import Column, String, BigInteger from sqlalchemy.dialects.postgresql import UUID from .base import Base, TimestampMixin - class HTLC(Base, TimestampMixin): __tablename__ = "htlcs" @@ -16,4 +15,4 @@ class HTLC(Base, TimestampMixin): time_lock = Column(BigInteger, nullable=False) status = Column(String, nullable=False, default="active") secret = Column(String, nullable=True) - hash_algorithm = Column(String, nullable=False, default="sha256") + hash_algorithm = Column(String, nullable=False, default="sha256") \ No newline at end of file diff --git a/backend/app/models/order.py b/backend/app/models/order.py index c5dab4e..87f028c 100644 --- a/backend/app/models/order.py +++ b/backend/app/models/order.py @@ -1,23 +1,23 @@ import uuid -from sqlalchemy import Column, String, BigInteger, Integer +from enum import Enum as PyEnum +from sqlalchemy import Column, String, BigInteger, Enum as SQLEnum, ForeignKey from sqlalchemy.dialects.postgresql import UUID from .base import Base, TimestampMixin +class OrderStatus(str, PyEnum): + PENDING = "pending" + PROCESSING = "processing" + COMPLETED = "completed" + FAILED = "failed" class SwapOrder(Base, TimestampMixin): __tablename__ = "swap_orders" id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) - onchain_id = Column(Integer, unique=True, index=True) - creator = Column(String, nullable=False, index=True) - from_chain = Column(String, nullable=False) - to_chain = Column(String, nullable=False) - from_asset = Column(String, nullable=False) - to_asset = Column(String, nullable=False) - from_amount = Column(BigInteger, nullable=False) - to_amount = Column(BigInteger, nullable=False) - min_fill_amount = Column(BigInteger, nullable=True) - filled_amount = Column(BigInteger, default=0) - expiry = Column(BigInteger, nullable=False) - status = Column(String, nullable=False, default="open") - counterparty = Column(String, nullable=True) + user_address = Column(String, nullable=False, index=True) + source_chain = Column(String, nullable=False) + target_chain = Column(String, nullable=False) + source_asset = Column(String, nullable=False) + target_asset = Column(String, nullable=False) + amount = Column(BigInteger, nullable=False) + status = Column(SQLEnum(OrderStatus), nullable=False, default=OrderStatus.PENDING) \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index f01c6d1..d0f2411 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -9,7 +9,7 @@ services: POSTGRES_PASSWORD: chainbridge_dev POSTGRES_DB: chainbridge ports: - - "5432:5432" + - "5433:5432" volumes: - postgres_dev_data:/var/lib/postgresql/data - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql:ro diff --git a/docker-compose.yml b/docker-compose.yml index e919b2e..6b2ec08 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-chainbridge_dev} POSTGRES_DB: ${POSTGRES_DB:-chainbridge} ports: - - "${POSTGRES_PORT:-5432}:5432" + - "${POSTGRES_PORT:-5433}:5432" volumes: - postgres_data:/var/lib/postgresql/data - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql:ro