def _createDatabaseTables(self):
		self._database.connect(reuse_if_open=True)
		self._database.drop_tables(MODELS)
		self._database.create_tables(MODELS)

		PluginMetaDataModel.create(key=PluginMetaDataModel.KEY_DATABASE_SCHEME_VERSION, value=CURRENT_DATABASE_SCHEME_VERSION)
		self._database.close()
		self._logger.info("Database tables created")
	def _createOrUpgradeSchemeIfNecessary(self):
		schemeVersionFromDatabaseModel = None
		try:
			schemeVersionFromDatabaseModel = PluginMetaDataModel.get(PluginMetaDataModel.key == PluginMetaDataModel.KEY_DATABASE_SCHEME_VERSION)
			pass
		except Exception as e:
			errorMessage = str(e)
			if errorMessage.startswith("no such table"):

				self._logger.info("Create database-table, because didn't exists")
				self._createDatabaseTables()
			else:
				self._logger.error(str(e))

		if not schemeVersionFromDatabaseModel == None:
			currentDatabaseSchemeVersion = int(schemeVersionFromDatabaseModel.value)
			if (currentDatabaseSchemeVersion < CURRENT_DATABASE_SCHEME_VERSION):
				# evautate upgrade steps (from 1-2 , 1...6)
				self._logger.info("We need to upgrade the database scheme from: '" + str(currentDatabaseSchemeVersion) + "' to: '" + str(CURRENT_DATABASE_SCHEME_VERSION) + "'")

				try:
					self.backupDatabaseFile(self._databasePath)
					self._upgradeDatabase(currentDatabaseSchemeVersion, CURRENT_DATABASE_SCHEME_VERSION)
				except Exception as e:
					self._logger.error("Error during database upgrade!!!!")
					self._logger.exception(e)
					return
				self._logger.info("Database-scheme successfully upgraded.")
		pass
 def backupDatabaseFile(self, backupFolder):
     now = datetime.datetime.now()
     currentDate = now.strftime("%Y%m%d-%H%M")
     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))
     backupDatabaseFileName = "printJobHistory-backup-" + currentDate + "-V" + currentSchemeVersion + ".db"
     backupDatabaseFilePath = os.path.join(backupFolder,
                                           backupDatabaseFileName)
     if not os.path.exists(backupDatabaseFilePath):
         shutil.copy(self._databaseFileLocation, backupDatabaseFilePath)
         self._logger.info("Backup of printjobhistory database created '" +
                           backupDatabaseFilePath + "'")
     else:
         self._logger.warning("Backup of printjobhistory database ('" +
                              backupDatabaseFilePath +
                              "') is already present. No backup created.")
     return backupDatabaseFilePath
	def testConnection(self, type, host, port,  databaeName, username, password):

		databaseToTest = None
		if ("postgres" == type):
			databaseToTest = PostgresqlDatabase(
				databaeName,
				user=username,
				password=password,
				host=host,
				port=port
			)
		else:
			databaseToTest = SqliteDatabase(self._databaseFileLocation)
		DatabaseManager.db = databaseToTest
		databaseToTest.bind(MODELS)
		# self._logger.info("Check if database-scheme upgrade needed.")
		# self._createOrUpgradeSchemeIfNecessary()

		schemeVersion = None
		jobCount = None
		try:
			databaseToTest.connect(reuse_if_open=True)

			schemeVersionFromDatabaseModel = None
			try:
				# scheme version
				schemeVersionFromDatabaseModel = PluginMetaDataModel.get(
					PluginMetaDataModel.key == PluginMetaDataModel.KEY_DATABASE_SCHEME_VERSION)
				schemeVersionFromDatabaseModel = int(schemeVersionFromDatabaseModel.value)

				# job count
				jobCount = self.countPrintJobsByQuery({
					"filterName" : "all"
				})
				pass
			except Exception as e:
				errorMessage = str(e)
				if errorMessage.startswith("no such table"):
					pass

			databaseToTest.close()


		except Exception as e:
			# Because this block of code is wrapped with "atomic", a
			# new transaction will begin automatically after the call
			# to rollback().
			errorMessage =  str(e);
			self._logger.warn("Test Database-Connection failed:" +errorMessage)
			return {
				"error": errorMessage
			}

		return {
			"schemeVersion": schemeVersionFromDatabaseModel,
			"jobCount": jobCount
		}