Skip to content

Commit

Permalink
Backwards compatiblity (#1022)
Browse files Browse the repository at this point in the history
* 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@mpie.de>
  • Loading branch information
jan-janssen and pyiron-runner authored Feb 1, 2023
1 parent 5f786fc commit 82a296a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
21 changes: 17 additions & 4 deletions pyiron_base/database/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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.")

Expand All @@ -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.")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand Down
46 changes: 22 additions & 24 deletions pyiron_base/database/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 82a296a

Please sign in to comment.