コード例 #1
0
        public void ApplyPendingScripts(SqlConnection connection, IMigratorConfiguration configuration, IEnumerable <PendingItem> scripts)
        {
            foreach (var updateScript in scripts)
            {
                ClasspathScripts.Add(updateScript);

                AppliedItem appliedScript = new AppliedItem
                {
                    Type     = updateScript.Type,
                    Executed = DateTime.Now,
                    Name     = updateScript.Name,
                    Version  = updateScript.Version,
                    Checksum = updateScript.Checksum
                };

                if (updateScript.Type == ItemType.Repeatable)
                {
                    AppliedItem oldItem = DbItems.FirstOrDefault(s => Equals(updateScript.Version, s.Version));
                    if (oldItem != null)
                    {
                        DbItems.Remove(oldItem);
                    }
                }

                DbItems.Add(appliedScript);
                AppliedItems.Add(appliedScript);

                Log.InfoFormat("Running script {0} with version {1}", updateScript.Name, updateScript.Version);
            }
        }
コード例 #2
0
        public RepeatableScriptsTests(ITestOutputHelper output)
        {
            LogManager.Adapter = new XunitLoggerFactoryAdapter(LogLevel.Debug, output);

            config = MigratorConfigurationBuilder.Build(Assembly.GetExecutingAssembly(), "TestRepeatableScripts")
                     .SetHaltOnValidationError(true);
        }
コード例 #3
0
        public void CreateSchemaTableIfNotExist(SqlConnection connection, IMigratorConfiguration configuration)
        {
            using (var tx = connection.BeginTransaction())
            {
                if (SchemaTableExists(connection, tx, configuration))
                {
                    Log.Debug("Schema table exists.");
                    return;
                }
                Log.Debug("Schema table is not found, creating it now...");

#if NETSTANDARD1_3
                Assembly assembly = typeof(DbAccessFacadeImpl).GetTypeInfo().Assembly;
                using (var stream = assembly.GetManifestResourceStream(GetType().Namespace + ".create.sql"))
#elif NET40 || NET452
                Assembly assembly = Assembly.GetExecutingAssembly();
                using (var stream = assembly.GetManifestResourceStream(GetType(), "create.sql"))
#endif
                {
                    Assert.NotNull(stream, "Could not locate create.sql resource");
                    foreach (var item in GetSchemaVersionTableScripts(stream, configuration))
                    {
                        using (var command = new SqlCommand(item, connection, tx))
                        {
                            command.ExecuteNonQuery();
                        }
                    }
                }
                tx.Commit();
            }
            Log.Debug("Schema table created.");
        }
コード例 #4
0
        public DatabaseMigratorImpl(IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade)
        {
            this.configuration  = configuration;
            this.dbAccessFacade = dbAccessFacade;

            Template.RegisterFilter(typeof(BinaryToHexFilter));
            Template.NamingConvention = new CSharpNamingConvention();
        }
コード例 #5
0
        public BootstrapHandler(SqlConnection connection, IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade)
        {
            Assert.NotNull(connection);
            Assert.NotNull(configuration);
            Assert.NotNull(dbAccessFacade);

            this.configuration  = configuration;
            this.dbAccessFacade = dbAccessFacade;
            this.connection     = connection;
        }
コード例 #6
0
        public DbItemCollector(SqlConnection connection, IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade)
        {
            Assert.NotNull(connection);
            Assert.NotNull(configuration);
            Assert.NotNull(dbAccessFacade);

            this.configuration  = configuration;
            this.dbAccessFacade = dbAccessFacade;
            this.connection     = connection;
        }
コード例 #7
0
 private bool SchemaTableExists(SqlConnection connection, SqlTransaction tx, IMigratorConfiguration configuration)
 {
     using (var command = new SqlCommand(string.Format(SelectSchemaVersionObjectIdSql, configuration.SchemaVersionTable), connection, tx))
     {
         using (var reader = command.ExecuteReader())
         {
             return(reader.Read() && !reader.IsDBNull(0));
         }
     }
 }
コード例 #8
0
 public void ApplyPendingScripts(SqlConnection connection, IMigratorConfiguration configuration, IEnumerable <PendingItem> scripts)
 {
     foreach (var script in scripts)
     {
         using (var tx = connection.BeginTransaction())
         {
             RunScript(connection, tx, configuration, script);
             tx.Commit();
         }
     }
 }
