private static void UpdateDatabaseWithScriptsIn(Server server, DbParams dbParams) { Console.WriteLine("Attempting to update DB {0} with scripts in {1}", dbParams.DbName, dbParams.DbPath); int scriptCount = 0; var dbPathTrimmed = dbParams.DbPath.FullName.TrimEnd('/').TrimEnd('\\').ToLower(); var managers = ChiefExecutive.GetAllUpdatableManagers(); foreach(var manager in managers) { if (dbPathTrimmed.EndsWith(manager.FolderName.ToLower())) { scriptCount = manager.RunScriptsToUpdate(server, dbParams.DbPath.FullName, dbParams.DbName); break; } } if (scriptCount == 0) { foreach (var manager in managers) { string possibleDirIfSuppliedDbRootDir = Path.Combine(dbPathTrimmed, manager.FolderName); if (Directory.Exists(possibleDirIfSuppliedDbRootDir)) scriptCount += manager.RunScriptsToUpdate(server, possibleDirIfSuppliedDbRootDir, dbParams.DbName); } } Console.WriteLine("Successfully executed {0} scripts", scriptCount); }
private static void RunScriptsInForDatabase(Server server, DbParams dbParams, IEnumerable<string> sqlFiles) { var executor = new SqlExecutor(server); var sqlFilesSortedAlphabetically = Order(sqlFiles); foreach(var sqlFile in sqlFilesSortedAlphabetically) { Console.WriteLine("Going to run " + sqlFile); executor.ExecuteNonQuery(File.ReadAllText(sqlFile), dbParams.DbName); } }
static void Main(string[] args) { DbParams dbParams = null; try { dbParams = new DbParams(args); } catch (NoParamsException) { Environment.Exit((int)DbParams.ExitStatus.ParametersNotSupplied); } catch (SomeParamsMissingException e) { Console.WriteLine(e.ParamNames[0] + " is mandatory"); Environment.Exit((int)DbParams.ExitStatus.SomeParameterNotSupplied); } if (!dbParams.DbPath.Exists) { Console.Error.WriteLine("--dbpath points to a directory that does not exist: " + dbParams.DbPath.FullName); Environment.Exit((int)DbParams.ExitStatus.ParameterNotValid); } var sqlFiles = Directory.GetFileSystemEntries(dbParams.DbPath.FullName, "*.sql"); if (!sqlFiles.Any()) { Console.Error.WriteLine("--dbpath points to a directory that does not contain any .sql files"); Environment.Exit((int)DbParams.ExitStatus.ParameterNotValid); } try { RunScriptsInForDatabase(ConnectionHelper.Connect(dbParams.DbHostName, dbParams.DbConnString), dbParams, sqlFiles); } catch (Exception e) { Console.Error.WriteLine("An exception occurred: " + e); Environment.Exit((int)DbParams.ExitStatus.ExecutionException); } Environment.Exit((int)DbParams.ExitStatus.Ok); }
static void Main(string[] args) { DbParams dbParams = null; try { dbParams = new DbParams(args); } catch (NoParamsException) { Environment.Exit((int)DbParams.ExitStatus.ParametersNotSupplied); } catch (SomeParamsMissingException e) { Console.WriteLine(e.ParamNames[0] + " is mandatory"); Environment.Exit((int)DbParams.ExitStatus.SomeParameterNotSupplied); } if (!dbParams.DbPath.Exists) { Console.Error.WriteLine("--dbpath points to a directory that does not exist"); Environment.Exit((int)DbParams.ExitStatus.ParameterNotValid); } try { var server = ConnectionHelper.Connect(dbParams.DbHostName, dbParams.DbConnString); UpdateDatabaseWithScriptsIn(server, dbParams); } catch (Exception e) { Console.Error.WriteLine("An exception occurred: " + e); Environment.Exit((int)DbParams.ExitStatus.ExecutionException); } Environment.Exit((int)DbParams.ExitStatus.Ok); }