Skip to content

Commit

Permalink
New Version 1.4.1
Browse files Browse the repository at this point in the history
- B #164 migration database scheme from 4-5 was broken (current scheme is version 6)
  • Loading branch information
OllisGit committed May 24, 2021
1 parent 3dbd15c commit 1c58e67
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
75 changes: 51 additions & 24 deletions octoprint_SpoolManager/DatabaseManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,45 +224,63 @@ def _upgradeFrom5To6(self):
return
pass


self._logger.info(" Successfully 5 -> 6")
pass

def _upgradeFrom4To5(self):
self._logger.info(" Starting 4 -> 5")
# What is changed:
# SpoolModel (needed, because lats script created a new table and altering was already done
# SpoolModel (needed, because last script created a new table and altering was already done
# - materialCharacteristic = CharField(null=True, index=True) # strong, soft,... # since V4: new
# - material = CharField(null=True, index=True) # since V4: added index
# - vendor = CharField(null=True, index=True) # since V4: added index
connection = sqlite3.connect(self._databaseSettings.fileLocation)
cursor = connection.cursor()

sql = """
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE 'spo_spoolmodel' ADD 'updated' DATETIME;
ALTER TABLE 'spo_spoolmodel' ADD 'originator' CHAR(60);
ALTER TABLE 'spo_spoolmodel' ADD 'materialCharacteristic' VARCHAR(255);
ALTER TABLE 'spo_spoolmodel' ADD 'isActive' INTEGER;
UPDATE 'spo_spoolmodel' SET isActive=1;
CREATE INDEX spoolmodel_materialCharacteristic ON spo_spoolmodel (materialCharacteristic);
CREATE INDEX spoolmodel_material ON spo_spoolmodel (material);
CREATE INDEX spoolmodel_vendor ON spo_spoolmodel (vendor);
UPDATE 'spo_pluginmetadatamodel' SET value=5 WHERE key='databaseSchemeVersion';
COMMIT;
PRAGMA foreign_keys=on;
"""
cursor.executescript(sql)
self._executeSQLQuietly(cursor, "ALTER TABLE 'spo_spoolmodel' ADD 'updated' DATETIME")
self._executeSQLQuietly(cursor, "ALTER TABLE 'spo_spoolmodel' ADD 'originator' CHAR(60)")
self._executeSQLQuietly(cursor, "ALTER TABLE 'spo_spoolmodel' ADD 'materialCharacteristic' VARCHAR(255)")
self._executeSQLQuietly(cursor, "ALTER TABLE 'spo_spoolmodel' ADD 'isActive' INTEGER")
self._executeSQLQuietly(cursor, "UPDATE 'spo_spoolmodel' SET isActive=1")
self._executeSQLQuietly(cursor, "CREATE INDEX spoolmodel_materialCharacteristic ON spo_spoolmodel (materialCharacteristic)")
self._executeSQLQuietly(cursor, "CREATE INDEX spoolmodel_material ON spo_spoolmodel (material)")
self._executeSQLQuietly(cursor, "CREATE INDEX spoolmodel_vendor ON spo_spoolmodel (vendor)")

self._executeSQLQuietly(cursor, "UPDATE 'spo_pluginmetadatamodel' SET value=5 WHERE key='databaseSchemeVersion'")

# sql = """
# PRAGMA foreign_keys=off;
# BEGIN TRANSACTION;
#
# ALTER TABLE 'spo_spoolmodel' ADD 'updated' DATETIME;
# ALTER TABLE 'spo_spoolmodel' ADD 'originator' CHAR(60);
# ALTER TABLE 'spo_spoolmodel' ADD 'materialCharacteristic' VARCHAR(255);
# ALTER TABLE 'spo_spoolmodel' ADD 'isActive' INTEGER;
# UPDATE 'spo_spoolmodel' SET isActive=1;
#
# CREATE INDEX spoolmodel_materialCharacteristic ON spo_spoolmodel (materialCharacteristic);
# CREATE INDEX spoolmodel_material ON spo_spoolmodel (material);
# CREATE INDEX spoolmodel_vendor ON spo_spoolmodel (vendor);
#
# UPDATE 'spo_pluginmetadatamodel' SET value=5 WHERE key='databaseSchemeVersion';
# COMMIT;
# PRAGMA foreign_keys=on;
# """
# cursor.executescript(sql)

connection.close()

self._logger.info(" Successfully 4 -> 5")
pass

def _executeSQLQuietly(self, cursor, sqlStatement):
try:
cursor.execute(sqlStatement)
except Exception as e:
self._logger.error(sqlStatement)
self._logger.exception(e)


def _upgradeFrom4To5_HACK(self, sqlStatement):

connection = sqlite3.connect(self._databaseSettings.fileLocation)
Expand Down Expand Up @@ -311,6 +329,7 @@ def _upgradeFrom3To4(self):
connection = sqlite3.connect(self._databaseSettings.fileLocation)
cursor = connection.cursor()

# SCHROTT!!!!! zuerst 'ALTER' und dann eine neue Tabelle erstellen ohne die ALTER-Spalten, SUPER !!!!
sql = """
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
Expand Down Expand Up @@ -445,7 +464,6 @@ def _upgradeFrom1To2(self):
# Calculate the remaining weight for all current spools
with self._database.atomic() as transaction: # Opens new transaction.
try:

allSpoolModels = self.loadAllSpoolsByQuery(None)
if (allSpoolModels != None):
for spoolModel in allSpoolModels:
Expand Down Expand Up @@ -630,7 +648,16 @@ def backupDatabaseFile(self):
self._logger.info("Starting database backup")
now = datetime.datetime.now()
currentDate = now.strftime("%Y%m%d-%H%M")
backupDatabaseFilePath = self._databaseSettings.fileLocation[0:-3] + "-backup-"+currentDate+".db"
currentSchemeVersion = "unknown"
try:
currentSchemeVersion = PluginMetaDataModel.get(
PluginMetaDataModel.key == PluginMetaDataModel.KEY_DATABASE_SCHEME_VERSION)
if (currentSchemeVersion != None):
currentSchemeVersion = str(currentSchemeVersion.value)
except Exception as e:
self._logger.exception("Could not read databasescheme version:" + str(e))

backupDatabaseFilePath = self._databaseSettings.fileLocation[0:-3] + "-backup-V" + currentSchemeVersion + "-" +currentDate+".db"
# backupDatabaseFileName = "spoolmanager-backup-"+currentDate+".db"
# backupDatabaseFilePath = os.path.join(backupFolder, backupDatabaseFileName)
if not os.path.exists(backupDatabaseFilePath):
Expand Down Expand Up @@ -771,7 +798,7 @@ def loadDatabaseMetaInformations(self, databaseSettings = None):

def loadSpool(self, databaseId, withReusedConnection=False):
def databaseCallMethode():
return SpoolModel.get_by_id(databaseId)
return SpoolModel.get_or_none(databaseId)

return self._handleReusableConnection(databaseCallMethode, withReusedConnection, "loadSpool")

Expand Down
3 changes: 0 additions & 3 deletions octoprint_SpoolManager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,6 @@ def on_event(self, event, payload):
self.alreadyCanceled = True
self._on_printJobFinished("canceled", payload)

elif (Events.CONNECTED == event):
self._on_printer_connected(payload)

if (Events.FILE_SELECTED == event or
Events.FILE_DESELECTED == event or
Events.UPDATED_FILES == event):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
#
plugin_version = "1.4.0"
plugin_version = "1.4.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 1c58e67

Please sign in to comment.