Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More SQLAlchemy compatibility #488

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def upgrade():
# Rescue current data
conn = op.get_bind()
Session = sessionmaker(bind=conn)
Session = sessionmaker(bind=conn, future=True)
session = Session()
Base = automap_base()
Base.prepare(conn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def upgrade():
conn = op.get_bind()
Session = sessionmaker(bind=conn)
Session = sessionmaker(bind=conn, future=True)
session = Session()
Base = automap_base()
Base.prepare(conn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def alter_tables_after_update():

def upgrade():
create_new_tables()
Session = sessionmaker(bind=op.get_bind())
Session = sessionmaker(bind=op.get_bind(), future=True)
session = Session()
Base = automap_base()
Base.prepare(op.get_bind())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def upgrade():
conn = op.get_bind()
Session = sessionmaker(bind=conn)
Session = sessionmaker(bind=conn, future=True)
session = Session()
meta = MetaData()
meta.reflect(conn)
Expand Down
2 changes: 1 addition & 1 deletion spinedb_api/db_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __enter__(self):
return None
self._context_open_count += 1
if self._session is None:
self._session = Session(self.engine)
self._session = Session(self.engine, future=True)
return self

def __exit__(self, _exc_type, _exc_val, _exc_tb):
Expand Down
9 changes: 3 additions & 6 deletions spinedb_api/spine_io/exporters/sql_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
# Public License for more details. You should have received a copy of the GNU Lesser General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################
"""
Module contains an SQL writer implementation.

"""
""" Module contains an SQL writer implementation. """
from sqlalchemy import Boolean, Column, DateTime, Float, Integer, MetaData, String, Table, create_engine
from sqlalchemy.orm import Session
from spinedb_api import parameter_value
Expand All @@ -32,11 +29,11 @@ def __init__(self, database, overwrite_existing):
self._overwrite_existing = overwrite_existing
if database.find("://") < 0:
database = "sqlite:///" + database
self._engine = create_engine(database)
self._engine = create_engine(database, future=True)
self._connection = self._engine.connect()
self._metadata = MetaData()
self._metadata.reflect(bind=self._engine)
self._session = Session(self._engine)
self._session = Session(self._engine, future=True)
self._table_name = None
self._column_names = None
self._column_converters = None
Expand Down
4 changes: 2 additions & 2 deletions spinedb_api/spine_io/importers/sqlalchemy_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
**extras: optional database schema
"""
self._connection_string = source
self._engine = create_engine(source)
self._engine = create_engine(source, future=True)

Check warning on line 48 in spinedb_api/spine_io/importers/sqlalchemy_connector.py

View check run for this annotation

Codecov / codecov/patch

spinedb_api/spine_io/importers/sqlalchemy_connector.py#L48

Added line #L48 was not covered by tests
self._connection = self._engine.connect()
self._session = Session(self._engine)
self._session = Session(self._engine, future=True)

Check warning on line 50 in spinedb_api/spine_io/importers/sqlalchemy_connector.py

View check run for this annotation

Codecov / codecov/patch

spinedb_api/spine_io/importers/sqlalchemy_connector.py#L50

Added line #L50 was not covered by tests
self._schema = extras.get("schema")
self._metadata = MetaData(schema=self._schema)
self._metadata.reflect(bind=self._engine)
Expand Down
24 changes: 12 additions & 12 deletions tests/spine_io/exporters/test_sql_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def test_write_header_only(self):
writer = SqlWriter(str(out_path), overwrite_existing=True)
write(db_map, writer, root_mapping)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("table 1", metadata.tables)
table = metadata.tables["table 1"]
column_names = [str(c) for c in table.c]
Expand All @@ -94,11 +94,11 @@ def test_write_single_object_class_and_object(self):
writer = SqlWriter(str(out_path), overwrite_existing=True)
write(db_map, writer, root_mapping)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("table 1", metadata.tables)
table = metadata.tables["table 1"]
column_names = [str(c) for c in table.c]
Expand Down Expand Up @@ -132,11 +132,11 @@ def test_write_datetime_value(self):
writer = SqlWriter(str(out_path), overwrite_existing=True)
write(db_map, writer, root_mapping)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("table 1", metadata.tables)
table = metadata.tables["table 1"]
column_names = [str(c) for c in table.c]
Expand Down Expand Up @@ -169,11 +169,11 @@ def test_write_duration_value(self):
writer = SqlWriter(str(out_path), overwrite_existing=True)
write(db_map, writer, root_mapping)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("table 1", metadata.tables)
table = metadata.tables["table 1"]
column_names = [str(c) for c in table.c]
Expand Down Expand Up @@ -201,11 +201,11 @@ def test_append_to_table(self):
write(db_map, writer, root_mapping1)
write(db_map, writer, root_mapping2)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("oc", metadata.tables)
table = metadata.tables["oc"]
column_names = [str(c) for c in table.c]
Expand Down Expand Up @@ -235,11 +235,11 @@ def test_appending_to_table_in_existing_database(self):
writer = SqlWriter(str(out_path), overwrite_existing=False)
write(db_map, writer, root_mapping)
self.assertTrue(out_path.exists())
engine = create_engine("sqlite:///" + str(out_path))
engine = create_engine("sqlite:///" + str(out_path), future=True)
with engine.begin():
metadata = MetaData()
metadata.reflect(bind=engine)
session = Session(engine)
session = Session(engine, future=True)
self.assertIn("oc", metadata.tables)
table = metadata.tables["oc"]
column_names = [str(c) for c in table.c]
Expand Down