From aac53b4cebf3bd33aed944006e909c66d2833a5f Mon Sep 17 00:00:00 2001 From: Kiyoon Kim Date: Tue, 30 Jul 2024 19:10:37 +0900 Subject: [PATCH] . --- src/bio_data_to_db/bindingdb/fix_tables.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/bio_data_to_db/bindingdb/fix_tables.py b/src/bio_data_to_db/bindingdb/fix_tables.py index d5c6abc..056f620 100644 --- a/src/bio_data_to_db/bindingdb/fix_tables.py +++ b/src/bio_data_to_db/bindingdb/fix_tables.py @@ -1,6 +1,7 @@ import html import polars as pl +import sqlalchemy def fix_assay_table(uri: str): @@ -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 @@ -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")