From 82a296ace7afa1f77d5a0b66f81321baaeb25fce Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 1 Feb 2023 09:56:56 -0600 Subject: [PATCH] Backwards compatiblity (#1022) * Test with 2.0.0 * _init() no longer exists * Format black * add commit * add commit to AutorestoredConnection * Test backwards compatibility * Add future --------- Co-authored-by: pyiron-runner --- pyiron_base/database/generic.py | 21 ++++++++++++--- pyiron_base/database/tables.py | 46 ++++++++++++++++----------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/pyiron_base/database/generic.py b/pyiron_base/database/generic.py index 574c90879..fb9f49ff1 100644 --- a/pyiron_base/database/generic.py +++ b/pyiron_base/database/generic.py @@ -28,7 +28,7 @@ from sqlalchemy.exc import OperationalError, DatabaseError from threading import Thread, Lock from queue import SimpleQueue, Empty as QueueEmpty -from pyiron_base.database.tables import HistoricalTable +from pyiron_base.database.tables import get_historical_table from pyiron_base.utils.error import retry __author__ = "Murat Han Celik" @@ -435,6 +435,10 @@ def close(self): if self._conn is not None: self._conn.close() + def commit(self): + if self._conn is not None: + self._conn.commit() + class DatabaseAccess(IsDatabase): """ @@ -471,11 +475,12 @@ def __init__(self, connection_string, table_name, timeout=60): connection_string, connect_args={"connect_timeout": 15}, poolclass=NullPool, + future=True, ) self.conn = AutorestoredConnection(self._engine, timeout=self._timeout) self._keep_connection = self._timeout > 0 else: - self._engine = create_engine(connection_string) + self._engine = create_engine(connection_string, future=True) self.conn = self._engine.connect() self.conn.connection.create_function("like", 2, self.regexp) self._keep_connection = True @@ -486,10 +491,12 @@ def __init__(self, connection_string, table_name, timeout=60): def _create_table(): self.__reload_db() - self.simulation_table = HistoricalTable(str(table_name), self.metadata) + self.simulation_table = get_historical_table( + table_name=str(table_name), metadata=self.metadata, extend_existing=True + ) self.metadata.create_all(bind=self._engine) - # too many jobs trying to talk to the database can cause this too fail. + # too many jobs trying to talk to the database can cause this to fail. retry( _create_table, error=OperationalError, @@ -731,6 +738,7 @@ def add_column(self, col_name, col_type): % (self.simulation_table.name, col_name, col_type) ) ) + self.conn.commit() else: raise PermissionError("Not avilable in viewer mode.") @@ -756,6 +764,7 @@ def change_column_type(self, col_name, col_type): % (self.simulation_table.name, col_name, col_type) ) ) + self.conn.commit() else: raise PermissionError("Not avilable in viewer mode.") @@ -902,6 +911,7 @@ def add_item_dict(self, par_dict): result = self.conn.execute( self.simulation_table.insert().values(**par_dict) ).inserted_primary_key[-1] + self.conn.commit() if not self._keep_connection: self.conn.close() return result @@ -986,6 +996,7 @@ def _item_update(self, par_dict, item_id): ) try: self.conn.execute(query, par_dict) + self.conn.commit() except (OperationalError, DatabaseError): if not self._sql_lite: self.conn = AutorestoredConnection(self._engine) @@ -994,6 +1005,7 @@ def _item_update(self, par_dict, item_id): self.conn.connection.create_function("like", 2, self.regexp) self.conn.execute(query, par_dict) + self.conn.commit() if not self._keep_connection: self.conn.close() else: @@ -1015,6 +1027,7 @@ def delete_item(self, item_id): self.simulation_table.c["id"] == int(item_id) ) ) + self.conn.commit() if not self._keep_connection: self.conn.close() else: diff --git a/pyiron_base/database/tables.py b/pyiron_base/database/tables.py index 9e9b98e30..7830af94e 100644 --- a/pyiron_base/database/tables.py +++ b/pyiron_base/database/tables.py @@ -26,28 +26,26 @@ __date__ = "Sep, 2021" -class HistoricalTable(Table): +def get_historical_table(table_name, metadata, extend_existing=True): """The historical table.""" - - def _init(self, table_name, metadata, *args, extend_existing=True, **kwargs): - super()._init( - table_name, - metadata, - Column("id", Integer, primary_key=True, autoincrement=True), - Column("parentid", Integer), - Column("masterid", Integer), - Column("projectpath", String(50)), - Column("project", String(255)), - Column("job", String(50)), - Column("subjob", String(255)), - Column("chemicalformula", String(50)), - Column("status", String(20)), - Column("hamilton", String(20)), - Column("hamversion", String(50)), - Column("username", String(20)), - Column("computer", String(100)), - Column("timestart", DateTime), - Column("timestop", DateTime), - Column("totalcputime", Float), - extend_existing=extend_existing, - ) + return Table( + table_name, + metadata, + Column("id", Integer, primary_key=True, autoincrement=True), + Column("parentid", Integer), + Column("masterid", Integer), + Column("projectpath", String(50)), + Column("project", String(255)), + Column("job", String(50)), + Column("subjob", String(255)), + Column("chemicalformula", String(50)), + Column("status", String(20)), + Column("hamilton", String(20)), + Column("hamversion", String(50)), + Column("username", String(20)), + Column("computer", String(100)), + Column("timestart", DateTime), + Column("timestop", DateTime), + Column("totalcputime", Float), + extend_existing=extend_existing, + )