static void Main(string[] args) { #if DEBUG Console.WriteLine("Running in debug with default parameters."); args = new[] { "localhost\\SQLExpress", "NAVDisclosure", @"C:\Projects\NAVD\Source\Database" }; #endif _catalog = args[1]; string connectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True", args[0], args[1]); string efConnectionString = string.Format("metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"", connectionString); DatabaseEntities db = new DatabaseEntities(efConnectionString); SchemaChange latestChange = db.SchemaChanges .OrderByDescending(changes => changes.MajorReleaseNumber) .OrderByDescending(changes => changes.MinorReleaseNumber) .OrderByDescending(changes => changes.PointReleaseNumber) .FirstOrDefault(); if (latestChange == null) { throw new ApplicationException("The SchemaChange table in your destination database is empty. It must contain at least 1 baseline record."); } string filePath = args[2]; if (filePath.Substring(filePath.Length - 1) != @"\") { filePath += @"\"; } using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = connection; cmd.CommandType = System.Data.CommandType.Text; RunSetup(cmd, new DirectoryInfo(filePath + "Helper")); DirectoryInfo schemaDir = new DirectoryInfo(filePath + "schema"); //Run Every Script in this minor release foreach (FileInfo script in schemaDir.GetFiles("?*.?*.?*.sql").OrderBy(f => f.Name) .Where(x => Int32.Parse(x.Name.Split('.')[0]) == latestChange.MajorReleaseNumber && Int32.Parse(x.Name.Split('.')[1]) == latestChange.MinorReleaseNumber && Int32.Parse(x.Name.Split('.')[2]) > latestChange.PointReleaseNumber)) { string command = script.OpenText().ReadToEnd(); command = Regex.Replace(command, @"(?I)\bGO\b", "\0"); Console.WriteLine(String.Format("Running {0}.", script.Name)); foreach (string commandFragment in command.Split('\0')) { cmd.CommandText = commandFragment; cmd.ExecuteNonQuery(); } } //Run Every Script in any subsequent minor release. foreach (FileInfo script in schemaDir.GetFiles("?*.?*.?*.sql") .Where(x => Int32.Parse(x.Name.Split('.')[0]) == latestChange.MajorReleaseNumber && Int32.Parse(x.Name.Split('.')[1]) > latestChange.MinorReleaseNumber)) { string command = script.OpenText().ReadToEnd(); command = Regex.Replace(command, @"(?I)\bGO\b", "\0"); Console.WriteLine(String.Format("Running {0}.", script.Name)); foreach (string commandFragment in command.Split('\0')) { cmd.CommandText = commandFragment; cmd.ExecuteNonQuery(); } } RemoveFunctions(cmd); RemoveStoredProcedures(cmd); RemoveViews(cmd); AddFunctions(cmd, filePath); AddViews(cmd, filePath); AddStoredProcedures(cmd, filePath); RunTeardown(cmd, new DirectoryInfo(filePath + "Helper")); #if DEBUG Console.ReadLine(); #endif } } }
static void Main(string[] args) { string connectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True", args[0], args[1]); string efConnectionString = string.Format("metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"", connectionString); DatabaseEntities db = new DatabaseEntities(efConnectionString); SchemaChange latestChange = (from changes in db.SchemaChanges orderby changes.MajorVersion descending, changes.MinorVersion descending select changes).FirstOrDefault<SchemaChange>(); if (latestChange == null) { throw new ApplicationException("The SchemaChange table in your destination database is empty. It must contain at least 1 baseline record."); } string filePath = args[2]; if (filePath.Substring(filePath.Length-1) != @"\") { filePath += @"\"; } using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandType = System.Data.CommandType.Text; DirectoryInfo schemaDir = new DirectoryInfo(filePath + "schema"); foreach (FileInfo script in schemaDir.GetFiles("*.sql") .Where(x => Int32.Parse(x.Name.Split('.')[0]) == latestChange.MajorVersion && Int32.Parse(x.Name.Split('.')[1]) > latestChange.MinorVersion)) { string command = script.OpenText().ReadToEnd(); command = Regex.Replace(command, @"(?I)\bGO\b", "\0"); foreach (string commandFragment in command.Split('\0')) { cmd.CommandText = commandFragment; cmd.ExecuteNonQuery(); } SchemaChange newChange = new SchemaChange(); newChange.MajorVersion = int.Parse(script.Name.Split('.')[0]); newChange.MinorVersion = int.Parse(script.Name.Split('.')[1]); newChange.ScriptName = script.Name; newChange.DateApplied = DateTime.Now; db.SchemaChanges.AddObject(newChange); db.SaveChanges(); } RemoveFuncs(cmd); AddFuncs(cmd, filePath); RemoveProcs(cmd); AddProcs(cmd, filePath); RemoveViews(cmd); AddViews(cmd, filePath); } } }