コード例 #1
0
ファイル: DatabaseInstaller.cs プロジェクト: nicknow/nHydrate
        /// <summary>
        /// Returns the upgrade script for the specified database
        /// </summary>
        /// <param name="connectionString">The database connection string</param>
        /// <param name="skipSections">A list of script sections to skip when executing</param>
        public string GetUpgradeScript(string connectionString, IEnumerable <string> skipSections)
        {
            var setup = new InstallSetup();

            setup.ConnectionString = connectionString;
            setup.SkipSections     = skipSections;
            return(UpgradeInstaller.GetUpgradeScript(setup));
        }
コード例 #2
0
ファイル: DatabaseInstaller.cs プロジェクト: nicknow/nHydrate
        /// <summary>
        /// Performs an install of a database
        /// </summary>
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            var commandParams = GetCommandLineParameters();

            if (PrintHelp(commandParams))
            {
                return;
            }

            if (commandParams.Count > 0)
            {
                var setup = new InstallSetup();
                setup.ConnectionString       = GetAppDbString(commandParams);
                setup.MasterConnectionString = GetMasterDbConnectionString(commandParams);
                if (GetUpgradeDbSetting(commandParams, setup.IsUpgrade))
                {
                    setup.InstallStatus = InstallStatusConstants.Upgrade;
                }
                if (commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key)))
                {
                    setup.InstallStatus = InstallStatusConstants.Create;
                }

                if (commandParams.Any(x => PARAMKEYS_UPGRADE.Contains(x.Key)) && commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key)))
                {
                    throw new Exception("You cannot specify both the create and update action.");
                }
                if (commandParams.Count(x => PARAMKEYS_NEWNAME.Contains(x.Key)) > 1)
                {
                    throw new Exception("The new database name was specified more than once.");
                }
                if (commandParams.Count(x => PARAMKEYS_MASTERDB.Contains(x.Key)) > 1)
                {
                    throw new Exception("The master database connection string was specified more than once.");
                }
                if (commandParams.Count(x => PARAMKEYS_APPDB.Contains(x.Key)) > 1)
                {
                    throw new Exception("The connection string was specified more than once.");
                }

                //Determine if calling as a script generator
                if (commandParams.ContainsKey(PARAMKEYS_SCRIPT))
                {
                    var scriptAction = commandParams[PARAMKEYS_SCRIPT].ToLower();
                    switch (scriptAction)
                    {
                    case "versioned":
                    case "unversioned":
                    case "create":
                        break;

                    default:
                        throw new Exception("The script action must be 'create', 'versioned', or 'unversioned'.");
                    }

                    if (!commandParams.ContainsKey(PARAMKEYS_SCRIPTFILE))
                    {
                        throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter must be set for script generation.");
                    }

                    var dumpFile = commandParams[PARAMKEYS_SCRIPTFILE];
                    if (!IsValidFileName(dumpFile))
                    {
                        throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' is not valid.");
                    }

                    var fileCreate = true;
                    if (commandParams.ContainsKey(PARAMKEYS_SCRIPTFILEACTION) &&
                        (commandParams[PARAMKEYS_SCRIPTFILEACTION] + string.Empty) == "append")
                    {
                        fileCreate = false;
                    }

                    if (File.Exists(dumpFile) && fileCreate)
                    {
                        File.Delete(dumpFile);
                        System.Threading.Thread.Sleep(500);
                    }

                    switch (scriptAction)
                    {
                    case "versioned":
                        if (!commandParams.ContainsKey(PARAMKEYS_DBVERSION))
                        {
                            throw new Exception("Generation of versioned scripts requires a '" + PARAMKEYS_DBVERSION + "' parameter.");
                        }

                        if (!GeneratedVersion.IsValid(commandParams[PARAMKEYS_DBVERSION]))
                        {
                            throw new Exception("The '" + PARAMKEYS_DBVERSION + "' parameter is not valid.");
                        }

                        Console.WriteLine("Generate Script Started");
                        setup.InstallStatus = InstallStatusConstants.Upgrade;
                        setup.Version       = new GeneratedVersion(commandParams[PARAMKEYS_DBVERSION]);
                        File.AppendAllText(dumpFile, UpgradeInstaller.GetUpgradeScript(setup));
                        Console.WriteLine("Generated Create Script");
                        break;

                    case "unversioned":
                        if (commandParams.ContainsKey(PARAMKEYS_DBVERSION))
                        {
                            throw new Exception("Generation of unversioned scripts cannot use a '" + PARAMKEYS_DBVERSION + "' parameter.");
                        }

                        Console.WriteLine("Generate Script Started");
                        setup.InstallStatus = InstallStatusConstants.Upgrade;
                        setup.Version       = UpgradeInstaller._def_Version;
                        File.AppendAllText(dumpFile, UpgradeInstaller.GetUpgradeScript(setup));
                        Console.WriteLine("Generated Create Script");
                        break;

                    case "create":
                        Console.WriteLine("Generate Script Started");
                        setup.InstallStatus = InstallStatusConstants.Create;
                        setup.Version       = new GeneratedVersion(-1, -1, -1, -1, -1);
                        File.AppendAllText(dumpFile, UpgradeInstaller.GetUpgradeScript(setup));
                        Console.WriteLine("Generated Create Script");
                        break;
                    }

                    return;
                }

                setup.NewDatabaseName = commandParams.Where(x => PARAMKEYS_NEWNAME.Contains(x.Key)).Select(x => x.Value).FirstOrDefault();
                Install(setup);
            }
            else
            {
                UIInstall();
            }
        }