public static int Run(string[] args) { var cmd = args[0]; try { switch (cmd) { case "migrate": MigrateCommand.Run(); break; case "-h": case "--help": Help(); break; default: Help(); return(1); } } catch (Exception e) { Console.Error.WriteLine(e.Message); return(1); } return(0); }
private void MigrateProject(string projectDirectory) { var result = MigrateCommand.Run(new [] { "-p", projectDirectory }); result.Should().Be(0); }
private void MigrateProject(string[] migrateArgs) { var result = MigrateCommand.Run(migrateArgs); result.Should().Be(0); }
public void when_migrate_command_is_executed_then_migration_is_run() { var listCommand = new MigrateCommand(MockSecureConsole.Object, _mockMigrator.Object); listCommand.Execute(_requiredMigrateCommandArguments); _mockMigrator.Verify(m => m.Migrate(It.IsAny<DatabaseConnectionInfo>(), It.IsAny<MigrationInfo>())); }
public void when_migrate_command_is_executed_then_migration_is_run() { var listCommand = new MigrateCommand(MockSecureConsole.Object, _mockMigrator.Object); listCommand.Execute(_requiredMigrateCommandArguments); _mockMigrator.Verify(m => m.Migrate(It.IsAny <DatabaseConnectionInfo>(), It.IsAny <MigrationInfo>())); }
public void It_migrates_root_project_and_skips_references(string projectName) { var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.SkipRefsTest").Path; FixUpProjectJsons(projectDirectory); MigrateCommand.Run(new [] { Path.Combine(projectDirectory, projectName), "--skip-project-references" }).Should().Be(0); VerifyMigration(Enumerable.Repeat(projectName, 1), projectDirectory); }
public void when_no_arguments_have_been_specified_then_the_list_command_help_text_is_displayed() { var stringWriter = new StringWriter(); typeof(ParserSettings).GetProperty("Consumed", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(Parser.Default.Settings, false); Parser.Default.Settings.HelpWriter = stringWriter; var listCommand = new MigrateCommand(MockSecureConsole.Object, _mockMigrator.Object); listCommand.Execute(new string[] { }); Assert.That(stringWriter, Is.Not.Null.Or.Empty); }
public void It_migrates_given_project_json() { var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path; FixUpProjectJsons(projectDirectory); var project = Path.Combine(projectDirectory, "ProjectA", "project.json"); MigrateCommand.Run(new [] { project }).Should().Be(0); string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE" }; VerifyMigration(migratedProjects, projectDirectory); }
public void Setup() { _commandArgs = new MigrateCommandArgs(); _commandArgs.Connection = TestConnectionString; _mockLog = new MockLog1(); _mockMigrationScripts = new List <IMigrationScriptFile>(); _mockMigrationDir = new Mock <IMigrationDirectory>(); _mockMigrationDir.Setup(x => x.GetScripts()).Returns(() => _mockMigrationScripts); _migrateCommand = new MigrateCommand(_mockMigrationDir.Object); _migrateCommand.Log = _mockLog; // setup the mock migration scripts var mockScript1 = new Mock <IMigrationScriptFile>(); mockScript1.SetupGet(x => x.Version).Returns(1); mockScript1.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( @"CREATE TABLE [TestTable] (Id INT NOT NULL) GO INSERT INTO [TestTable] (Id) VALUES (1)", @"DROP TABLE [TestTable]")); _mockMigrationScripts.Add(mockScript1.Object); var mockScript2 = new Mock <IMigrationScriptFile>(); mockScript2.SetupGet(x => x.Version).Returns(2); mockScript2.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [TestTable] (Id) VALUES (2)", "DELETE FROM [TestTable] WHERE Id = 2")); _mockMigrationScripts.Add(mockScript2.Object); // setup a migration script that throws an exception during Setup, but don't add it to the scripts collection _mockScriptWithBadSetup = new Mock <IMigrationScriptFile>(); _mockScriptWithBadSetup.SetupGet(x => x.Version).Returns(3); _mockScriptWithBadSetup.SetupGet(x => x.FilePath).Returns("C:\\3_my_bad_script.sql"); _mockScriptWithBadSetup.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [NonExistantTable] (Id) VALUES (1)", null)); // setup a migration script that throws an exception during Teardown, but don't add it to the scripts collection _mockScriptWithBadTeardown = new Mock <IMigrationScriptFile>(); _mockScriptWithBadTeardown.SetupGet(x => x.Version).Returns(4); _mockScriptWithBadTeardown.SetupGet(x => x.FilePath).Returns("C:\\4_my_bad_script.sql"); _mockScriptWithBadTeardown.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [TestTable] (Id) VALUES (4)", "DELETE FROM [NonExistantTable] WHERE Id = 4")); CreateDatabase(); }
public void It_migrates_all_projects_in_given_directory(bool skipRefs) { var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"MigrateDirectory.SkipRefs.{skipRefs}").Path; FixUpProjectJsons(projectDirectory); if (skipRefs) { MigrateCommand.Run(new [] { projectDirectory, "--skip-project-references" }).Should().Be(0); } else { MigrateCommand.Run(new [] { projectDirectory }).Should().Be(0); } string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE", "ProjectF", "ProjectG" }; VerifyMigration(migratedProjects, projectDirectory); }
protected TableCommand(MigrateCommand parent, string tableName) : this((Command)parent, tableName) { }
internal SchemaCollection(MigrateCommand command) { _command = command; }
internal TableCollection(MigrateCommand command) { _command = command; }
public AlterTableCommand(MigrateCommand parent, string tableName) : base(parent, tableName) { }
public static void Migrate(Databases db) { MigrateCommand.Migrate(db); }
internal TableCollection(MigrateCommand migrateCommand) { _migrateCommand = migrateCommand; }
static void Main(string[] args) { var app = new CommandLineApplication(); app.Name = "ConsoleApp"; app.HelpOption("-?|-h|--help"); app.OnExecute(() => { app.ShowHelp(); return(0); }); app.Command("fetcher", (command) => { command.Description = "Execute selected Fetcher jobs. All by default."; command.HelpOption("-?|-h|--help"); var debugFlag = command.Option("-d|--debug", "Skip actual job execution.", CommandOptionType.NoValue); var printDependencyTreeFlag = command.Option("-p|--print-dependency-tree", "Print a Graphviz Dot file of the dependency tree and exit.", CommandOptionType.NoValue); var listAvailableJobsFlag = command.Option("-l|--list-available-jobs", "Print the list of available jobs and exit.", CommandOptionType.NoValue); var selectedJobNames = command.Option("-j|--job", "Filter jobs by name.", CommandOptionType.MultipleValue); var selectedJobScope = command.Option("-s|--scope", "Filter jobs by scope: 'YouTube', 'AdWords', 'Facebook' or 'Instagram'.", CommandOptionType.SingleValue); var fetchAll = command.Option("-a|--fetch-all", "Fetch daily metrics until completion", CommandOptionType.NoValue); var maxEntities = command.Option("-m|--max-entities", "Number of entities to fetch", CommandOptionType.SingleValue); var ignoreApi = command.Option("-i|--ignore-api", "Stops execution if request is not found on cache", CommandOptionType.NoValue); command.OnExecute(() => { var configuration = new JobConfiguration { IgnoreAPI = ignoreApi.HasValue(), MaxEntities = maxEntities.HasValue() ? Convert.ToInt32(maxEntities.Value()) : 0, Paginate = !fetchAll.HasValue(), }; var jobType = JobType.Fetcher; var jobScope = JobScope.All; if (selectedJobScope.HasValue()) { if (!Enum.TryParse <JobScope>(selectedJobScope.Value(), out jobScope)) { Console.WriteLine("Invalid job scope. Possible values are:"); foreach (var x in Enum.GetNames(typeof(JobScope))) { Console.WriteLine($"\t{x}"); } return(1); } } var jobNames = selectedJobNames.HasValue() ? selectedJobNames.Values : new List <string> { "All" }; if (printDependencyTreeFlag.HasValue()) { RunJobsCommand.PrintDependencyTree(jobType, jobScope, jobNames); } else if (listAvailableJobsFlag.HasValue()) { RunJobsCommand.ListAvailableJobs(jobType, jobScope, jobNames); } else if (selectedJobScope.Value() == "Instagram") { return(RunJobsCommand.RunInstagramJobs(jobType, jobScope, configuration, debugFlag.HasValue())); } else { return(RunJobsCommand.RunJobs(jobType, jobScope, jobNames, configuration, debugFlag.HasValue())); } return(0); }); }); app.Command("migrate", (command) => { command.Description = "Apply current migrations. All by default."; command.HelpOption("-?|-h|--help"); var lake = command.Option("--data-lake", "Migrate data-lake databases", CommandOptionType.NoValue); var facebook = command.Option("--facebook-lake", "Migrate facebook-lake database", CommandOptionType.NoValue); command.OnExecute(() => { if (lake.HasValue()) { MigrateCommand.MigrateDataLake(); } else if (facebook.HasValue()) { MigrateCommand.MigrateFacebook(); } else { MigrateCommand.MigrateDataLake(); MigrateCommand.MigrateFacebook(); } return(0); }); }); app.Command("zip-facebook-cache", (command) => { command.Description = "Creates zip file with current facebook cache. Useful for recording test data."; command.HelpOption("-?|-h|--help"); var zipDir = command.Argument("cache-dir", "Path to the cache directory"); var zipName = command.Argument("zip-name", "Name of zip file to be created"); command.OnExecute(() => { if (zipDir.Value == null || zipName.Value == null) { Console.WriteLine(command.GetHelpText()); return(1); } ZipFacebooCacheCommand.ZipCache(zipDir.Value, zipName.Value); Console.WriteLine("Successfully zipped Facebook cache."); return(0); }); }); app.Command("error-report", (command) => { command.Description = "Report on errors occurring during job execution"; command.HelpOption("-?|-h|--help"); var since = command.Option("-s|--since", "Start date from which to aggregate logs", CommandOptionType.SingleValue); command.OnExecute(() => { var startDate = since.HasValue() ? DateTime.Parse(since.Value()) : DateTime.UtcNow.AddDays(-30); ErrorReportCommand.Report(startDate); return(0); }); }); app.Command("check-data-lake", (command) => { command.Description = "Report on properties of Data Lake table"; command.HelpOption("-?|-h|--help"); var table = command.Option("-t|--table", "Table to check.", CommandOptionType.SingleValue); command.OnExecute(() => { var allTables = DataLakeIntrospection.GetIValidityRangeEntities().ToDictionary(x => x.Relational().TableName, x => x); var tables = ApplyTableFilter(table, allTables); if (tables.Any()) { ContinuityReportCommand.Report(tables); } else { Console.WriteLine("Available tables:"); foreach (var t in allTables.OrderBy(x => x.Key)) { Console.WriteLine(t.Key); } } return(0); }); }); app.Command("export", (command) => { command.Description = "Export Data Lake tables into a file."; command.HelpOption("-?|-h|--help"); var fileType = command.Option("-t|--type", "Select file type: 'csv' or 'json'.", CommandOptionType.SingleValue); var source = command.Option("-s|--source", "Select platform to export: 'facebook', 'youtube' or 'adwords'. All by default", CommandOptionType.SingleValue); var limit = command.Option("-l|--limit", "Select a limit of rows. 10 by default.", CommandOptionType.SingleValue); var platforms = new List <string> { "facebook", "youtube", "adwords", "" }; command.OnExecute(() => { var selectedFileType = fileType.HasValue() ? fileType.Value() : "json"; var selectedPlatform = source.HasValue() ? source.Value() : ""; var queryLimit = limit.HasValue() ? Convert.ToInt32(limit.Value()) : 100; if (!platforms.Contains(selectedPlatform)) { Console.WriteLine("Invalid source!\n\nList sources:"); foreach (var s in platforms) { Console.WriteLine($"\t{s}"); } Environment.Exit(1); } ExportData.QueryMetrics(selectedFileType, selectedPlatform, queryLimit); return(0); }); }); Log.Logger = LoggerFactory.GetFacebookLogger(); app.Execute(args); Log.CloseAndFlush(); }
public static void MigrateFacebook() { MigrateCommand.MigrateDataLake(); MigrateCommand.MigrateFacebook(); }
static void Main(string[] args) { var app = new CommandLineApplication(); app.Name = "ConsoleApp"; app.HelpOption("-?|-h|--help"); app.OnExecute(() => { app.ShowHelp(); return(0); }); app.Command("jobs", (command) => { command.Description = "Execute selected jobs. All by default."; command.HelpOption("-?|-h|--help"); var debugFlag = command.Option("-d|--debug", "Skip actual job execution.", CommandOptionType.NoValue); var printDependencyTreeFlag = command.Option("-p|--print-dependency-tree", "Print a Graphviz Dot file of the dependency tree and exit.", CommandOptionType.NoValue); var listAvailableJobsFlag = command.Option("-l|--list-available-jobs", "Print the list of available jobs and exit.", CommandOptionType.NoValue); var selectedJobNames = command.Option("-j|--job", "Filter jobs by name.", CommandOptionType.MultipleValue); var selectedJobType = command.Option("-t|--type", "Filter jobs by type: 'Fetcher' or 'Transformation'.", CommandOptionType.SingleValue); var selectedJobScope = command.Option("-s|--scope", "Filter jobs by scope: 'YouTube', 'AdWords', 'Facebook' or 'Application'.", CommandOptionType.SingleValue); var fetchAll = command.Option("-a|--fetch-all", "Fetch daily metrics until completion", CommandOptionType.NoValue); var maxEntities = command.Option("-m|--max-entities", "Number of entities to fetch", CommandOptionType.SingleValue); var ignoreApi = command.Option("-i|--ignore-api", "Stops execution if request is not found on cache", CommandOptionType.NoValue); command.OnExecute(() => { var configuration = new JobConfiguration { IgnoreAPI = ignoreApi.HasValue(), MaxEntities = maxEntities.HasValue() ? Convert.ToInt32(maxEntities.Value()) : 0, Paginate = !fetchAll.HasValue(), }; var jobType = JobType.All; if (selectedJobType.HasValue()) { if (!Enum.TryParse <JobType>(selectedJobType.Value(), out jobType)) { Console.WriteLine("Invalid job type. Possible values are:"); foreach (var x in Enum.GetNames(typeof(JobType))) { Console.WriteLine($"\t{x}"); } return(1); } } var jobScope = JobScope.All; if (selectedJobScope.HasValue()) { if (!Enum.TryParse <JobScope>(selectedJobScope.Value(), out jobScope)) { Console.WriteLine("Invalid job scope. Possible values are:"); foreach (var x in Enum.GetNames(typeof(JobScope))) { Console.WriteLine($"\t{x}"); } return(1); } } var jobNames = selectedJobNames.HasValue() ? selectedJobNames.Values : new List <string>(); if (printDependencyTreeFlag.HasValue()) { RunJobsCommand.PrintDependencyTree(jobType, jobScope, jobNames); } else if (listAvailableJobsFlag.HasValue()) { RunJobsCommand.ListAvalableJobs(jobType, jobScope, jobNames); } else { return(RunJobsCommand.RunJobs(jobType, jobScope, jobNames, configuration, debugFlag.HasValue())); } return(0); }); }); app.Command("migrate", (command) => { command.Description = "Apply current migrations"; command.HelpOption("-?|-h|--help"); var lake = command.Option("--data-lake", "Migrate data-lake databases", CommandOptionType.NoValue); var analytics = command.Option("--application", "Migrate application database", CommandOptionType.NoValue); command.OnExecute(() => { if (!lake.HasValue() && !analytics.HasValue()) { Console.WriteLine("No database selected"); return(1); } if (lake.HasValue()) { MigrateCommand.MigrateDataLake(); } if (analytics.HasValue()) { MigrateCommand.MigrateApplication(); } return(0); }); }); app.Command("init-facebook-lake", (command) => { command.Description = "Initialize the Facebook Data Lake database"; command.HelpOption("-?|-h|--help"); command.OnExecute(() => { MigrateCommand.MigrateFacebook(); return(0); }); }); app.Command("zip-facebook-cache", (command) => { command.Description = "Creates zip file with current facebook cache. Useful for recording test data."; command.HelpOption("-?|-h|--help"); var zipDir = command.Argument("cache-dir", "Path to the cache directory"); var zipName = command.Argument("zip-name", "Name of zip file to be created"); command.OnExecute(() => { if (zipDir.Value == null || zipName.Value == null) { Console.WriteLine(command.GetHelpText()); return(1); } ZipFacebooCacheCommand.ZipCache(zipDir.Value, zipName.Value); return(0); }); }); app.Command("error-report", (command) => { command.Description = "Report on errors occurring during job execution"; command.HelpOption("-?|-h|--help"); var since = command.Option("-s|--since", "Start date from which to aggregate logs", CommandOptionType.SingleValue); command.OnExecute(() => { var startDate = since.HasValue() ? DateTime.Parse(since.Value()) : DateTime.UtcNow.AddDays(-30); ErrorReportCommand.Report(startDate); return(0); }); }); app.Command("check-data-lake", (command) => { command.Description = "Report on properties of Data Lake table"; command.HelpOption("-?|-h|--help"); var table = command.Option("-t|--table", "Table to check.", CommandOptionType.SingleValue); command.OnExecute(() => { var allTables = DataLakeIntrospection.GetIValidityRangeEntities().ToDictionary(x => x.Relational().TableName, x => x); var tables = ApplyTableFilter(table, allTables); if (tables.Any()) { ContinuityReportCommand.Report(tables); } else { Console.WriteLine("Available tables:"); foreach (var t in allTables.OrderBy(x => x.Key)) { Console.WriteLine(t.Key); } } return(0); }); }); app.Command("check-ap", (command) => { command.Description = "Report on properties of Application Database"; command.HelpOption("-?|-h|--help"); command.OnExecute(() => { ApplicationConsistencyReport.Report(); return(0); }); }); app.Command("application-reset", (command) => { command.Description = "Reset application database table"; command.HelpOption("-?|-h|--help"); var table = command.Option("-t|--table", "Table to reset.", CommandOptionType.SingleValue); command.OnExecute(() => { var allTables = ApplicationResetCommand.GetResetableTables().ToDictionary(x => x.TableName, x => x.Entity); var tables = ApplyTableFilter(table, allTables); if (tables.Any()) { ApplicationResetCommand.ResetTables(tables); } else { Console.WriteLine("Available tables:"); foreach (var t in allTables.OrderBy(x => x.Key)) { Console.WriteLine(t.Key); } } return(0); }); }); app.Command("archive-old-videos", (command) => { command.Description = "Archieves all videos published before a specific date (defaults to 2015-06-01)."; command.HelpOption("-?|-h|--help"); var dateOption = command.Option("-d|--date", "Table to reset.", CommandOptionType.SingleValue); command.OnExecute(() => { var date = dateOption.HasValue() ? DateTime.Parse(dateOption.Value()) : new DateTime(2015, 6, 1); ArchiveVideosCommand.ArchiveVideosPublishedBefore(date); return(0); }); }); app.Command("check-health-and-send-email", (command) => { command.Description = "Check error in system and send a email if find some"; command.HelpOption("-?|-h|--help"); var forced = command.Option("-f|--force-send-email", "Force to send a report e-mail", CommandOptionType.NoValue); IConfiguration Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var mailConfiguration = Configuration.GetSection("MailService"); var mailServiceKey = mailConfiguration.GetValue <string>("MailgunKey"); var emails = new List <string>(); mailConfiguration.GetSection("EmailsOnCall").Bind(emails); command.OnExecute(() => { CheckSystemHealthAndReport.Run(emails.ToArray(), mailServiceKey, DateTime.UtcNow.AddDays(-7), forced.HasValue()); return(0); }); }); Log.Logger = LoggerFactory.GetFacebookLogger(); app.Execute(args); Log.CloseAndFlush(); }
public void Setup() { _commandArgs = new MigrateCommandArgs(); _commandArgs.Connection = TestConnectionString; _mockLog = new MockLog1(); _mockMigrationScripts = new List<IMigrationScriptFile>(); _mockMigrationDir = new Mock<IMigrationDirectory>(); _mockMigrationDir.Setup(x => x.GetScripts()).Returns(() => _mockMigrationScripts); _migrateCommand = new MigrateCommand(_mockMigrationDir.Object); _migrateCommand.Log = _mockLog; // setup the mock migration scripts var mockScript1 = new Mock<IMigrationScriptFile>(); mockScript1.SetupGet(x => x.Version).Returns(1); mockScript1.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( @"CREATE TABLE [TestTable] (Id INT NOT NULL) GO INSERT INTO [TestTable] (Id) VALUES (1)", @"DROP TABLE [TestTable]")); _mockMigrationScripts.Add(mockScript1.Object); var mockScript2 = new Mock<IMigrationScriptFile>(); mockScript2.SetupGet(x => x.Version).Returns(2); mockScript2.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [TestTable] (Id) VALUES (2)", "DELETE FROM [TestTable] WHERE Id = 2")); _mockMigrationScripts.Add(mockScript2.Object); // setup a migration script that throws an exception during Setup, but don't add it to the scripts collection _mockScriptWithBadSetup = new Mock<IMigrationScriptFile>(); _mockScriptWithBadSetup.SetupGet(x => x.Version).Returns(3); _mockScriptWithBadSetup.SetupGet(x => x.FilePath).Returns("C:\\3_my_bad_script.sql"); _mockScriptWithBadSetup.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [NonExistantTable] (Id) VALUES (1)", null)); // setup a migration script that throws an exception during Teardown, but don't add it to the scripts collection _mockScriptWithBadTeardown = new Mock<IMigrationScriptFile>(); _mockScriptWithBadTeardown.SetupGet(x => x.Version).Returns(4); _mockScriptWithBadTeardown.SetupGet(x => x.FilePath).Returns("C:\\4_my_bad_script.sql"); _mockScriptWithBadTeardown.Setup(x => x.Read()).Returns(() => new MigrationScriptContents( "INSERT INTO [TestTable] (Id) VALUES (4)", "DELETE FROM [NonExistantTable] WHERE Id = 4")); CreateDatabase(); }