Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Jul 30, 2024
1 parent 501f542 commit aac53b4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/bio_data_to_db/bindingdb/fix_tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import html

import polars as pl
import sqlalchemy


def fix_assay_table(uri: str):
Expand All @@ -9,8 +10,8 @@ def fix_assay_table(uri: str):
Notes:
- original types are VARCHAR but we are converting them to TEXT. The code should work for both cases.
- the table is replaced
- this removes primary key and foreign key constraints.
- the table is replaced.
- primary key and foreign key constraints are reserved by manually adding them back just like the original table
"""
query = """
SELECT
Expand All @@ -36,6 +37,16 @@ def fix_assay_table(uri: str):
if_table_exists="replace",
)

with sqlalchemy.create_engine(uri).connect() as conn:
conn.execute(
sqlalchemy.text("""
ALTER TABLE assay MODIFY COLUMN `entryid` INT(11);
ALTER TABLE assay MODIFY COLUMN `assayid` INT(11);
ALTER TABLE assay ADD PRIMARY KEY (`entryid`,`assayid`);
ALTER TABLE assay ADD CONSTRAINT `assay_ibfk_1` FOREIGN KEY (`entryid`) REFERENCES `entry` (`entryid`);
""")
)

if __name__ == "__main__":
fix_assay_table("mysql://user:@localhost:3306/bind")

# if __name__ == "__main__":
# fix_assay_table("mysql://username:@localhost:3306/bind")

0 comments on commit aac53b4

Please sign in to comment.