mirror of https://gitlab.com/pamhyr/pamhyr2
PamhyrDB, PamhyrID, AddFile: Fix some bugs.
parent
f19941f6da
commit
62a1726ef1
|
|
@ -28,27 +28,19 @@ from Model.Scenario import Scenario
|
||||||
|
|
||||||
class AddFile(SQLSubModel):
|
class AddFile(SQLSubModel):
|
||||||
_sub_classes = []
|
_sub_classes = []
|
||||||
_id_cnt = 0
|
|
||||||
|
|
||||||
def __init__(self, id: int = -1, enabled=True,
|
def __init__(self, id: int = -1, enabled=True,
|
||||||
name="", path="", text="",
|
name="", path="", text="",
|
||||||
status=None):
|
status=None):
|
||||||
super(AddFile, self).__init__()
|
super(AddFile, self).__init__(id)
|
||||||
|
|
||||||
if id == -1:
|
|
||||||
self.id = AddFile._id_cnt
|
|
||||||
else:
|
|
||||||
self.id = id
|
|
||||||
|
|
||||||
self._status = status
|
self._status = status
|
||||||
|
|
||||||
self._enabled = enabled
|
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._path = path
|
||||||
self._text = text
|
self._text = text
|
||||||
|
|
||||||
AddFile._id_cnt = max(id, AddFile._id_cnt+1)
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
|
|
@ -115,9 +107,9 @@ class AddFile(SQLSubModel):
|
||||||
self._status.modified()
|
self._status.modified()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _db_create(cls, execute):
|
def _db_create(cls, execute, ext=""):
|
||||||
execute(f"""
|
execute(f"""
|
||||||
CREATE TABLE additional_files(
|
CREATE TABLE additional_files{ext} (
|
||||||
{cls.create_db_add_pamhyr_id()},
|
{cls.create_db_add_pamhyr_id()},
|
||||||
enabled BOOLEAN NOT NULL,
|
enabled BOOLEAN NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
|
|
@ -141,10 +133,28 @@ class AddFile(SQLSubModel):
|
||||||
cls._db_create(execute)
|
cls._db_create(execute)
|
||||||
|
|
||||||
if release < 13:
|
if release < 13:
|
||||||
cls.update_db_add_pamhyr_id(execute)
|
cls._db_update_to_0_0_13(execute)
|
||||||
|
|
||||||
return True
|
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
|
@classmethod
|
||||||
def _db_load(cls, execute, data=None):
|
def _db_load(cls, execute, data=None):
|
||||||
new = []
|
new = []
|
||||||
|
|
@ -177,7 +187,7 @@ class AddFile(SQLSubModel):
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"additional_files(pamhyr_id, enabled, name, path, text) " +
|
"additional_files(pamhyr_id, enabled, name, path, text) " +
|
||||||
"VALUES (" +
|
"VALUES (" +
|
||||||
f"{self.id}, {self._enabled}, " +
|
f"{self._pamhyr_id}, {self._enabled}, " +
|
||||||
f"'{self._db_format(self._name)}', " +
|
f"'{self._db_format(self._name)}', " +
|
||||||
f"'{self._db_format(self._path)}', " +
|
f"'{self._db_format(self._path)}', " +
|
||||||
f"'{self._db_format(self._text)}'" +
|
f"'{self._db_format(self._text)}'" +
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,19 @@ class Scenario(SQLSubModel):
|
||||||
|
|
||||||
return True
|
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
|
@classmethod
|
||||||
def _db_load(cls, execute, data=None):
|
def _db_load(cls, execute, data=None):
|
||||||
scenarios = {}
|
scenarios = {}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class Study(SQLModel):
|
||||||
|
|
||||||
def __init__(self, filename=None, init_new=True):
|
def __init__(self, filename=None, init_new=True):
|
||||||
# Metadata
|
# Metadata
|
||||||
self._version = "0.0.12"
|
self._version = "0.0.13"
|
||||||
self.creation_date = datetime.now()
|
self.creation_date = datetime.now()
|
||||||
self.last_modification_date = datetime.now()
|
self.last_modification_date = datetime.now()
|
||||||
self.last_save_date = datetime.now()
|
self.last_save_date = datetime.now()
|
||||||
|
|
@ -322,7 +322,7 @@ class Study(SQLModel):
|
||||||
commit=True
|
commit=True
|
||||||
)
|
)
|
||||||
|
|
||||||
new.scenarios = Scenario._db_load(
|
new.scenarios = Scenarios._db_load(
|
||||||
sql_exec,
|
sql_exec,
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,11 @@ class PamhyrID(object):
|
||||||
|
|
||||||
self._pamhyr_id = self.get_new_pamhyr_id(id)
|
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
|
pid = id
|
||||||
|
|
||||||
if pid == -1:
|
if pid < 0:
|
||||||
pid = PamhyrID._pamhyr_id_cnt
|
pid = PamhyrID._pamhyr_id_cnt
|
||||||
|
|
||||||
PamhyrID._pamhyr_id_cnt = max(
|
PamhyrID._pamhyr_id_cnt = max(
|
||||||
|
|
@ -37,6 +38,8 @@ class PamhyrID(object):
|
||||||
PamhyrID._pamhyr_id_cnt + 1
|
PamhyrID._pamhyr_id_cnt + 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return pid
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pamhyr_id(self):
|
def pamhyr_id(self):
|
||||||
return self._pamhyr_id
|
return self._pamhyr_id
|
||||||
|
|
@ -46,12 +49,10 @@ class PamhyrID(object):
|
||||||
autoset=True):
|
autoset=True):
|
||||||
execute(
|
execute(
|
||||||
f"ALTER TABLE {table} " +
|
f"ALTER TABLE {table} " +
|
||||||
f"ADD COLUMN pamhyr_id INTEGER"
|
f"ADD COLUMN pamhyr_id INTEGER NOT NULL DEFAULT -1"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not autoset:
|
if autoset:
|
||||||
return True
|
|
||||||
|
|
||||||
table = execute(f"SELECT id FROM {table}")
|
table = execute(f"SELECT id FROM {table}")
|
||||||
|
|
||||||
for row in table:
|
for row in table:
|
||||||
|
|
@ -63,6 +64,8 @@ class PamhyrID(object):
|
||||||
f"WHERE id = {row[0]}"
|
f"WHERE id = {row[0]}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_db_add_pamhyr_id(cls):
|
def create_db_add_pamhyr_id(cls):
|
||||||
return "pamhyr_id INTEGER NOT NULL"
|
return "pamhyr_id INTEGER NOT NULL"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue