public void SentinelServiceDB_DeployDB() { SqlTestDatabase dbTest; Package dbPackage = null; DBPackageInstaller dbInstaller; DBInstallParams dbParams; DBInstallResult result; using (dbTest = SqlTestDatabase.Create()) { SqlConnectionInfo conInfo; SqlContext ctx = null; SqlCommand cmd; DataTable dt; try { // Deploy to a non-existent database dbPackage = new Package(EnvironmentVars.Expand("$(LT_BUILD)\\LillTek.SentinelService.dbpack")); dbParams = new DBInstallParams("SentinelService", dbTest.ConnectionInfo.Database); dbInstaller = new DBPackageInstaller(dbPackage); result = dbInstaller.Install(dbParams); Assert.AreEqual(DBInstallResult.Installed, result); conInfo = SqlConnectionInfo.Parse(dbInstaller.ConnectionString); ctx = new SqlContext(conInfo); ctx.Open(); cmd = ctx.CreateSPCall("GetProductInfo"); dt = ctx.ExecuteTable(cmd); Assert.AreEqual(1, dt.Rows.Count); cmd = ctx.CreateSPCall("Ping"); dt = ctx.ExecuteTable(cmd); Assert.AreEqual(1, dt.Rows.Count); Assert.AreEqual("OK", SqlHelper.AsString(dt.Rows[0]["STATUS"])); ctx.Close(); ctx = null; // Deploy again and we should see that the database is up-to-date. SqlConnection.ClearAllPools(); result = dbInstaller.Install(dbParams); Assert.AreEqual(DBInstallResult.UpToDate, result); } finally { if (dbPackage != null) { dbPackage.Close(); } if (ctx != null) { ctx.Close(); } } } }
/// <summary> /// Implemnts -install mode. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="packagePath">Path to the database package.</param> private static void Install(string[] args, string packagePath) { DBPackageInstaller installer; Package package = null; DBInstallResult result; string configFile = null; string conStringKey = null; string conStringMacro = null; string defDB = null; Config config; string conString = null; StreamReader reader; FileStream fs; foreach (string arg in args) { if (arg.StartsWith("-config:")) { configFile = arg.Substring(8); } else if (arg.StartsWith("-setting:")) { conStringKey = arg.Substring(9); } else if (arg.StartsWith("-macro:")) { conStringMacro = arg.Substring(7); } else if (arg.StartsWith("-defdb:")) { defDB = arg.Substring(7); } } if (defDB == null) { defDB = string.Empty; } try { if (configFile != null && conStringKey != null) { reader = new StreamReader(configFile, Helper.AnsiEncoding); try { config = new Config(null, reader); } finally { reader.Close(); } conString = config.Get(conStringKey, (string)null); if (conString == null || conString.StartsWith("$(")) { conString = null; } } if (conString == null) { conString = "server=localhost;database=" + defDB + ";uid=;pwd="; } package = new Package(packagePath); installer = new DBPackageInstaller(package); result = installer.InstallWizard(conString); if (result != DBInstallResult.Installed && result != DBInstallResult.Upgraded) { return; } conString = installer.ConnectionString; if (configFile != null && conStringMacro != null) { fs = new FileStream(configFile, FileMode.Open, FileAccess.ReadWrite); try { Config.EditMacro(fs, Helper.AnsiEncoding, conStringMacro, conString); } finally { fs.Close(); } } } catch (Exception e) { MessageBox.Show(e.Message, Program.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (package != null) { package.Close(); } } }