Skip to content

Commit 91846b2

Browse files
committed
REF: used ruff to reformat code
1 parent c12448c commit 91846b2

20 files changed

Lines changed: 1473 additions & 528 deletions

alembic/env.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
"""Alembic migration environment configuration file."""
12

23
from __future__ import annotations
34
from logging.config import fileConfig
4-
from sqlalchemy import engine_from_config, pool
5-
from alembic import context
65
from pathlib import Path
76
import os
87

9-
# make sure we import local repo version, not installed package
8+
from sqlalchemy import engine_from_config, pool
9+
from alembic import context
10+
11+
from bionexus.db import models as bnx_models
12+
13+
# Make sure we import local repo version, not installed package
1014
REPO_ROOT = Path(__file__).resolve().parents[1]
1115
if str(REPO_ROOT) not in os.sys.path:
1216
os.sys.path.insert(0, str(REPO_ROOT))
1317

14-
# load .env from repo root
18+
# Load .env from repo root
1519
try:
1620
from dotenv import load_dotenv
21+
1722
load_dotenv(Path(__file__).resolve().parents[1] / ".env")
1823
except Exception:
1924
pass
@@ -22,17 +27,27 @@
2227
if config.config_file_name is not None:
2328
fileConfig(config.config_file_name)
2429

25-
# import module that defines base and all models
26-
from bionexus.db import models as bnx_models
30+
# Import module that defines base and all models
2731
target_metadata = bnx_models.Base.metadata
2832

29-
def get_url():
33+
34+
def get_url() -> str:
35+
"""
36+
Get the database URL from environment variable.
37+
38+
:return: database URL string
39+
:raises RuntimeError: if BIONEXUS_DB_URL is not set
40+
"""
3041
url = os.getenv("BIONEXUS_DB_URL")
3142
if not url:
3243
raise RuntimeError("BIONEXUS_DB_URL environment variable not set")
3344
return url
3445

