private static void SetDatabaseType(dbmgrCommandLineOptions options) { // Specify the database type of either SQL Server or Oracle if (options.DatabaseType != null) { if (options.DatabaseType.Equals(new SQLServerScripts().ShortName, StringComparison.InvariantCultureIgnoreCase)) { _database = new SQLServerScripts(); Log.Logger.Debug("Database Type SQL Server specified on command line."); } else if (options.DatabaseType.Equals(new OracleScripts().ShortName, StringComparison.InvariantCultureIgnoreCase)) { _database = new OracleScripts(); Log.Logger.Debug("Database Type Oracle specified on command line."); } else { throw new NotSupportedException($"Invalid database type option selected {options.DatabaseType}"); } } if (_database == null) { Log.Logger.Information("Database Type not specified on command line; defaulting Database Type to SQL Server."); _database = new SQLServerScripts(); } }
private static int ExecuteCommand(dbmgrCommandLineOptions options) { Stopwatch elapsedTime = Stopwatch.StartNew(); try { // Connect to the database // Step #1 - figure out the provider SetDatabaseType(options); // Step #2 - get the connection parameters string[] replacementParameters = null; string netConnectionString = null; // Do we have standard parameters? if (!string.IsNullOrWhiteSpace(options.DbName)) { // Handle our individual parameters replacementParameters = _database.ParseStandardConnection(options.DbName, options.DbServer, options.DbPort, options.DbUser, options.DbPwd, options.DbOpt1, options.DbOpt2); } else if (!string.IsNullOrWhiteSpace(options.DbFile)) { // Handle our database file try { Log.Logger.Information("Loading standard parameters from file"); string[] fileparams = File.ReadAllLines(options.DbFile); if (fileparams.Length >= 1) { options.DbName = fileparams[0]; } else { options.DbName = null; } if (fileparams.Length >= 2) { options.DbServer = fileparams[1]; } else { options.DbServer = null; } if (fileparams.Length >= 3) { options.DbPort = fileparams[2]; } else { options.DbPort = null; } if (fileparams.Length >= 4) { options.DbUser = fileparams[3]; } else { options.DbUser = null; } if (fileparams.Length >= 5) { options.DbPwd = fileparams[4]; } else { options.DbPwd = null; } if (fileparams.Length >= 6) { options.DbOpt1 = fileparams[5]; } else { options.DbOpt1 = null; } if (fileparams.Length >= 7) { options.DbOpt2 = fileparams[6]; } else { options.DbOpt2 = null; } if (string.IsNullOrWhiteSpace(options.DbName)) { Log.Logger.Error("Format of standard parameter file is incorrect"); return(EXIT_CODE_ARGUMENT_ERROR); } replacementParameters = _database.ParseStandardConnection(options.DbName, options.DbServer, options.DbPort, options.DbUser, options.DbPwd, options.DbOpt1, options.DbOpt2); } catch (FileNotFoundException) { Log.Logger.Error("Unable to find standard parameter file: {0}.", options.DbFile); return(EXIT_CODE_GENERAL_ERROR); } catch (Exception ex) { Log.Logger.Error("Error loading standard parameter file", ex); return(EXIT_CODE_GENERAL_ERROR); } } // if we didn't have standard parameters, let's try provider-specific if (replacementParameters == null) { // Do we have provider-specific connection info? if (!string.IsNullOrWhiteSpace(options.ConnectInfo)) { // Handle the connect info Log.Logger.Information("Replacement parameters parsed from command line"); replacementParameters = _database.ParseProviderConnection(options.ConnectInfo); if (replacementParameters == null || replacementParameters.Count() == 0) { Log.Logger.Error("Unable to parse database replacement parameters"); return(EXIT_CODE_ARGUMENT_ERROR); } } else if (!string.IsNullOrWhiteSpace(options.ConnectInfoFile)) { // We have the database connection in the vault file try { string input = File.ReadAllText(options.ConnectInfoFile); Log.Logger.Information("Connection info read from connection info file"); replacementParameters = _database.ParseProviderConnection(input); if (replacementParameters == null) { // Try another file format where each provider specific item is on one line Log.Logger.Information("Replacement parameters loaded from vault file"); replacementParameters = File.ReadAllLines(options.ConnectInfoFile); int l = 1; foreach (string s in replacementParameters) { Log.Logger.Debug("Replacement parameter #{0}: {1}", l++, s); } } } catch (FileNotFoundException) { Log.Logger.Error("Unable to find connection info file: {0}.", options.ConnectInfoFile); return(EXIT_CODE_GENERAL_ERROR); } catch (Exception ex) { Log.Logger.Error("Error loading connection info file", ex); return(EXIT_CODE_GENERAL_ERROR); } } } // Do we have a connection string already? if (!string.IsNullOrWhiteSpace(options.ConnectString)) { netConnectionString = options.ConnectString; Log.Logger.Information("Connection string override from command line"); } else if (!string.IsNullOrWhiteSpace(options.ConnectStringFile)) { string input = File.ReadAllText(options.ConnectStringFile); if (!string.IsNullOrWhiteSpace(input)) { Log.Logger.Information("Connection string override from file"); netConnectionString = input; } } // Set up the migrator IDatabaseConfiguration config = new NetDatabaseConfiguration(_database.DbConnectionKey); dbmgrDataMigration m = new dbmgrDataMigration(config, _destinationBase, _database, options.ReplacementFile, replacementParameters, netConnectionString); if (options.DryRun) { Log.Logger.Information("Executing in DRY RUN mode. No updates to database will take place."); m.NoDbUpdates = options.DryRun; } // Process the commands that don't need the database if (options.SetupFolders) { return(ProcessSetupCommand(m, options.Blue || options.Green) ? EXIT_CODE_SUCCESS : EXIT_CODE_GENERAL_ERROR); } if (!String.IsNullOrWhiteSpace(options.GenerateDelta)) { return(ProcessGenerateCommand(m, options.GenerateDelta, null)); } // These commands require a connection to the database if (options.TestConnectivity) { return(ProcessConnectCommand(m) ? EXIT_CODE_SUCCESS : EXIT_CODE_GENERAL_ERROR); } if (options.ExtractCurrent) { return(ProcessExtractCurrentCommand(m) ? EXIT_CODE_SUCCESS : EXIT_CODE_GENERAL_ERROR); } if (!string.IsNullOrWhiteSpace(options.ExtractSchema)) { return(ProcessExtractSchemaCommand(m, options.ExtractSchema) ? EXIT_CODE_SUCCESS : EXIT_CODE_GENERAL_ERROR); } // Run the migration if (options.Migrate || options.CreateSchema) { try { // Validate and/or create the schema bool exists = ValidateAndCreateSchema(m, options.NoCreate); if (!exists) { Log.Logger.Error("Cannot continue - unable to validate migration schema!"); return(EXIT_CODE_GENERAL_ERROR); } // Start the migration if (options.Migrate) { if (options.Blue || options.Green) { ProcessBlueGreenMigrateCommand(m, options.Blue); } else { ProcessMigrateCommand(m); } } return(EXIT_CODE_SUCCESS); } catch (Exception nse) { Log.Logger.Error("ERROR: " + nse.Message); return(EXIT_CODE_GENERAL_ERROR); } } Log.Logger.Error($"Unknown command{Environment.NewLine}"); GetUsage(); return(EXIT_CODE_ARGUMENT_ERROR); } catch (Exception e) { Log.Logger.Error(e, "Exiting with an exception"); return(EXIT_CODE_GENERAL_ERROR); } finally { Log.Logger.Information("Finished running in " + elapsedTime.ShowElapsedTime()); } }