public static DbSchemaChange GetByNo(DbConnector dbConn, uint changeNo) { dbConn.SetQueryText(string.Format("select ChangeID, ChangeNo, ScriptName, DateInstall from A_SCHEMA_CHANGES_LOG where ChangeNo = {0}", changeNo)); dbConn.Execute(); if (!dbConn.NextRow()) { return null; } var sch = new DbSchemaChange(); sch.FromDb(dbConn); return sch; }
public static DbSchemaChange GetLast(DbConnector dbConn) { dbConn.SetQueryText("select ChangeID, ChangeNo, ScriptName, DateInstall from A_SCHEMA_CHANGES_LOG order by ChangeNo desc"); dbConn.Execute(); if (!dbConn.NextRow()) { return null; } var sch = new DbSchemaChange(); sch.FromDb(dbConn); return sch; }
private static bool _applySchemaChange(DbConnector dbConn, string changeFile) { Console.WriteLine("DbBuilder: Applying schema change script {0}...", changeFile); string cfn = Path.GetFileNameWithoutExtension(changeFile); if (cfn.Length != 9) { goto WrongScriptName; } string pfx = cfn.Substring(0, 4); if (pfx != "Sch-") { goto WrongScriptName; } uint chn = (uint)Convert.ToUInt32(cfn.Substring(4)); if (chn == 0) { goto WrongScriptName; } var dbSch = DbSchemaChange.GetByNo(dbConn, chn); if (dbSch != null) { Console.WriteLine("DbBuilder: Change #{0} already installed. Exit.", chn); return true; } dbSch = DbSchemaChange.GetLast(dbConn); if (dbSch == null) { Console.WriteLine("DbBuilder: Error: Schema seems not installed. STOP !"); return false; } if ((chn - 1) != dbSch.ChangeNo) { Console.WriteLine("DbBuilder: Error: Bad changes sequence. STOP !"); return false; } if (!_executeScript(dbConn, changeFile)) { Console.WriteLine("DbBuilder: Error: Script {0} failed. STOP !", changeFile); return false; } dbSch = new DbSchemaChange(); dbSch.ChangeID = Guid.NewGuid(); dbSch.ChangeNo = chn; dbSch.ScriptName = cfn; dbSch.DateInstall = DateTime.Now; dbSch.Add(dbConn); Console.WriteLine("DbBuilder: Schema change #{0}...", chn); return true; WrongScriptName: Console.WriteLine("DbBuilder: Error: Wrong script name. STOP !"); return false; }
private static bool _createSchema(DbConnector dbConn) { Console.WriteLine("DbBuilder: Creating schema changes log table..."); if (!_executeScript(dbConn, Path.Combine(_project, "Schema\\SchemaChangesLogTable.sql"))) { Console.WriteLine("DbBuilder: Error: Failed to create schema changes log table. STOP !"); return false; } Console.WriteLine("DbBuilder: Creating schema..."); DbSchemaChange sch = new DbSchemaChange(); sch.ChangeID = Guid.NewGuid(); sch.ChangeNo = 0; sch.ScriptName = "Schema.sql"; sch.DateInstall = DateTime.Now; if (!_executeScript(dbConn, Path.Combine(_project, "Schema\\" + sch.ScriptName))) { Console.WriteLine("DbBuilder: Error: Failed to create schema. STOP !"); return false; } sch.Add(dbConn); Console.WriteLine("DbBuilder: Done."); return true; }