private static string ToConnectionString(DatabaseConnectionConfig databaseConnection) { var csb = new SqlConnectionStringBuilder { DataSource = databaseConnection.Server, UserID = databaseConnection.User, Password = databaseConnection.Password.ToUnsecureString() }; return csb.ToString(); }
public static IProcessExecutionResult SqlCmdExec(DatabaseConnectionConfig databaseConnection, string script) { var args = string.Format(@"-r 1 -S ""{0}"" -U ""{1}"" -P ""{2}""", databaseConnection.Server, databaseConnection.User, databaseConnection.Password.ToUnsecureString()); var input = new MemoryStream(); var writer = new StreamWriter(input); writer.WriteLine(script); writer.WriteLine("GO"); writer.WriteLine("exit"); writer.Flush(); input.Position = 0; return ProcessUtils.Exec("sqlcmd", args, input); }
public static IProcessExecutionResult BackupDatabase(DatabaseConnectionConfig databaseConnection, string dbName, string backupFilePath, bool compress) { var sb = new StringBuilder(); sb.AppendFormat("BACKUP DATABASE {0} TO DISK = @backupFile WITH FORMAT, INIT, NAME = @name, SKIP, NOREWIND, NOUNLOAD, STATS = 10", ToBracketedIdentifier(dbName)); var parameters = new SqlParamCollection(); parameters.Add("@backupFile", SqlDbType.NVarChar).Value = backupFilePath; parameters.Add("@name", SqlDbType.NVarChar).Value = dbName; if (compress) sb.Append(", COMPRESSION"); return new SqlCommandExecutionResult(databaseConnection, dbName, sb.ToString(), parameters); }
public void Add(DatabaseConnectionConfig dbConfig, string dbName, StateHash hash) { var db = GetDatabaseDir(dbName, true); var file = Path.Combine(db.FullName, GetFileName(hash)); if (File.Exists(file)) return; if (mAutoGC) GarbageCollect(true); var exitCode = 0; mLog.LogFormat("Caching {0} to {1}", dbName, file); if (!mDryRun) { using (var process = SqlUtils.BackupDatabase(dbConfig, dbName, file, mAllowCompression)) using (mLog.IndentScope()) { foreach (var line in process.GetOutput()) { mLog.Log(line.Line); } exitCode = process.ExitCode; } } if (exitCode != 0) { mLog.LogFormat("WARNING: Error caching {0}. Continuing anyways.", dbName); File.Delete(file); } else { UpdateHit(dbName, hash); } }
public static IReadOnlyCollection<Tuple<string, string>> GetLogicalAndPhysicalNamesFromBackupFile(DatabaseConnectionConfig databaseConnection, string backupFile) { using (var f = new SqlCommandFactory(ToConnectionString(databaseConnection), null)) using (var cmd = f.CreateCommand(null)) { cmd.CommandText = "RESTORE FILELISTONLY FROM DISK = @path"; cmd.Parameters.Add("@path", SqlDbType.NVarChar).Value = backupFile; var result = new List<Tuple<string, string>>(); using (var r = cmd.ExecuteReader()) { while (r.Read()) { var logicalName = (string)r["LogicalName"]; var physicalName = (string)r["PhysicalName"]; result.Add(Tuple.Create(logicalName, physicalName)); } } return result; } }
public void Add(DatabaseConnectionConfig dbConfig, string dbName, StateHash hash) { }
public RestoreDatabasesTransform(DatabaseConnectionConfig databaseConnection, DatabaseBackupInfo[] databases, StateHash knownState = null) { mDatabaseConnection = databaseConnection; mDatabases = databases; mKnownState = knownState; }
public DatabasesConfig(DatabaseConnectionConfig connection, BackupsConfig backups) { mConnection = connection; mBackups = backups; }
public static IProcessExecutionResult Exec(DatabaseConnectionConfig databaseConnection, string dbName, string script, IEnumerable<SqlParameter> parameters) { return new SqlCommandExecutionResult(databaseConnection, dbName, script, parameters); }
public SqlCommandExecutionResult(DatabaseConnectionConfig db, string name, string script, IEnumerable<SqlParameter> parameters) { mFactory = new SqlCommandFactory(ToConnectionString(db), OnMessage); mCommand = mFactory.CreateCommand(name); mCommand.CommandText = script; mOutput = new BlockingCollection<ProcessOutputLine>(); foreach (var sqlParameter in parameters) { mCommand.Parameters.Add(sqlParameter); } }