def make(self, workDir): allSettings = self.getValue(".") for name, dbData in allSettings.items(): if name == "__make__": continue elif "__make__" in dbData and dbData["__make__"] is False: continue if not "version" in dbData: raise ValueError("Must specify db version for %s " % name) if not "schema" in dbData: raise ValueError("Must specify db schema for %s" % name) schemaProp = self.getProperty(name.replace(".", "\.") + ".schema") schemaPath = schemaProp.resolveAsRelativePath() if schemaPath and os.path.exists(schemaPath): with open(schemaPath, "r") as schemaFile: schemaData = schemaFile.read() else: schemaData = schemaProp.getValue() if not schemaData: print("Error: Schema not not set") sys.exit(1) targetDatabaseConfigItem = { "path": dbData["path"], "schema": schemaData, "version": dbData["version"], "data": {} } colKey = "name" if "col_key" not in dbData else dbData["col_key"] colVal = "value" if "col_val" not in dbData else dbData["col_val"] db = Database(schemaData) for tableName, data in dbData["data"].items(): table = db.getTable(tableName) if not table: logger.warn( "Skipping table %s as it's not in supplied schema" % tableName) continue if tableName not in targetDatabaseConfigItem["data"]: targetDatabaseConfigItem["data"][tableName] = [] for key, val in data.items(): if key.startswith("__") and key.endswith("__"): continue targetDatabaseConfigItem["data"][tableName].append({ colKey: key, colVal: val }) self.setValue("update.databases.%s" % name.replace(".", "\."), targetDatabaseConfigItem)
def make(self, workDir): allSettings = self.getConfigValue(".") for name, dbData in allSettings.items(): if name == "__make__": continue elif "__make__" in dbData and dbData["__make__"] is False: continue if not "version" in dbData: raise ValueError("Must specify db version for %s " % name) if not "schema" in dbData: raise ValueError("Must specify db schema for %s" % name) schemaProp = self.getConfigProperty(name.replace(".", "\.") + ".schema") schemaPath = schemaProp.resolveAsRelativePath() if schemaPath and os.path.exists(schemaPath): with open(schemaPath, "r") as schemaFile: schemaData = schemaFile.read() else: schemaData = schemaProp.getValue() if not schemaData: print("Error: Schema not not set") sys.exit(1) targetDatabaseConfigItem = { "path": dbData["path"], "schema": schemaData, "version": dbData["version"], "data": {} } colKey = "name" if "col_key" not in dbData else dbData["col_key"] colVal = "value" if "col_val" not in dbData else dbData["col_val"] db = Database(schemaData) for tableName, data in dbData["data"].items(): table = db.getTable(tableName) if not table: logger.warn("Skipping table %s as it's not in supplied schema" % tableName) continue if tableName not in targetDatabaseConfigItem["data"]: targetDatabaseConfigItem["data"][tableName] = [] for key, val in data.items(): if key.startswith("__") and key.endswith("__"): continue targetDatabaseConfigItem["data"][tableName].append({ colKey: key, colVal: val }) self.setConfigValue("update.databases.%s" % name.replace(".", "\."), targetDatabaseConfigItem)
def make(self, workDir): allSettings = self.getValue(".", {}) for name, dbData in allSettings.items(): if name == "__make__": continue elif "__make__" in dbData and dbData["__make__"] is False: continue elif "__depend__" in dbData: targetDep = self.getMaker().getConfig().get( dbData["__depend__"], None) if not targetDep or ("__make__" in targetDep and targetDep["__make__"] == False): continue path = workDir + dbData["path"] if not "version" in dbData: raise ValueError("Must specify db version for %s " % name) logger.debug("Making %s" % dbData["path"]) schemaProp = self.getProperty(name.replace(".", "\.") + ".schema") schemaPath = schemaProp.resolveAsRelativePath() if schemaPath and os.path.exists(schemaPath): with open(schemaPath, "r") as schemaFile: schemaData = schemaFile.read() else: schemaData = schemaProp.getValue() db = Database(schemaData) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) if os.path.exists(path): os.remove(path) conn = sqlite3.connect(path) for tableName, data in dbData["data"].items(): table = db.getTable(tableName) assert table, "Table %s is not in supplied schema" % tableName for row in data: table.createRow(**row) conn.executescript("PRAGMA user_version = %s;" % dbData["version"]) conn.executescript(schemaData) queries = db.getQueries() for q in queries: conn.execute(q) conn.commit() conn.close()
def make(self, workDir): allSettings = self.getValue(".", {}) for name, dbData in allSettings.items(): if name == "__make__": continue elif "__make__" in dbData and dbData["__make__"] is False: continue elif "__depend__" in dbData: targetDep = self.getMaker().getConfig().get(dbData["__depend__"], None) if not targetDep or ("__make__" in targetDep and targetDep["__make__"] == False): continue path = workDir + dbData["path"] if not "version" in dbData: raise ValueError("Must specify db version for %s " % name) logger.debug("Making %s" % dbData["path"]) schemaProp = self.getProperty(name.replace(".", "\.") + ".schema") schemaPath = schemaProp.resolveAsRelativePath() if schemaPath and os.path.exists(schemaPath): with open(schemaPath, "r") as schemaFile: schemaData = schemaFile.read() else: schemaData = schemaProp.getValue() db = Database(schemaData) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) if os.path.exists(path): os.remove(path) conn = sqlite3.connect(path) for tableName, data in dbData["data"].items(): table = db.getTable(tableName) assert table, "Table %s is not in supplied schema" % tableName for row in data: table.createRow(**row) conn.executescript("PRAGMA user_version = %s;" % dbData["version"]) conn.executescript(schemaData) queries = db.getQueries() for q in queries: conn.execute(q) conn.commit() conn.close()
def diffSettings(self, settings, refDbPath): refDb = Database(refDbPath) if "schema" in settings and settings["schema"]: if os.path.exists(settings["schema"]): cuffSchemaFile = open(settings["schema"]) currDb = Database(cuffSchemaFile.read()) cuffSchemaFile.close() else: currDb = Database(settings["schema"]) if "version" in settings: currDb.setVersion(int(settings["version"])) else: currDb = None diffSettings = { "data": {} } databaseContent = { "data": {} } if not currDb or not refDb.isEqualSchema(currDb): diffSettings["schema"] = refDb.getSchema() if not currDb or not refDb.getVersion() == currDb.getVersion(): diffSettings["version"] = refDb.getVersion() colKeyName = settings["col_key"] if "col_key" in settings else "name" colValueName = settings["col_val"] if "col_val" in settings else "value" refSettingsData = settings["data"] for table in refDb.getTables(): if not table.name in refSettingsData: databaseContent["data"][table.name] = [] rows = table.selectRows() for row in rows: databaseContent["data"][table.name].append(row.toDict()) else: refSettingsTable = refSettingsData[table.name] diffSettings["data"][table.name] = {} for row in table.selectRows(): key = row.getValueFor(colKeyName) value = row.getValueFor(colValueName) if not key in refSettingsTable or refSettingsTable[key] != value: diffSettings["data"][table.name][key] = value return (diffSettings, databaseContent)