Ejemplo n.º 1
0
            public override UpgradeEngine Given()
            {
                scriptProvider = Substitute.For <IScriptProvider>();
                scriptProvider.GetScripts(Arg.Any <IConnectionManager>()).Returns(new List <SqlScript> {
                    new SqlScript("1234", "foo")
                });
                versionTracker = Substitute.For <IJournal>();
                dbConnection   = Substitute.For <IDbConnection>();
                dbCommand      = Substitute.For <IDbCommand>();
                dbConnection.CreateCommand().Returns(dbCommand);
                var connectionManager = new TestConnectionManager(dbConnection);

                scriptExecutor = new SqlScriptExecutor(() => connectionManager, () => Substitute.For <IUpgradeLog>(), null, () => true, null, () => versionTracker);

                var builder = new UpgradeEngineBuilder()
                              .WithScript(new SqlScript("1234", "create table $var$ (Id int)"))
                              .JournalTo(versionTracker)
                              .WithVariable("var", "sub");

                builder.Configure(c => c.ScriptExecutor    = scriptExecutor);
                builder.Configure(c => c.ConnectionManager = connectionManager);

                var upgrader = builder.Build();

                return(upgrader);
            }
Ejemplo n.º 2
0
    /// <summary>
    /// Creates an upgrader for Sql Anywhere databases.
    /// </summary>
    /// <param name="supported">Fluent helper type.</param>
    /// <param name="connectionString">Sql Anywhere database connection string.</param>
    /// <returns>
    /// A builder for a database upgrader designed for Sql Anywhere databases.
    /// </returns>
    public static UpgradeEngineBuilder SqlAnywhereDatabase(this SupportedDatabases supported, string connectionString)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = new SqlAnywhereConnectionManager(connectionString));
        builder.Configure(c => c.ScriptExecutor    = new SqlAnywhereScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => c.VariablesEnabled, c.ScriptPreprocessors, () => c.Journal));
        builder.Configure(c => c.Journal           = new SqlAnywhereTableJournal(() => c.ConnectionManager, () => c.Log, () => c.Hasher, null, "schemaversions"));
        return(builder);
    }
        public static UpgradeEngineBuilder HashedSqlDatabase(this SupportedDatabases supported, SqlConnectionManager connectionManager)
        {
            var builder = new UpgradeEngineBuilder();

            builder.Configure(c => c.ConnectionManager = connectionManager);
            builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => c.VariablesEnabled, c.ScriptPreprocessors, () => JournalFactory(c)));
            builder.Configure(c => c.Journal           = JournalFactory(c));
            return(builder);
        }
Ejemplo n.º 4
0
    /// <summary>
    /// Creates an upgrader for SQL Server databases.
    /// </summary>
    /// <param name="connectionManager">The <see cref="IConnectionManager"/> to be used during a database
    /// upgrade. See <see cref="SqlAnywhereConnectionManager"/> for an example implementation</param>
    /// <param name="schema">The SqlAnywhere schema name to use. Defaults to ''.</param>
    /// <returns>
    /// A builder for a database upgrader designed for SqlAnywhere databases.
    /// </returns>
    static UpgradeEngineBuilder SqlAnywhereDatabase(IConnectionManager connectionManager, string schema)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new SqlAnywhereScriptExecutor(() => c.ConnectionManager, () => c.Log, schema, () => c.VariablesEnabled, c.ScriptPreprocessors, () => c.Journal));
        builder.Configure(c => c.Journal           = new SqlAnywhereTableJournal(() => c.ConnectionManager, () => c.Log, () => c.Hasher, schema, "SchemaVersions"));
        return(builder);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Creates an upgrader for SQL Server databases.
    /// </summary>
    /// <param name="supported">Fluent helper type.</param>
    /// <param name="connectionFactory">The connection factory.</param>
    /// <param name="schema">The SQL schema name to use. Defaults to 'dbo'.</param>
    /// <returns>
    /// A builder for a database upgrader designed for SQL Server databases.
    /// </returns>
    public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported, Func <IDbConnection> connectionFactory, string schema)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionFactory = connectionFactory);
        builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(c.ConnectionFactory, () => c.Log, schema, () => c.VariablesEnabled, c.ScriptPreprocessors));
        builder.Configure(c => c.Journal           = new SqlTableJournal(c.ConnectionFactory, schema, "SchemaVersions", c.Log));
        return(builder);
    }