コード例 #9
0
        private IEnumerable <string> GetSchemaVersionTableScripts(Stream stream, IMigratorConfiguration configuration)
        {
            Assert.NotNull(stream, "Could not locate create.sql resource");

            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                var createScript = reader.ReadToEnd();
                if (createScript.Contains(SchemaVersionTableParam))
                {
                    createScript = createScript.Replace(SchemaVersionTableParam, configuration.SchemaVersionTable);
                }

                return(new ScriptSplitter(createScript));
            }
        }
コード例 #10
0
        public IList <AppliedItem> GetAppliedScripts(SqlConnection connection, IMigratorConfiguration configuration)
        {
            List <AppliedItem> result = new List <AppliedItem>();

            using (var command = new SqlCommand(string.Format(SelectFromSchemaVersionSql, configuration.SchemaVersionTable), connection))
            {
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result.Add(BuildAppliedScript(reader));
                    }
                }
            }
            return(result);
        }
コード例 #11
0
        /// <summary>
        /// Строка подключения
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        private static string GetConnectionString(IMigratorConfiguration config)
        {
            string connectionString = null;

            if (!string.IsNullOrWhiteSpace(config.ConnectionString))
            {
                connectionString = config.ConnectionString.Trim();
            }
            else if (!string.IsNullOrWhiteSpace(config.ConnectionStringName))
            {
                string cstringName = config.ConnectionStringName.Trim();
                connectionString = ConfigurationManager.ConnectionStrings[cstringName].ConnectionString;
            }

            Require.IsNotNullOrEmpty(connectionString, true, "Не задана строка подключения");

            return(connectionString);
        }
コード例 #12
0
        /// <summary>
        /// Строка подключения
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        private static string GetConnectionString(IMigratorConfiguration config)
        {
            string connectionString = null;

            if (!string.IsNullOrWhiteSpace(config.ConnectionString))
            {
                connectionString = config.ConnectionString.Trim();
            }
            else if (!string.IsNullOrWhiteSpace(config.ConnectionStringName))
            {
                string cstringName = config.ConnectionStringName.Trim();
                connectionString = ConfigurationManager.ConnectionStrings[cstringName].ConnectionString;
            }

            Require.IsNotNullOrEmpty(connectionString, true, "Не задана строка подключения");

            return connectionString;
        }
コード例 #13
0
        /// <summary>
        /// Загрузка сборки с миграциями
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        private static Assembly GetAssembly(IMigratorConfiguration config)
        {
            Assembly assembly = null;

            if (!string.IsNullOrWhiteSpace(config.Assembly))
            {
                assembly = Assembly.Load(config.Assembly);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(config.AssemblyFile))
                {
                    assembly = Assembly.LoadFrom(config.AssemblyFile);
                }
            }

            Require.IsNotNull(assembly, "Не задана сборка, содержащая миграции");
            return assembly;
        }
コード例 #14
0
        /// <summary>
        /// Загрузка сборки с миграциями
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        private static Assembly GetAssembly(IMigratorConfiguration config)
        {
            Assembly assembly = null;

            if (!string.IsNullOrWhiteSpace(config.Assembly))
            {
                assembly = Assembly.Load(config.Assembly);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(config.AssemblyFile))
                {
                    assembly = Assembly.LoadFrom(config.AssemblyFile);
                }
            }

            Require.IsNotNull(assembly, "Не задана сборка, содержащая миграции");
            return(assembly);
        }
コード例 #15
0
        private void RunScript(SqlConnection connection, SqlTransaction tx, IMigratorConfiguration configuration, PendingItem script)
        {
            Log.DebugFormat(script.Type == ItemType.Versioned ? "Applying script {0}" : "Reapplying script {0}", script.Name);

            AppliedItem appliedScript = GetAppliedScript(connection, tx, configuration, script);

            if (appliedScript != null && appliedScript.Type == ItemType.Versioned)
            {
                Log.Debug("Script already applied, skipping.");
                return;
            }

            foreach (var scriptItem in new ScriptSplitter(script.Content.ToString()))
            {
                try
                {
                    using (var command = new SqlCommand(scriptItem, connection, tx))
                    {
                        command.CommandTimeout = configuration.CommandTimeout;
                        command.ExecuteNonQuery();
                    }
                }
                catch (SqlException)
                {
                    Log.Error("Error while running script:\n" + scriptItem);
                    throw;
                }
            }

            if (appliedScript == null)
            {
                InsertIntoSchemaVersion(connection, tx, configuration, script);
            }
            else
            {
                UpdateSchemaVersion(connection, tx, configuration, script);
            }
        }
