Exemplo n.º 1
0
        public IEnumerable <SqlUpdateScript> GetScripts()
        {
            var names = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(x => x.StartsWith(Root)).OrderBy(x => x);

            foreach (var name in names)
            {
                // The embedded resource option changes the path and puts an underscore in the version number,
                // "DatabaseUpdates.v3.16.DataLog.sql" will appear as "DatabaseUpdates.v3._16.DataLog.sql".  So
                // This code is to compensate for that and to extract out the version correctly.
                var match = Regex.Match(name, @"\.v([0-9]+)\._([0-9]+)\.*_*([0-9]*)\.(\w+)\.", RegexOptions.IgnoreCase);
                if (!match.Success)
                {
                    throw new InvalidDataException("SQL Resource Update Script Name is invalid: {0}".FormatWith(Path.GetFileName(name)));
                }
                string  value3   = match.Groups[3].Length == 0 ? "0" : match.Groups[3].Value;
                Version version  = new Version($"{match.Groups[1].Value}.{match.Groups[2].Value}.{value3}");
                var     database = match.Groups[4].Value;

                Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    SqlUpdateScript resourceStream = new SqlUpdateScript()
                    {
                        Database = database,
                        Version  = version,
                        SqlText  = reader.ReadToEnd()
                    };

                    yield return(resourceStream);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Runs the specified update script against the database.
 /// Note: Assumes transactions are being handled outside of this method.
 /// </summary>
 /// <param name="adapter">The SQL adapter</param>
 /// <param name="script">The databse update script</param>
 /// <returns>True if the script was executed successfully.  False otherwise.</returns>
 private bool RunDatabaseUpdate(string databaseHost, SqlUpdateScript script)
 {
     // Execute the SQL update script against the defined database host
     using (SqlTextAdapter adapter = new SqlTextAdapter(databaseHost, "Master"))
     {
         UpdateStatus("Updating {0} to version {1}".FormatWith(script.Database, script.Version));
         if (!ExecuteSql(script.SqlText, adapter, true))
         {
             return(false);
         }
     }
     return(true);
 }