Ejemplo n.º 6
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="connectionManager"></param>
    /// <param name="schema"></param>
    /// <param name="deployVersion"></param>
    /// <returns></returns>
    private static UpgradeEngineBuilder SqlDatabase(IConnectionManager connectionManager, string schema, string deployVersion)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, schema, () => c.VariablesEnabled, c.ScriptPreprocessors));
        builder.Configure(c => c.Journal           = new SqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, deployVersion));
        return(builder);
    }
        public static UpgradeEngineBuilder HashedSqlDatabase(this SupportedDatabases supported, SqlConnectionManager connectionManager, string versionTableSchema, string versionTableName)
        {
            var builder = new UpgradeEngineBuilder();

            builder.Configure(c => c.ConnectionManager = connectionManager);
            builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, versionTableSchema, () => c.VariablesEnabled, c.ScriptPreprocessors));
            builder.Configure(c => c.Journal           = new HashedSqlTableJournal(() => c.ConnectionManager, () => c.Log, versionTableSchema, versionTableName));
            return(builder);
        }
Ejemplo n.º 8
0
    /// <summary>
    /// Creates an upgrader for PostgreSQL databases.
    /// </summary>
    /// <param name="connectionManager">The <see cref="PostgresqlConnectionManager"/> to be used during a database upgrade.</param>
    /// <returns>
    /// A builder for a database upgrader designed for PostgreSQL databases.
    /// </returns>
    public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => false, c.ScriptPreprocessors));
        builder.Configure(c => c.Journal           = new PostgresqlTableJournal(() => c.ConnectionManager, () => c.Log, null, "schemaversions"));
        builder.WithPreprocessor(new PostgresqlPreprocessor());
        return(builder);
    }
Ejemplo n.º 9
0
    /// <summary>
    /// Creates an upgrader for PostgreSQL databases.
    /// </summary>
    /// <param name="connectionManager">The <see cref="PostgresqlConnectionManager"/> to be used during a database upgrade.</param>
    /// <param name="schema">The schema in which to check for changes</param>
    /// <returns>
    /// A builder for a database upgrader designed for PostgreSQL databases.
    /// </returns>
    public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager, string schema)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new PostgresqlScriptExecutor(() => c.ConnectionManager, () => c.Log, schema, () => c.VariablesEnabled, c.ScriptPreprocessors, () => c.Journal));
        builder.Configure(c => c.Journal           = new PostgresqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, "schemaversions"));
        builder.WithPreprocessor(new PostgresqlPreprocessor());
        return(builder);
    }
Ejemplo n.º 10
0
    private static UpgradeEngineBuilder SqlCeDatabase(IConnectionManager connectionManager)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => c.VariablesEnabled, c.ScriptPreprocessors));
        builder.Configure(c => c.Journal           = new SqlTableJournal(() => connectionManager, () => c.Log, null, "SchemaVersions"));
        builder.WithPreprocessor(new SqlCePreprocessor());
        return(builder);
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Creates an upgrader for Firebird databases.
    /// </summary>
    /// <param name="connectionManager">The <see cref="FirebirdConnectionManager"/> to be used during a database upgrade.</param>
    /// <returns>
    /// A builder for a database upgrader designed for Firebird databases.
    /// </returns>
    public static UpgradeEngineBuilder FirebirdDatabase(IConnectionManager connectionManager)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = connectionManager);
        builder.Configure(c => c.ScriptExecutor    = new FirebirdScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => c.VariablesEnabled, c.ScriptPreprocessors, () => c.Journal));
        builder.Configure(c => c.Journal           = new FirebirdTableJournal(() => c.ConnectionManager, () => c.Log, "schemaversions"));
        builder.WithPreprocessor(new FirebirdPreprocessor());
        return(builder);
    }
