/// <summary> /// Migrates the specified file name and path. /// <remarks>This method is called directly when performing integration tests.</remarks> /// </summary> /// <param name="fileNameAndPath">The file name and path.</param> /// <param name="withBackup">if set to <c>true</c> [with backup].</param> /// <param name="withAnnouncer">if set to <c>true</c> [with announcer].</param> public static void Migrate(string fileNameAndPath, bool withBackup = false, bool withAnnouncer = false) { var currentDb = new FileInfo(fileNameAndPath); var backupDb = string.Empty; if (withBackup) { Backup(currentDb); } var emptyAnnouncers = new Announcer[] { }; var consoleAnnouncer = new Announcer[] { new ConsoleAnnouncer() }; var announcerToProvide = new CompositeAnnouncer(withAnnouncer ? consoleAnnouncer : emptyAnnouncers); var ctx = new RunnerContext(announcerToProvide) { ApplicationContext = string.Empty, Database = "sqlite", Connection = string.Format(CONNECTION_STRING_FORMAT, fileNameAndPath).NormalizePathSeparator(), Targets = new[] { "Drey.Configuration" } }; try { var executor = new TaskExecutor(ctx); executor.Execute(); } catch (Exception) { if (withBackup) { currentDb.Delete(); if (!string.IsNullOrWhiteSpace(backupDb)) { File.Copy(backupDb, currentDb.FullName); } } throw; } }
public override bool Execute() { if (string.IsNullOrEmpty(databaseType)) { Log.LogError("You must specify a database type. i.e. mysql or sqlserver"); return false; } if (Targets == null || Targets.Length == 0) { Log.LogError("You must specify a migration assemblies "); return false; } IAnnouncer announcer = new ConsoleAnnouncer { ShowElapsedTime = Verbose, ShowSql = Verbose }; StreamWriter outputWriter = null; if (Output) { if (string.IsNullOrEmpty(OutputFilename)) OutputFilename = Path.GetFileName(Target) + ".sql"; outputWriter = new StreamWriter(OutputFilename); var fileAnnouncer = new TextWriterAnnouncer(outputWriter) { ShowElapsedTime = false, ShowSql = true }; announcer = new CompositeAnnouncer(announcer, fileAnnouncer); } Log.LogMessage(MessageImportance.Low, "Creating Context"); var runnerContext = new RunnerContext(announcer) { ApplicationContext = ApplicationContext, Database = databaseType, Connection = Connection, ConnectionStringConfigPath = ConnectionStringConfigPath, Targets = Targets, PreviewOnly = PreviewOnly, Namespace = Namespace, NestedNamespaces = Nested, Task = Task, Version = Version, Steps = Steps, WorkingDirectory = WorkingDirectory, Profile = Profile, Tags = Tags.ToTags(), Timeout = Timeout, TransactionPerSession = TransactionPerSession }; Log.LogMessage(MessageImportance.Low, "Executing Migration Runner"); try { new TaskExecutor(runnerContext).Execute(); } catch (ProcessorFactoryNotFoundException ex) { Log.LogError("While executing migrations the following error was encountered: {0}", ex.Message); return false; } catch (Exception ex) { Log.LogError("While executing migrations the following error was encountered: {0}, {1}", ex.Message, ex.StackTrace); return false; } finally { if (outputWriter != null) { outputWriter.Dispose(); } } return true; }
protected override void ExecuteTask() { IAnnouncer announcer = new ConsoleAnnouncer { ShowElapsedTime = Verbose, ShowSql = Verbose }; StreamWriter outputWriter = null; if (Output) { if (string.IsNullOrEmpty(OutputFilename)) OutputFilename = Path.GetFileName(Target) + ".sql"; outputWriter = new StreamWriter(OutputFilename); var fileAnnouncer = new TextWriterAnnouncer(outputWriter) { ShowElapsedTime = false, ShowSql = true }; announcer = new CompositeAnnouncer(announcer, fileAnnouncer); } var runnerContext = new RunnerContext(announcer) { ApplicationContext = ApplicationContext, Database = Database, Connection = Connection, Target = Target, PreviewOnly = Preview, Namespace = Namespace, Task = Task, Version = Version, Steps = Steps, WorkingDirectory = WorkingDirectory, Profile = Profile, Tags = Tags.ToTags(), Timeout = Timeout }; try { new TaskExecutor(runnerContext).Execute(); } catch (ProcessorFactoryNotFoundException ex) { announcer.Error("While executing migrations the following error was encountered: {0}", ex.Message); } catch (Exception e) { announcer.Error("While executing migrations the following error was encountered: {0}, {1}", e.Message, e.StackTrace); } finally { if (outputWriter != null) outputWriter.Dispose(); } }
private void ExecuteMigrations(string outputTo) { using (var sw = new StreamWriter(outputTo)) { var fileAnnouncer = ExecutingAgainstMsSql ? new TextWriterWithGoAnnouncer(sw) : new TextWriterAnnouncer(sw); fileAnnouncer.ShowElapsedTime = false; fileAnnouncer.ShowSql = true; consoleAnnouncer.ShowElapsedTime = verbose; consoleAnnouncer.ShowSql = verbose; var announcer = new CompositeAnnouncer(consoleAnnouncer, fileAnnouncer); ExecuteMigrations(announcer); } }
private void ExecuteMigrations( string outputTo ) { using ( var sw = new StreamWriter( outputTo ) ) { var fileAnnouncer = new TextWriterAnnouncer( sw ) { ShowElapsedTime = false, ShowSql = true }; var consoleAnnouncer = new TextWriterAnnouncer( _announcerOutput ) { ShowElapsedTime = Verbose, ShowSql = Verbose }; var announcer = new CompositeAnnouncer( new[] { consoleAnnouncer, fileAnnouncer }); ExecuteMigrations( announcer ); } }