Skip to content

Commit

Permalink
sqla2.0 (#17)
Browse files Browse the repository at this point in the history
* migrate to SQLAlchemy 2.0
  • Loading branch information
eloyfelix authored Jan 27, 2023
1 parent 8733f47 commit 1305063
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions cbl_migrator/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def fill_table(o_eng_conn, d_eng_conn, table, chunk_size):
res = connr.execute(q)
data = res.all()
if len(data):
last_id = data[-1].__getitem__(pk.name)
last_id = getattr(data[-1], pk.name)
with d_eng.begin() as conn:
conn.execute(
table.insert(),
Expand Down Expand Up @@ -225,7 +225,7 @@ def __copy_schema(self):
col = self.__fix_column_type(col, o_eng.name, d_eng.name)
col.autoincrement = False
new_metadata_cols.add(col)
table.columns = new_metadata_cols.as_immutable()
table.columns = new_metadata_cols.as_readonly()
new_metadata_tables[table_name] = table
metadata.tables = immutabledict(new_metadata_tables)
metadata.create_all(d_eng)
Expand Down
2 changes: 1 addition & 1 deletion cbl_migrator/test/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, ForeignKey, CheckConstraint
from sqlalchemy.types import String, Integer, Float, Text

Expand Down
66 changes: 32 additions & 34 deletions cbl_migrator/test/test_migration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import MetaData, create_engine, inspect
from sqlalchemy import MetaData, create_engine, inspect, insert
from .schema import Base, Compound, CompoundStructure, CompoundProperties
from .. import DbMigrator
import unittest
Expand Down Expand Up @@ -97,40 +97,38 @@ def __gen_test_data(self):
engine = create_engine(self.origin)
Base.metadata.create_all(bind=engine)

# compound data
stmt = Compound.__table__.insert(
[
{"cid": i, "structure_type": "MOL", "compound_name": "chemblone"}
for i in range(1, 42)
]
)
engine.execute(stmt)

# structure data
structure_data = []
for i in range(1, 42):
smiles = "CC(=O)Oc1ccccc1C(=O)O"
inchi_key = inchis[i - 1]
structure_data.append(
{
"sid": i,
"cid": i,
"smiles": smiles,
"molblock": molblock,
"inchi_key": inchi_key,
}
com_stmt = insert(Compound)
com_struct_stmt = insert(CompoundStructure)
com_props_stmt = insert(CompoundProperties)

with engine.begin() as conn:
conn.execute(
com_stmt,
[
{"cid": i, "structure_type": "MOL", "compound_name": "chemblone"}
for i in range(1, 42)
],
)
conn.execute(
com_struct_stmt,
[
{
"sid": i,
"cid": i,
"smiles": "CC(=O)Oc1ccccc1C(=O)O",
"molblock": molblock,
"inchi_key": inchis[i - 1],
}
for i in range(1, 42)
],
)
conn.execute(
com_props_stmt,
[
{"pid": i, "cid": i, "mw": random.random(), "logp": random.random()}
for i in range(1, 42)
],
)
stmt = CompoundStructure.__table__.insert(structure_data)
engine.execute(stmt)

# properties data
stmt = CompoundProperties.__table__.insert(
[
{"pid": i, "cid": i, "mw": random.random(), "logp": random.random()}
for i in range(1, 42)
]
)
engine.execute(stmt)

def __get_tables_insp(self):
o_eng = create_engine(self.origin)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if __name__ == "__main__":
setup(
name="cbl_migrator",
version="0.3.4",
version="0.3.5",
author="Eloy Félix",
author_email="eloyfelix@gmail.com",
description="Migrates Oracle dbs to PostgreSQL, MySQL and SQLite",
Expand All @@ -12,7 +12,7 @@
packages=["cbl_migrator", "cbl_migrator.bin"],
long_description=open("README.md", encoding="utf-8").read(),
long_description_content_type="text/markdown",
install_requires=["SQLAlchemy~=1.4"],
install_requires=["SQLAlchemy~=2.0"],
tests_require=["pytest"],
entry_points={
"console_scripts": ["cbl-migrator=cbl_migrator.bin.run_migrator:main"],
Expand Down

0 comments on commit 1305063

Please sign in to comment.