Ejemplo n.º 12
0
    /// <summary>
    /// Creates an upgrade for SQLite databases with a given database connection
    /// </summary>
    /// <param name="supported">Fluent helper type.</param>
    /// <param name="sqliteConnection">Given SQLite database connection object which is used by DbUp upgrade engine</param>
    /// <returns>
    /// A builder for a database upgrader designed for SQLite databases.
    /// </returns>
    public static UpgradeEngineBuilder SQLiteDatabase(this SupportedDatabases supported, SQLiteConnection sqliteConnection)
    {
        var builder = new UpgradeEngineBuilder();

        builder.Configure(c => c.ConnectionManager = new SQLiteConnectionManager(sqliteConnection));
        builder.Configure(c => c.Journal           = new SQLiteTableJournal(() => c.ConnectionManager, () => c.Log, "SchemaVersions"));
        builder.Configure(c => c.ScriptExecutor    = new SqlScriptExecutor(() => c.ConnectionManager, () => c.Log, null,
                                                                           () => c.VariablesEnabled, c.ScriptPreprocessors));
        builder.WithPreprocessor(new SQLitePreprocessor());
        return(builder);
    }
Ejemplo n.º 13
0
 /// <summary>
 /// Applies a filter to the upgrade pipeline, allowing the list of scripts
 /// to be sorted and filtered immediately before execution.
 ///
 /// Scripts that have previously been executed will not run again.
 /// </summary>
 /// <returns>The filtered scripts.</returns>
 /// <param name="builder">Builder.</param>
 /// <param name="filter">A delegate that takes the input set of scripts
 /// and returns them sorted and filtered.</param>
 public static UpgradeEngineBuilder WithFilter(
     this UpgradeEngineBuilder builder,
     Func <IEnumerable <SqlScript>, IEnumerable <SqlScript> > filter)
 {
     builder.Configure(config => config.ScriptFilter = new DelegatedFilter(filter));
     return(builder);
 }
Ejemplo n.º 14
0
 public static UpgradeEngineBuilder JournalToFlywayHashTable(this UpgradeEngineBuilder builder, IFlywayConf conf)
 {
     (var schema, var table) = GetTableParts(conf);
     builder.Configure(c =>
                       c.Journal = new FlywaySqlHashTableJournal(conf, () => c.ConnectionManager, () => c.Log, schema, table)
                       );
     return(builder);
 }
Ejemplo n.º 15
0
    /// <summary>
    /// Allows you to set the execution timeout for scripts.
    /// </summary>
    /// <param name="builder">The builder.</param>
    /// <param name="timeout">A <c>TimeSpan</c> value containing the timeout value or <c>null</c>.</param>
    /// <exception cref="System.ArgumentOutOfRangeException">The timeout value is less than zero or greater than 2,147,483,647 seconds.</exception>
    /// <remarks>Setting the timeout parameter to <c>null</c> will use the default timeout of the underlying provider.</remarks>
    /// <returns></returns>
    public static UpgradeEngineBuilder WithExecutionTimeout(this UpgradeEngineBuilder builder, TimeSpan?timeout)
    {
        if (timeout == null)
        {
            builder.Configure(c => c.ScriptExecutor.ExecutionTimeoutSeconds = null);
            return(builder);
        }

        var totalSeconds = timeout.Value.TotalSeconds;

        if ((0 > totalSeconds) || (totalSeconds > int.MaxValue))
        {
            throw new ArgumentOutOfRangeException("timeout", timeout, string.Format("The timeout value must be a value between 1 and {0} seconds", int.MaxValue));
        }

        builder.Configure(c => c.ScriptExecutor.ExecutionTimeoutSeconds = Convert.ToInt32(totalSeconds));
        return(builder);
    }
