PamhyrDB, PamhyrID, AddFile: Fix some bugs.

scenarios
Pierre-Antoine Rouby 2024-07-19 16:29:47 +02:00
parent f19941f6da
commit 62a1726ef1
4 changed files with 55 additions and 29 deletions

View File

@ -28,27 +28,19 @@ from Model.Scenario import Scenario
class AddFile(SQLSubModel):
_sub_classes = []
_id_cnt = 0
def __init__(self, id: int = -1, enabled=True,
name="", path="", text="",
status=None):
super(AddFile, self).__init__()
if id == -1:
self.id = AddFile._id_cnt
else:
self.id = id
super(AddFile, self).__init__(id)
self._status = status
self._enabled = enabled
self._name = f"File {self.id}" if name == "" else name
self._name = f"File #{self._pamhyr_id}" if name == "" else name
self._path = path
self._text = text
AddFile._id_cnt = max(id, AddFile._id_cnt+1)
def __getitem__(self, key):
value = None
@ -115,9 +107,9 @@ class AddFile(SQLSubModel):
self._status.modified()
@classmethod
def _db_create(cls, execute):
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE additional_files(
CREATE TABLE additional_files{ext} (
{cls.create_db_add_pamhyr_id()},
enabled BOOLEAN NOT NULL,
name TEXT NOT NULL,
@ -141,10 +133,28 @@ class AddFile(SQLSubModel):
cls._db_create(execute)
if release < 13:
cls.update_db_add_pamhyr_id(execute)
cls._db_update_to_0_0_13(execute)
return True
@classmethod
def _db_update_to_0_0_13(cls, execute):
table = "additional_files"
cls.update_db_add_pamhyr_id(execute, table)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")
execute(
"INSERT INTO additional_files_tmp " +
"(pamhyr_id, enabled, name, path, text, scenario) " +
"SELECT pamhyr_id, enabled, name, path, text, scenario " +
"FROM additional_files"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
new = []
@ -177,7 +187,7 @@ class AddFile(SQLSubModel):
"INSERT INTO " +
"additional_files(pamhyr_id, enabled, name, path, text) " +
"VALUES (" +
f"{self.id}, {self._enabled}, " +
f"{self._pamhyr_id}, {self._enabled}, " +
f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._path)}', " +
f"'{self._db_format(self._text)}'" +

View File

@ -95,6 +95,19 @@ class Scenario(SQLSubModel):
return True
@classmethod
def update_db_add_scenario(cls, execute, table):
execute(
f"ALTER TABLE {table} " +
"ADD COLUMN scenario INTEGER NOT NULL DEFAULT 0"
)
execute(
f"ALTER TABLE {table} " +
"ADD CONSTRAINT fk_scenario FOREIGN KEY (scenario) " +
"REFERENCES scenario(id)"
)
@classmethod
def _db_load(cls, execute, data=None):
scenarios = {}

View File

@ -44,7 +44,7 @@ class Study(SQLModel):
def __init__(self, filename=None, init_new=True):
# Metadata
self._version = "0.0.12"
self._version = "0.0.13"
self.creation_date = datetime.now()
self.last_modification_date = datetime.now()
self.last_save_date = datetime.now()
@ -322,7 +322,7 @@ class Study(SQLModel):
commit=True
)
new.scenarios = Scenario._db_load(
new.scenarios = Scenarios._db_load(
sql_exec,
data=data
)

View File

@ -26,10 +26,11 @@ class PamhyrID(object):
self._pamhyr_id = self.get_new_pamhyr_id(id)
def get_new_pamhyr_id(self, id):
@classmethod
def get_new_pamhyr_id(cls, id):
pid = id
if pid == -1:
if pid < 0:
pid = PamhyrID._pamhyr_id_cnt
PamhyrID._pamhyr_id_cnt = max(
@ -37,6 +38,8 @@ class PamhyrID(object):
PamhyrID._pamhyr_id_cnt + 1
)
return pid
@property
def pamhyr_id(self):
return self._pamhyr_id
@ -46,12 +49,10 @@ class PamhyrID(object):
autoset=True):
execute(
f"ALTER TABLE {table} " +
f"ADD COLUMN pamhyr_id INTEGER"
f"ADD COLUMN pamhyr_id INTEGER NOT NULL DEFAULT -1"
)
if not autoset:
return True
if autoset:
table = execute(f"SELECT id FROM {table}")
for row in table:
@ -63,6 +64,8 @@ class PamhyrID(object):
f"WHERE id = {row[0]}"
)
return True
@classmethod
def create_db_add_pamhyr_id(cls):
return "pamhyr_id INTEGER NOT NULL"