/// <summary> /// Performs an install of a database /// </summary> public void Install(InstallSetup setup) { //The connection string must reference an existing database if (!DatabaseServer.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string does not reference a valid database."); } try { UpgradeInstaller.UpgradeDatabase(setup); } catch (InvalidSQLException ex) { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("BEGIN ERROR SQL"); sb.AppendLine(ex.SQL); sb.AppendLine("END ERROR SQL"); sb.AppendLine(); Log.Verbose(sb.ToString()); UpgradeInstaller.LogError(ex, sb.ToString()); throw; } catch (Exception ex) { throw; } }
/// <summary /> private void UIInstall() { if (IdentifyDatabaseConnectionString()) { var setup = new InstallSetup() { ConnectionString = this.Settings.GetPrimaryConnectionString(), InstallStatus = InstallStatusConstants.Upgrade, }; if (this.Action == ActionTypeConstants.Create) { setup.InstallStatus = InstallStatusConstants.Create; UpgradeInstaller.UpgradeDatabase(setup); } else if (this.Action == ActionTypeConstants.Upgrade) { UpgradeInstaller.UpgradeDatabase(setup); } else if (this.Action == ActionTypeConstants.AzureCopy) { UpgradeInstaller.AzureCopyDatabase(this.Settings); } } }
/// <summary> /// Performs an install of a database /// </summary> public void Install(InstallSetup setup) { if (setup.InstallStatus == InstallStatusConstants.Create) { //Connection cannot reference an existing database if (SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string references an existing database."); } //The new database name must be specified if (string.IsNullOrEmpty(setup.NewDatabaseName)) { throw new Exception("A new database name was not specified."); } //The connection string and the new database name must be the same var builder = new SqlConnectionStringBuilder(setup.ConnectionString); if (builder.InitialCatalog.ToLower() != setup.NewDatabaseName.ToLower()) { throw new Exception("A new database name does not match the specified connection string."); } SqlServers.CreateDatabase(setup); } else if (setup.InstallStatus == InstallStatusConstants.Upgrade) { //The connection string must reference an existing database if (!SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string does not reference a valid database."); } } try { UpgradeInstaller.UpgradeDatabase(setup); } catch (InvalidSQLException ex) { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("BEGIN ERROR SQL"); sb.AppendLine(ex.SQL); sb.AppendLine("END ERROR SQL"); sb.AppendLine(); System.Diagnostics.Trace.WriteLine(sb.ToString()); UpgradeInstaller.LogError(ex, sb.ToString()); throw; } catch (Exception ex) { throw; } }
/// <summary /> private void UIInstall(InstallSetup setup) { if (IdentifyDatabaseConnectionString(setup)) { setup.ConnectionString = this.Settings.GetPrimaryConnectionString(); setup.InstallStatus = InstallStatusConstants.Upgrade; if (this.Action == ActionTypeConstants.Create) { setup.InstallStatus = InstallStatusConstants.Create; UpgradeInstaller.UpgradeDatabase(setup); } else if (this.Action == ActionTypeConstants.Upgrade) { UpgradeInstaller.UpgradeDatabase(setup); } } }
/// <summary> /// Performs an install of a database /// </summary> public void Install(InstallSetup setup) { if (setup.InstallStatus == InstallStatusConstants.Create) { //Conection cannot reference an existing database if (SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string references an existing database."); } //The new database name must be specified if (string.IsNullOrEmpty(setup.NewDatabaseName)) { throw new Exception("A new database name was not specified."); } //The connection string and the new database name must be the same var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(setup.ConnectionString); if (builder.InitialCatalog.ToLower() != setup.NewDatabaseName.ToLower()) { throw new Exception("A new database name does not match the specified connection string."); } SqlServers.CreateDatabase(setup); } else if (setup.InstallStatus == InstallStatusConstants.Upgrade) { //The connection string must reference an existing database if (!SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string does not reference a valid database."); } } UpgradeInstaller.UpgradeDatabase(setup); }
public void Run(InstallSettings settings) { //STEPS TO COPY DATABASE TO AZURE //1. Verify that target is a blank database //2. Execute only tables schemas with no defaults, relations, etc //3. Copy data with BCP one table at a time //4. Run full installer on the target database //1. Verify that target is a blank database if (!this.TargetIsBlank(settings)) { throw new Exception("The target database must be empty!"); } //2. Execute only tables schemas and PK with no defaults, relations, etc Assembly assem = Assembly.GetExecutingAssembly(); string[] resourceNames = assem.GetManifestResourceNames(); var resourceName = resourceNames.FirstOrDefault(x => x.EndsWith(".Create_Scripts.Generated.CreateSchema.sql")); if (string.IsNullOrEmpty(resourceName)) { throw new Exception("Could not find the 'CreateSchema.sql' resource!"); } var scripts = SqlServers.ReadSQLFileSectionsFromResource(resourceName, new InstallSetup()); SqlConnection connection = null; try { connection = new SqlConnection(settings.GetCloudConnectionString()); connection.Open(); ////Create version table //var sb = new StringBuilder(); //sb.AppendLine("if not exists(select * from sysobjects where name = '__nhydrateschema' and xtype = 'U')"); //sb.AppendLine("BEGIN"); //sb.AppendLine("CREATE TABLE [__nhydrateschema] ("); //sb.AppendLine("[dbVersion] [varchar] (50) NOT NULL,"); //sb.AppendLine("[LastUpdate] [datetime] NOT NULL,"); //sb.AppendLine("[ModelKey] [uniqueidentifier] NOT NULL,"); //sb.AppendLine("[History] [text] NOT NULL"); //sb.AppendLine(")"); //sb.AppendLine("--PRIMARY KEY FOR TABLE"); //sb.AppendLine("if not exists(select * from sysobjects where name = '__pk__nhydrateschema' and xtype = 'PK')"); //sb.AppendLine("ALTER TABLE [__nhydrateschema] WITH NOCHECK ADD CONSTRAINT [__pk__nhydrateschema] PRIMARY KEY CLUSTERED ([ModelKey])"); //sb.AppendLine("END"); //var command2 = new SqlCommand(sb.ToString(), connection); //command2.ExecuteNonQuery(); foreach (string sql in scripts) { if ( sql.Contains("--CREATE TABLE") || sql.Contains("--CREATE AUDIT TABLE") || sql.StartsWith("--APPEND AUDIT") || sql.StartsWith("--PRIMARY KEY FOR TABLE")) { var command = new SqlCommand(sql, connection); command.ExecuteNonQuery(); } } } catch (Exception ex) { throw; } finally { if (connection != null) { connection.Close(); } } //3. Copy data with BCP one table at a time this.CopyData(settings); //4. Run full installer on the target database var setup = new InstallSetup() { ConnectionString = settings.GetCloudConnectionString(), InstallStatus = InstallStatusConstants.Upgrade, }; UpgradeInstaller.UpgradeDatabase(setup); }