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); } }
public RepeatableScriptsTests(ITestOutputHelper output) { LogManager.Adapter = new XunitLoggerFactoryAdapter(LogLevel.Debug, output); config = MigratorConfigurationBuilder.Build(Assembly.GetExecutingAssembly(), "TestRepeatableScripts") .SetHaltOnValidationError(true); }
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."); }
public DatabaseMigratorImpl(IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade) { this.configuration = configuration; this.dbAccessFacade = dbAccessFacade; Template.RegisterFilter(typeof(BinaryToHexFilter)); Template.NamingConvention = new CSharpNamingConvention(); }
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; }
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; }
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)); } } }
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(); } } }
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)); } }
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); }
/// <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); }
/// <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; }
/// <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; }
/// <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); }
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); } }
/// <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)); }
/// <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); }
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(); } }
public IList <AppliedItem> GetAppliedScripts(SqlConnection connection, IMigratorConfiguration configuration) { return(DbItems); }
public void CreateSchemaTableIfNotExist(SqlConnection connection, IMigratorConfiguration configuration) { }
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); }
public DatabaseMigratorImpl(IMigratorConfiguration configuration) : this(configuration, new DbAccessFacadeImpl()) { }
public static IDatabaseMigrator Build(IMigratorConfiguration configuration, IDbAccessFacade dbAccessFacade) => new DatabaseMigratorImpl(configuration, dbAccessFacade);
public static IDatabaseMigrator Build(IMigratorConfiguration configuration) => new DatabaseMigratorImpl(configuration);
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(); } }