Ejemplo n.º 16
0
        public static UpgradeEngineBuilder WithDowngradeTableProvider <TDowngradeEnabledTableJournal>(this UpgradeEngineBuilder builder,
                                                                                                      IScriptProvider downgradeScriptProvider,
                                                                                                      string schema = null,
                                                                                                      string table  = null) where TDowngradeEnabledTableJournal : DowngradeEnabledTableJournal
        {
            builder.Configure(c =>
            {
                Func <IConnectionManager> connectionManager = () => c.ConnectionManager;
                Func <IUpgradeLog> log = () => c.Log;
                c.Journal = (TDowngradeEnabledTableJournal)Activator.CreateInstance(typeof(TDowngradeEnabledTableJournal),
                                                                                    connectionManager, log, schema, table, downgradeScriptProvider);
            });

            return(builder);
        }
    private static UpgradeEngineBuilder OracleDatabase(IConnectionManager connectionManager)
    {
        var builder = new UpgradeEngineBuilder();

        // Connection manager
        builder.Configure(c => c.ConnectionManager = connectionManager);

        // Script executor
        builder.Configure(
            c => c.ScriptExecutor =
                new OracleScriptExecutor(connectionManagerProvider: () => c.ConnectionManager,
                                         loggerProvider: () => c.Log,
                                         variablesEnabledProvider: () => c.VariablesEnabled,
                                         scriptPreprocessors: c.ScriptPreprocessors,
                                         table: OracleTableJournal.DefaultJournalName));

        // Journal database ltable
        builder.Configure(
            c => c.Journal =
                new OracleTableJournal(connectionManager: () => c.ConnectionManager,
                                       logger: () => c.Log,
                                       table: OracleTableJournal.DefaultJournalName));
        return(builder);
    }
        public DowngradeEnabledUpgradeEngine(UpgradeEngineBuilder builder, bool autoDowngradeEnabled)
        {
            _autoDowngradeEnabled = autoDowngradeEnabled;

            builder.Configure(c =>
            {
                if (!(c.Journal is DowngradeEnabledTableJournal))
                {
                    throw new NotSupportedException("Can't build 'DowngradeEnabledUpgradeEngine', journal table not inherits 'DowngradeEnabledTableJournal'");
                }

                _journal           = c.Journal as DowngradeEnabledTableJournal;
                _scriptProviders   = c.ScriptProviders;
                _connectionManager = c.ConnectionManager;
                _log = c.Log;
            });

            UpgradeEngine = builder.Build();
        }