35-
def run_migrations_offline():
46+
47+
def run_migrations_offline() -> None:
48+
"""
49+
Run migrations in 'offline' mode.
50+
"""
3651
url = get_url()
3752
context.configure(
3853
url=url,
@@ -45,23 +60,25 @@ def run_migrations_offline():
4560
with context.begin_transaction():
4661
context.run_migrations()
4762

48-
def run_migrations_online():
63+
64+
def run_migrations_online() -> None:
65+
"""
66+
Run migrations in 'online' mode.
67+
"""
4968
connectable = engine_from_config(
50-
{"sqlalchemy.url": get_url()},
51-
prefix="sqlalchemy.",
52-
poolclass=pool.NullPool
69+
{"sqlalchemy.url": get_url()}, prefix="sqlalchemy.", poolclass=pool.NullPool
5370
)
5471
with connectable.connect() as connection:
5572
context.configure(
5673
connection=connection,
5774
target_metadata=target_metadata,
5875
compare_type=True,
5976
compare_server_default=True,
60-
# include_schemas=True,
6177
)
6278
with context.begin_transaction():
6379
context.run_migrations()
6480

81+
6582
if context.is_offline_mode():
6683
run_migrations_offline()
6784
else:

alembic/versions/0001_init.py

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
branch_labels = None
99
depends_on = None
1010

11+
1112
def upgrade():
12-
op.execute("CREATE EXTENSION IF NOT EXISTS vector;") # pgvector for fp_morgan_b2048_r2_vec
13+
op.execute(
14+
"CREATE EXTENSION IF NOT EXISTS vector;"
15+
) # pgvector for fp_morgan_b2048_r2_vec
1316

1417
op.create_table(
1518
"compound",
1619
sa.Column("id", sa.BigInteger, primary_key=True),
17-
1820
sa.Column("inchikey", sa.String(27), nullable=True),
1921
sa.Column("inchi", sa.Text, nullable=True),
2022
sa.Column("smiles", sa.Text, nullable=True),
21-
2223
sa.Column("mol_formula", sa.String(64), nullable=True),
2324
sa.Column("mol_weight", sa.Float, nullable=True),
2425
sa.Column("exact_mass", sa.Float, nullable=True),
2526
sa.Column("m_plus_h", sa.Float, nullable=True),
2627
sa.Column("m_plus_na", sa.Float, nullable=True),
27-
2828
sa.Column("c_count", sa.Integer, nullable=True),
2929
sa.Column("h_count", sa.Integer, nullable=True),
3030
sa.Column("n_count", sa.Integer, nullable=True),
@@ -35,36 +35,68 @@ def upgrade():
3535
sa.Column("cl_count", sa.Integer, nullable=True),
3636
sa.Column("br_count", sa.Integer, nullable=True),
3737
sa.Column("i_count", sa.Integer, nullable=True),
38-
3938
sa.Column("fp_morgan_b2048_r2_bit", BIT(2048), nullable=True),
4039
sa.Column("fp_morgan_b2048_r2_pop", sa.SmallInteger, nullable=True),
4140
sa.Column("fp_morgan_b2048_r2_vec", Vector(2048), nullable=True),
42-
43-
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
44-
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
41+
sa.Column(
42+
"created_at",
43+
sa.DateTime(timezone=True),
44+
server_default=sa.text("NOW()"),
45+
nullable=False,
46+
),
47+
sa.Column(
48+
"updated_at",
49+
sa.DateTime(timezone=True),
50+
server_default=sa.text("NOW()"),
51+
nullable=False,
52+
),
4553
)
4654

4755
op.create_unique_constraint("uq_compound_inchikey", "compound", ["inchikey"])
48-
56+
4957
op.create_table(
5058
"compound_record",
5159
sa.Column("id", sa.BigInteger, primary_key=True),
52-
sa.Column("compound_id", sa.BigInteger, sa.ForeignKey("compound.id", ondelete="CASCADE"), nullable=False),
53-
60+
sa.Column(
61+
"compound_id",
62+
sa.BigInteger,
63+
sa.ForeignKey("compound.id", ondelete="CASCADE"),
64+
nullable=False,
65+
),
5466
sa.Column("source", sa.String(32), nullable=False),
5567
sa.Column("ext_id", sa.String(128), nullable=False),
5668
sa.Column("name", sa.String(512), nullable=True),
5769
sa.Column("synonyms", ARRAY(sa.String), nullable=True),
58-
59-
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
60-
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
70+
sa.Column(
71+
"created_at",
72+
sa.DateTime(timezone=True),
73+
server_default=sa.text("NOW()"),
74+
nullable=False,
75+
),
76+
sa.Column(
77+
"updated_at",
78+
sa.DateTime(timezone=True),
79+
server_default=sa.text("NOW()"),
80+
nullable=False,
81+
),
6182
)
6283

6384
# allow same (source, ext_id) to link to multiple compounds
64-
op.create_unique_constraint("uq_compound_record_compound_source_ext", "compound_record", ["compound_id", "source", "ext_id"])
65-
op.create_index("ix_compound_record_compound_id", "compound_record", ["compound_id"])
85+
op.create_unique_constraint(
86+
"uq_compound_record_compound_source_ext",
87+
"compound_record",
88+
["compound_id", "source", "ext_id"],
89+
)
90+
op.create_index(
91+
"ix_compound_record_compound_id", "compound_record", ["compound_id"]
92+
)
6693
# speed lookups by accession
67-
op.create_index("ix_compound_record_source_ext", "compound_record", ["source", "ext_id"], unique=False)
94+
op.create_index(
95+
"ix_compound_record_source_ext",
96+
"compound_record",
97+
["source", "ext_id"],
98+
unique=False,
99+
)
68100

69101
# compound
70102
op.execute("""
@@ -100,17 +132,22 @@ def upgrade():
100132
EXECUTE FUNCTION public.set_timestamp_compound_record();
101133
""")
102134

135+
103136
def downgrade():
104137
# drop in reverse order
105138
# drop triggers/functions first (otherwise DROP TABLE will drop dependent objs, but be explicit)
106-
op.execute("DROP TRIGGER IF EXISTS compound_record_set_timestamp ON public.compound_record;")
139+
op.execute(
140+
"DROP TRIGGER IF EXISTS compound_record_set_timestamp ON public.compound_record;"
141+
)
107142
op.execute("DROP FUNCTION IF EXISTS public.set_timestamp_compound_record;")
108143
op.execute("DROP TRIGGER IF EXISTS compound_set_timestamp ON public.compound;")
109144
op.execute("DROP FUNCTION IF EXISTS public.set_timestamp_compound;")
110145

111146
op.drop_index("ix_compound_record_source_ext", table_name="compound_record")
112147
op.drop_index("ix_compound_record_compound_id", table_name="compound_record")
113-
op.drop_constraint("uq_compound_record_compound_source_ext", "compound_record", type_="unique")
148+
op.drop_constraint(
149+
"uq_compound_record_compound_source_ext", "compound_record", type_="unique"
150+
)
114151
op.drop_table("compound_record")
115152

116153
op.drop_constraint("uq_compound_inchikey", "compound", type_="unique")

alembic/versions/0002_rev.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Revises: 0001_init
44
Create Date: 2025-10-10 21:56:15.797293
55
"""
6+
67
from alembic import op
78
import sqlalchemy as sa
89

@@ -11,24 +12,38 @@
1112
branch_labels = None
1213
depends_on = None
1314

15+
1416
def upgrade():
1517
op.create_table(
1618
"genbank_region",
1719
sa.Column("id", sa.BigInteger, primary_key=True),
18-
1920
sa.Column("source", sa.String(32), nullable=False),
2021
sa.Column("ext_id", sa.String(128), nullable=False),
21-
22-
sa.Column("gbk_text", sa.Text, nullable=False), # full GenBank as TEXT
23-
sa.Column("size_bytes", sa.Integer, nullable=True), # raw size on ingest
24-
sa.Column("sha256", sa.String(64), nullable=True), # content checksum for dedup/integrity
25-
26-
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
27-
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
22+
sa.Column("gbk_text", sa.Text, nullable=False), # full GenBank as TEXT
23+
sa.Column("size_bytes", sa.Integer, nullable=True), # raw size on ingest
24+
sa.Column(
25+
"sha256", sa.String(64), nullable=True
26+
), # content checksum for dedup/integrity
27+
sa.Column(
28+
"created_at",
29+
sa.DateTime(timezone=True),
30+
server_default=sa.text("NOW()"),
31+
nullable=False,
32+
),
33+
sa.Column(
34+
"updated_at",
35+
sa.DateTime(timezone=True),
36+
server_default=sa.text("NOW()"),
37+
nullable=False,
38+
),
2839
)
2940

3041
# uniqueness on (source, ext_id) — this also creates the btree index you need
31-
op.create_unique_constraint("uq_genbank_region_source_ext", "genbank_region", ["source", "ext_id"],)
42+
op.create_unique_constraint(
43+
"uq_genbank_region_source_ext",
44+
"genbank_region",
45+
["source", "ext_id"],
46+
)
3247

3348
# fast de-dup by content when sha256 is present (partial unique index)
3449
op.execute("""
@@ -54,9 +69,12 @@ def upgrade():
5469
EXECUTE FUNCTION public.set_timestamp_genbank_region();
5570
""")
5671

72+
5773
def downgrade():
5874
# drop trigger and its function first
59-
op.execute("DROP TRIGGER IF EXISTS genbank_region_set_timestamp ON public.genbank_region;")
75+
op.execute(
76+
"DROP TRIGGER IF EXISTS genbank_region_set_timestamp ON public.genbank_region;"
77+
)
6078
op.execute("DROP FUNCTION IF EXISTS public.set_timestamp_genbank_region;")
6179

6280
# drop optional partial unique index (if created)

alembic/versions/0003_rev.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Revises: 0002_rev
44
Create Date: 2025-10-11 10:32:00.000000
55
"""
6+
67
from alembic import op
78
import sqlalchemy as sa
89
from sqlalchemy.dialects.postgresql import JSONB
@@ -12,15 +13,24 @@
1213
branch_labels = None
1314
depends_on = None
1415

16+
1517
def upgrade():
1618
op.create_table(
1719
"annotation",
1820
sa.Column("id", sa.BigInteger, primary_key=True),
19-
2021
# polymorphic target: either compound OR genbank_region (exactly one non-null)
21-
sa.Column("compound_id", sa.BigInteger, sa.ForeignKey("compound.id", ondelete="CASCADE"), nullable=True),
22-
sa.Column("genbank_region_id", sa.BigInteger, sa.ForeignKey("genbank_region.id", ondelete="CASCADE"), nullable=True),
23-
22+
sa.Column(
23+
"compound_id",
24+
sa.BigInteger,
25+
sa.ForeignKey("compound.id", ondelete="CASCADE"),
26+
nullable=True,
27+
),
28+
sa.Column(
29+
"genbank_region_id",
30+
sa.BigInteger,
31+
sa.ForeignKey("genbank_region.id", ondelete="CASCADE"),
32+
nullable=True,
33+
),
2434
# flexible classification fields
2535
# examples:
2636
# scheme: "taxonomy", key: "species", value: "Streptomyces coelicolor"
@@ -30,12 +40,20 @@ def upgrade():
3040
sa.Column("scheme", sa.String(64), nullable=False),
3141
sa.Column("key", sa.String(64), nullable=False),
3242
sa.Column("value", sa.String(256), nullable=False),
33-
3443
# structured payload (for provenance, scores, ontology IDs, etc.)
3544
sa.Column("metadata_json", JSONB, nullable=True),
36-
37-
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
38-
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
45+
sa.Column(
46+
"created_at",
47+
sa.DateTime(timezone=True),
48+
server_default=sa.text("NOW()"),
49+
nullable=False,
50+
),
51+
sa.Column(
52+
"updated_at",
53+
sa.DateTime(timezone=True),
54+
server_default=sa.text("NOW()"),
55+
nullable=False,
56+
),
3957
sa.CheckConstraint(
4058
"("
4159
"(compound_id IS NOT NULL AND genbank_region_id IS NULL) OR "
@@ -54,8 +72,12 @@ def upgrade():
5472

5573
# helpful indexes for common lookups
5674
op.create_index("ix_annotation_compound_id", "annotation", ["compound_id"])
57-
op.create_index("ix_annotation_genbank_region_id", "annotation", ["genbank_region_id"])
58-
op.create_index("ix_annotation_scheme_key_value", "annotation", ["scheme", "key", "value"])
75+
op.create_index(
76+
"ix_annotation_genbank_region_id", "annotation", ["genbank_region_id"]
77+
)
78+
op.create_index(
79+
"ix_annotation_scheme_key_value", "annotation", ["scheme", "key", "value"]
80+
)
5981

6082
# JSON metadata indexing (existence/path queries)
6183
op.execute("""
@@ -80,6 +102,7 @@ def upgrade():
80102
EXECUTE FUNCTION public.set_timestamp_annotation();
81103
""")
82104

105+
83106
def downgrade():
84107
# drop triggers and their functions first
85108
op.execute("DROP TRIGGER IF EXISTS annotation_set_timestamp ON public.annotation;")
@@ -92,7 +115,9 @@ def downgrade():
92115
op.drop_index("ix_annotation_scheme_key_value", table_name="annotation")
93116
op.drop_index("ix_annotation_genbank_region_id", table_name="annotation")
94117
op.drop_index("ix_annotation_compound_id", table_name="annotation")
95-
op.drop_constraint("uq_annotation_target_scheme_key_value", "annotation", type_="unique")
118+
op.drop_constraint(
119+
"uq_annotation_target_scheme_key_value", "annotation", type_="unique"
120+
)
96121

97122
# drop table
98123
op.drop_table("annotation")

0 commit comments

Comments
 (0)