コード例 #16
0
        /// <summary>
        /// Создание экземпляра мигратора, инициализированного заданными настройками
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        public static Migrator CreateMigrator(IMigratorConfiguration config)
        {
            Require.IsNotNull(config, "Конфигурация не задана");
            Require.IsNotNullOrEmpty(config.Provider, "Не задан используемый тип провайдера");

            Assembly assembly = GetAssembly(config);

            string connectionString = GetConnectionString(config);

            var provider = ProviderFactory.Create(config.Provider.Trim(), connectionString);

            if (config.CommandTimeout.HasValue)
            {
                provider.CommandTimeout = config.CommandTimeout.Value;
            }

            if (config.NeedQuotesForNames.HasValue)
            {
                provider.NeedQuotesForNames = config.NeedQuotesForNames.Value;
            }

            return(new Migrator(provider, assembly));
        }
コード例 #17
0
        /// <summary>
        /// Создание экземпляра мигратора, инициализированного заданными настройками
        /// </summary>
        /// <param name="config">Конфигурация мигратора</param>
        public static Migrator CreateMigrator(IMigratorConfiguration config)
        {
            Require.IsNotNull(config, "Конфигурация не задана");
            Require.IsNotNullOrEmpty(config.Provider, "Не задан используемый тип провайдера");

            Assembly assembly = GetAssembly(config);

            string connectionString = GetConnectionString(config);

            var provider = ProviderFactory.Create(config.Provider.Trim(), connectionString);

            if (config.CommandTimeout.HasValue)
            {
                provider.CommandTimeout = config.CommandTimeout.Value;
            }

            if (config.NeedQuotesForNames.HasValue)
            {
                provider.NeedQuotesForNames = config.NeedQuotesForNames.Value;
            }

            return new Migrator(provider, assembly);
        }
コード例 #18
0
        private void UpdateSchemaVersion(SqlConnection connection, SqlTransaction tx, IMigratorConfiguration configuration, PendingItem script)
        {
            var updateSql = string.Format(UpdateSchemaVersionHashSql, configuration.SchemaVersionTable);

            using (var command = new SqlCommand(updateSql, connection, tx))
            {
                command.Parameters.Add(new SqlParameter("Hash", script.Checksum));
                command.Parameters.Add(new SqlParameter("Executed", DateTime.Now));
                command.Parameters.Add(new SqlParameter("Version", script.Version));
                command.ExecuteNonQuery();
            }
        }
コード例 #19
0
 public IList <AppliedItem> GetAppliedScripts(SqlConnection connection, IMigratorConfiguration configuration)
 {
     return(DbItems);
 }
コード例 #20
0
 public void CreateSchemaTableIfNotExist(SqlConnection connection, IMigratorConfiguration configuration)
 {
 }
コード例 #21
0
        public AppliedItem GetAppliedScript(SqlConnection connection, SqlTransaction tx, IMigratorConfiguration configuration, PendingItem script)
        {
            AppliedItem result = null;

            using (var command = new SqlCommand(string.Format(SelectSingleFromSchemaVersionSql, configuration.SchemaVersionTable), connection, tx))
            {
                command.CommandTimeout = configuration.CommandTimeout;
                command.Parameters.Add(new SqlParameter("Version", script.Version));

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = BuildAppliedScript(reader);
                    }
                }
            }

            return(result);
        }
コード例 #22
0
 public DatabaseMigratorImpl(IMigratorConfiguration configuration) : this(configuration, new DbAccessFacadeImpl())
 {
 }
コード例 #23
0
 public static IDatabaseMigrator Build(IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade) => new DatabaseMigratorImpl(configuration, dbAccessFacade);
コード例 #24
0
 public static IDatabaseMigrator Build(IMigratorConfiguration configuration) => new DatabaseMigratorImpl(configuration);
コード例 #25
0
        private void InsertIntoSchemaVersion(SqlConnection connection, SqlTransaction tx, IMigratorConfiguration configuration, PendingItem script)
        {
            var insertSql = string.Format(InsertIntoSchemaVersionSql, configuration.SchemaVersionTable);

            using (var command = new SqlCommand(insertSql, connection, tx))
            {
                command.Parameters.Add(new SqlParameter("Type", ScriptTypeUtils.ResolveString(script.Type)));
                command.Parameters.Add(new SqlParameter("Version", script.Version));
                command.Parameters.Add(new SqlParameter("Script", script.Name));
                command.Parameters.Add(new SqlParameter("Executed", DateTime.Now));
                command.Parameters.Add(new SqlParameter("Hash", script.Checksum));
                command.ExecuteNonQuery();
            }
        }