Ejemplo n.º 19
0
        RunTest(UpgradeEngineBuilder builder)
        {
            var executor    = new Mock <IScriptExecutor>();
            var connections = new Mock <IConnectionManager>();

            builder.Configure(c =>
            {
                c.ScriptExecutor    = executor.Object;
                c.ConnectionManager = connections.Object;
            });

            var scripts = new List <SqlScript>();

            executor.Setup(x => x.Execute(It.IsAny <SqlScript>()))
            .Callback(new Action <SqlScript>(s => scripts.Add(s)));

            executor.Setup(x => x.Execute(It.IsAny <SqlScript>(), It.IsAny <IDictionary <string, string> >()))
            .Callback(new Action <SqlScript, IDictionary <string, string> >((s, a) => scripts.Add(s)));

            builder.Build().PerformUpgrade();

            return(scripts, executor, connections);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Configures hashing script contents and saving them to the journal table.
        /// This means that if a script is not changed, it won't be re-run, but if it is
        /// changed then it will. This avoids the need to treat "run always" and
        /// "run once" scripts differently
        ///
        /// A filter is also installed that ensures script names include the hash.
        /// You can optionally provide your own additional filtering.
        /// </summary>
        /// <returns>The to sql with hashing.</returns>
        /// <param name="builder">Builder.</param>
        /// <param name="filter">Filter.</param>
        /// <param name="schemaName">Schema name.</param>
        /// <param name="tableName">Table name.</param>
        public static UpgradeEngineBuilder JournalToSqlWithHashing(
            this UpgradeEngineBuilder builder,
            Func <IEnumerable <SqlScript>, IEnumerable <SqlScript> > filter = null,
            string schemaName = null,
            string tableName  = null)
        {
            builder.Configure(config =>
            {
                config.Journal = new SqlHashingJournal(
                    () => config.ConnectionManager,
                    () => config.Log,
                    schemaName,
                    tableName);

                config.ScriptFilter = new DelegatedFilter(
                    scripts => (filter != null
                                    ? filter(scripts)
                                    : scripts)
                    .HashNames());
            });

            return(builder);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Sets the filter that filters the sorted lists of script prior to execution. This allows
 /// scripts to be excluded based on which scripts have already been run.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <param name="filter">The filter.</param>
 /// <returns>
 /// The same builder
 /// </returns>
 public static UpgradeEngineBuilder WithFilter(this UpgradeEngineBuilder builder, IScriptFilter filter)
 {
     builder.Configure(b => b.ScriptFilter = filter);
     return(builder);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Tracks the list of executed scripts in a SQL Server table.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <param name="schema">The schema.</param>
 /// <param name="table">The table.</param>
 /// <returns></returns>
 public static UpgradeEngineBuilder JournalToPostgresqlTable(this UpgradeEngineBuilder builder, string schema, string table)
 {
     builder.Configure(c => c.Journal = new PostgresqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table));
     return(builder);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Logs to the console using pretty colours.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>
 /// The same builder
 /// </returns>
 public static UpgradeEngineBuilder LogScriptOutput(this UpgradeEngineBuilder builder)
 {
     builder.Configure(c => c.ConnectionManager.IsScriptOutputLogged = true);
     return(builder);
 }
Ejemplo n.º 24
0
    /// <summary>
    /// Run each script in it's own transaction
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static UpgradeEngineBuilder WithTransactionPerScript(this UpgradeEngineBuilder builder)
    {
        builder.Configure(c => c.ConnectionManager.TransactionMode = TransactionMode.TransactionPerScript);

        return(builder);
    }
Ejemplo n.º 25
0
    /// <summary>
    /// Run creates a new connection for each script, without a transaction
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static UpgradeEngineBuilder WithoutTransaction(this UpgradeEngineBuilder builder)
    {
        builder.Configure(c => c.ConnectionManager.TransactionMode = TransactionMode.NoTransaction);

        return(builder);
    }
Ejemplo n.º 26
0
 /// <summary>
 /// Sets a configuration flag which will cause the engine to perform variable expansion.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns></returns>
 public static UpgradeEngineBuilder WithVariablesEnabled(this UpgradeEngineBuilder builder)
 {
     builder.Configure(c => c.VariablesEnabled = true);
     return(builder);
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Adds a set of variables that will be replaced before scripts are executed.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <param name="variables">The variables.</param>
 /// <returns>
 /// The same builder
 /// </returns>
 public static UpgradeEngineBuilder WithVariables(this UpgradeEngineBuilder builder, IDictionary <string, string> variables)
 {
     builder.Configure(c => c.AddVariables(variables));
     return(builder);
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Adds a preprocessor that can replace portions of a script.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <param name="preprocessor">The preprocessor.</param>
 /// <returns>
 /// The same builder
 /// </returns>
 public static UpgradeEngineBuilder WithPreprocessor(this UpgradeEngineBuilder builder, IScriptPreprocessor preprocessor)
 {
     builder.Configure(c => c.ScriptPreprocessors.Add(preprocessor));
     return(builder);
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Sets the comparer used to sort scripts and match script names against the log of already run scripts.
 /// The default comparer is StringComparer.Ordinal.
 /// By implementing your own comparer you can make make the matching and ordering case insensitive,
 /// change how numbers are handled or support the renaming of scripts
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <param name="comparer">The sorter.</param>
 /// <returns>
 /// The same builder
 /// </returns>
 public static UpgradeEngineBuilder WithScriptNameComparer(this UpgradeEngineBuilder builder, IComparer <string> comparer)
 {
     builder.Configure(b => b.ScriptNameComparer = new ScriptNameComparer(comparer));
     return(builder);
 }
Ejemplo n.º 30
0
 public static UpgradeEngineBuilder WithScriptOrderer(this UpgradeEngineBuilder builder, Func <IEnumerable <SqlScript>, ScriptNameComparer, IOrderedEnumerable <SqlScript> > orderer)
 {
     builder.Configure(b => b.ScriptOrderer = orderer);
     return(builder);
 }