/// <summary> /// Replace elements by conventions<br/> /// Replace {SchemaName} with <paramref name="schemaName"/> or <see cref="IDbConfigSchemaTargets.Schema"/><br/> /// Replace {SchemaPrefixId} with <paramref name="schemaPrefixId"/> or <see cref="IDbConfigSchemaTargets.GetSchemaPrefixId()"/><br/> /// Replace {SchemaPrefixUniqueId} with <paramref name="schemaPrefixUniqueId"/> or <see cref="IDbMigrationConfig.GetSchemaPrefixUniqueId()"/><br/> /// Replace {MigrationName} with <paramref name="migrationName"/> or <see cref="IDbMigrationConfig.GetMigrationName()"/><br/> /// Replace {SchemaPassword} with <paramref name="schemaPassword"/> or <see cref="IDbMigrationConfig.SchemaPassword"/> or <see cref="IDbConfigCredentials.Password"/> <br/> /// Replace {User} with <paramref name="user"/> or <see cref="IDbConfigCredentials.User"/><br/> /// Replace {Password} with <paramref name="password"/> or <see cref="IDbConfigCredentials.Password"/> or <see cref="IDbConfigCredentials.User"/> <br/> /// </summary> /// <param name="migrationConfig"></param> /// <param name="sql"></param> /// <param name="schemaName"></param> /// <param name="schemaPrefixId"></param> /// <param name="schemaPrefixUniqueId"></param> /// <param name="migrationName"></param> /// <param name="schemaPassword"></param> /// <param name="user"></param> /// <param name="password"></param> /// <returns></returns> public static string PrepareSql(this IDbMigrationConfig migrationConfig, string sql, string schemaName = null, string schemaPrefixId = null, string schemaPrefixUniqueId = null, string migrationName = null, string schemaPassword = null, string user = null, string password = null) { if (sql == null) { return(null); } schemaName = schemaName ?? migrationConfig.Schema; schemaPassword = schemaPassword ?? migrationConfig.SchemaPassword.WithDefault(schemaName).WithDefault(migrationConfig.GetDbConfig()?.Password); schemaPrefixId = schemaPrefixId ?? migrationConfig.GetSchemaPrefixId(); schemaPrefixUniqueId = schemaPrefixUniqueId ?? migrationConfig.GetSchemaPrefixUniqueId(); migrationName = migrationName ?? migrationConfig.GetMigrationName(); user = user ?? migrationConfig?.GetDbConfig().User.WithDefault(migrationName); password = password ?? migrationConfig?.GetDbConfig().Password; return(sql .ReplaceIgnoreCase("{MigrationName}", migrationName) .ReplaceIgnoreCase("{User}", user) .ReplaceIgnoreCase("{Password}", password) .ReplaceIgnoreCase("{SchemaName}", schemaName?.ToUpper()) .ReplaceIgnoreCase("{SchemaPassword}", schemaPassword?.ToUpper()) .ReplaceIgnoreCase("{SchemaPrefixId}", schemaPrefixId) .ReplaceIgnoreCase("{SchemaPrefixUniqueId}", schemaPrefixUniqueId)); }
/// <summary> /// Constructor<br/> /// -------------<br/> /// AnonymousOperation is assigned with "NOOP"<br/> /// SchemaPrefixId is assigned with value from parameter <paramref name="migrationConfig"/> - property <see cref="IMigrationModel.SchemaPrefixId"/><br/> /// SchemaPrefixUniqueId is assigned with value from parameter <paramref name="migrationConfig"/> - property <see cref="IMigrationModel.SchemaPrefixUniqueId"/><br/> /// <br/> /// i.e:<br/> /// When configuration have database:migration:schemaPrefix:Id = "PR" and database:migration:schemaPrefix:UniqueId = "abode" <br/> /// SchemaPrefixId will be resolved to "PR"<br/> /// SchemaPrefixUniqueId will be resolved to "abode"<br/> /// </summary> /// <param name="migrationConfig"></param> public ChangeLogContext(IDbMigrationConfig migrationConfig) : this() { SchemaPrefixId = migrationConfig?.GetSchemaPrefixId(); SchemaPrefixUniqueId = migrationConfig?.GetSchemaPrefixUniqueId(); InitMetadata(migrationConfig); }
private static void DropDatabase(IDbMigrationConfig dbMigrationConfig, IDbConnection dbConnection, Action before = null, Action <string> after = null) { var maxTries = 5; var dropped = false; var sqlDropDatabase = string.Format(DatabaseDropSqlTemplate, dbMigrationConfig.DatabaseName.ToLower()); while (dbConnection.Exists(string.Format(DatabaseExistsSqlTemplate, dbMigrationConfig.DatabaseName.ToLower()))) { before?.Invoke(); before = null; KillAllDatabaseConnections(dbMigrationConfig, dbConnection); dbConnection.ExecuteWithCommand(sqlDropDatabase); if (--maxTries < 0) { break; } dropped = true; } if (dropped) { after?.Invoke(sqlDropDatabase); } }
public ExtendedOracleMigrationGenerator( OracleQuoterBase quoter, IDbMigrationConfig dbMigrationConfig) { Quoter = quoter; DbMigrationConfig = dbMigrationConfig; }
public ExtendedOracleProcessorBase(OracleBaseDbFactory factory, IMigrationGenerator generator, ILogger logger, IOptionsSnapshot <ProcessorOptions> options, IOptionsSnapshot <RunnerOptions> runnerOptions, IConnectionStringAccessor connectionStringAccessor, IExtendedMigrationGenerator <ExtendedOracleMigrationGenerator> extendedGenerator, IDbMigrationConfig migrationConfig, IMigrationSourceItem migrationSourceItem = null, IVersionTableMetaData versionTableMetaData = null, IOptions <FluentMigratorLoggerOptions> loggerGenOptions = null, IOptions <LogFileFluentMigratorLoggerOptions> logFileOptions = null) : base(ProcessorIds.OracleProcessorId, factory, generator, logger, options, connectionStringAccessor) { RunnerOptions = runnerOptions; MigrationConfig = migrationConfig; VersionTableMetaData = versionTableMetaData; LoggerOptions = loggerGenOptions?.Value; LoggerFileOptions = logFileOptions?.Value; IsSqlLogEnabled = (LoggerOptions?.ShowSql ?? false) || (LoggerFileOptions?.ShowSql ?? false); ExtendedGenerator = extendedGenerator; MigrationMetadata = new MigrationMetadata(migrationSourceItem).InitMetadata(MigrationConfig); ConnectionStringFunc = () => connectionStringAccessor.ConnectionString; Initialize(); }
public ExtendedPostgresProcessor(IDbMigrationConfig dbMigrationConfig, PostgresQuoter quoter, PostgresDbFactory factory, PostgresGenerator generator, ILogger <ExtendedPostgresProcessor> logger, IOptionsSnapshot <ProcessorOptions> options, IConnectionStringAccessor connectionStringAccessor, IExtendedMigrationGenerator <ExtendedPostgresGenerator> extendedGenerator) : base(factory, generator, logger, options, connectionStringAccessor) { ExtendedGeneratorField = extendedGenerator; DbMigrationConfig = dbMigrationConfig; if (dbMigrationConfig.ProcessorId == ProcessorIds.PostgresProcessorId) { var stopWatch = new StopWatch(); PostgresDatabaseCreator.CreateDatabase( dbMigrationConfig, () => { Logger.LogSay($"Creating Postgres database '{dbMigrationConfig.DatabaseName.ToLower()}'..."); stopWatch.Start(); }, sql => { stopWatch.Stop(); Logger.LogSql(sql); Logger.LogSay($"Created Postgres database '{dbMigrationConfig.DatabaseName.ToLower()}'..."); Logger.LogElapsedTime(stopWatch.ElapsedTime()); }); } this.SetPrivateFieldValue <PostgresProcessor>("_quoter", quoter); }
public ExtendedPostgresGenerator( PostgresQuoter quoter, IDbMigrationConfig dbMigrationConfig) { Quoter = quoter; DbMigrationConfig = dbMigrationConfig; }
/// <summary> /// Resolve table-value by <paramref name="template"/> and <paramref name="tableName"/><br/> /// i.e: With <paramref name="template"/> equal to "tables:{<paramref name="tableName"/>}:globalId" and <paramref name="tableName"/> equal to "Person" (and ie. <paramref name="migrationConfig"/>.GetSchemaPrefixId() (<see cref="IDbConfigSchemaTargets.GetSchemaPrefixId()"/>) returns "EX" <br/> /// => Will search configuration:<br/> /// - "database:migration:tables:Person:globalId"<br/> /// - "database:migration:tables:EXPerson:globalId"<br/> /// - "database:tables:Person:globalId"<br/> /// - "database:tables:EXPerson:globalId"<br/> /// </summary> /// <param name="migrationConfig"></param> /// <param name="template">string template containing {tableName}</param> /// <param name="tableName"></param> /// <param name="fallbackTemplates"></param> /// <returns></returns> public static string GetTableConfigValue(this IDbMigrationConfig migrationConfig, string template, string tableName, params string[] fallbackTemplates) { if (migrationConfig == null) { return(null); } var defaultKey = template.ReplaceIgnoreCase("{tableName}", tableName); var alternativeKey = template.ReplaceIgnoreCase("{tableName}", tableName.TrimPrefixName(migrationConfig.GetSchemaPrefixId())); var alternativeKey2 = template.ReplaceIgnoreCase("{tableName}", tableName.GetPrefixedName(migrationConfig.GetSchemaPrefixId())); var list = new List <string>(new[] { defaultKey, alternativeKey, alternativeKey2 }.Distinct()); if (fallbackTemplates.Any()) { list.AddRange(fallbackTemplates.SelectMany(x => new[] { x.ReplaceIgnoreCase("{tableName}", tableName), x.ReplaceIgnoreCase("{tableName}", tableName.TrimPrefixName(migrationConfig.GetSchemaPrefixId())), x.ReplaceIgnoreCase("{tableName}", tableName.GetPrefixedName(migrationConfig.GetSchemaPrefixId())), }.Distinct())); } var keys = list.Distinct().ToArray(); return(migrationConfig.GetAllMigrationConfigValues()?.GetValue(keys) ?? migrationConfig.GetDbConfig().GetAllDatabaseConfigValues()?.GetValue(keys)); }
/// <summary> /// Resolve underlying <see cref="IConfiguration"/> to the implemented class of <see cref="IDbMigrationConfig"/> /// </summary> /// <param name="dbMigrationConfig"></param> /// <returns></returns> public static IConfiguration GetConfiguration(this IDbMigrationConfig dbMigrationConfig) { if (dbMigrationConfig != null && dbMigrationConfig is MsDbMigrationConfig msDbMigrationConfig) { return(msDbMigrationConfig.Configuration); } return(dbMigrationConfig?.GetDbConfig()?.GetConfiguration()); }
private static void KillAllDatabaseConnections(IDbMigrationConfig dbMigrationConfig, IDbConnection dbConnection) { if (dbConnection.Exists(string.Format(DatabaseExistsSqlTemplate, dbMigrationConfig.DatabaseName.ToLower()))) { var sqlKillAllConnections = string.Format(DatabaseKillAllConnections, dbMigrationConfig.DatabaseName.ToLower()); dbConnection.ExecuteWithCommand(sqlKillAllConnections); } }
public DefaultOracleCustomMigrationProcessor( ILogger <DefaultOracleCustomMigrationProcessor> logger, IExtendedMigrationProcessorOracle processor, IDbMigrationConfig migrationConfig) { Logger = logger; Processor = processor; MigrationConfig = migrationConfig; Enabled = MigrationConfig.GetDbConfig().DbType == SupportedDatabaseTypes.Oracle; }
/// <summary> /// Initialize MigrationCallingName and MigrationAssembly from <paramref name="migrationConfig"/> /// </summary> /// <param name="migrationConfig"></param> public MigrationMetadata InitMetadata(IDbMigrationConfig migrationConfig) { MigrationCallingNameConfigField = migrationConfig?.GetMigrationName(); if (MigrationCallingNameConfigField.IsNotEmpty()) { MigrationName = MigrationCallingNameConfigField; } return(this); }
private static void CreateDatabase(IDbMigrationConfig dbMigrationConfig, IDbConnection dbConnection, Action before = null, Action <string> after = null) { if (!dbConnection.Exists(string.Format(DatabaseExistsSqlTemplate, dbMigrationConfig.DatabaseName.ToLower()))) { before?.Invoke(); var sqlCreateDatabase = string.Format(DatabaseCreateSqlTemplate, dbMigrationConfig.DatabaseName.ToLower(), dbMigrationConfig.DatabaseOwner.ToLower()); dbConnection.ExecuteWithCommand(sqlCreateDatabase); after?.Invoke(sqlCreateDatabase); } }
/// <summary> /// Fetch config value for <paramref name="key"/> /// </summary> /// <param name="migrationConfig"></param> /// <param name="key"></param> /// <returns></returns> protected string GetConfigValue(IDbMigrationConfig migrationConfig, string key) { if (migrationConfig == null) { return(null); } return(migrationConfig.GetAllMigrationConfigValues()?.GetValue(key) ?? migrationConfig.GetDbConfig().GetAllDatabaseConfigValues()?.GetValue(key)); }
private static string GetConnecionStringWithoutDatabase(this IDbMigrationConfig dbMigrationConfig) { var dbConfig = dbMigrationConfig.GetDbConfig(); return(string.Format(ConnectionStringWithoutDatabaseTemplate, dbConfig.AdminUser.ToLower(), dbConfig.AdminPassword, dbConfig.Hostname.ToLower(), dbConfig.Port, dbConfig.Pooling)); }
public static void DropDatabase(IDbMigrationConfig dbMigrationConfig, Action before = null, Action <string> after = null) { using (var dbConnection = CreateDbConnection(GetConnecionStringWithoutDatabase(dbMigrationConfig))) { dbConnection.Open(); try { DropDatabase(dbMigrationConfig, dbConnection, before, after); } finally { dbConnection.Close(); } } }
/// <summary> /// Constructor<br/> /// -------------<br/> /// SchemaPrefixId is assigned with value from parameter <paramref name="migrationConfig"/> - method <see cref="IDbConfigSchemaTargets.GetSchemaPrefixId()"/><br/> /// SchemaPrefixUniqueId is assigned with value from parameter <paramref name="migrationConfig"/> - method <see cref="IDbMigrationConfig.GetSchemaPrefixUniqueId()"/><br/> /// ShortName is assigned value from configuration database:migration:schemaPrefix:tables:{tableName}:ShortName or database:schemaPrefix:tables:{tableName}:ShortName<br/> /// GlobalId is assigned value from configuration database:migration:schemaPrefix:tables:{tableName}:GlobalId or database:schemaPrefix:tables:{tableName}:GlobalId<br/> /// KeyType is assigned value from configuration database:migration:schemaPrefix:tables:{tableName}:KeyType or database:schemaPrefix:tables:{tableName}:KeyType<br/> /// AnonymousOperation is assigned to "NOOP" OR value from configuration database:migration:schemaPrefix:tables:{tableName}:AnonymousOperation or database:schemaPrefix:tables:{tableName}:AnonymousOperation<br/> /// <br/> /// i.e:<br/> /// When configuration have database:migration:schemaPrefix:Id = "PR" and database:migration:schemaPrefix:UniqueId = "abode" <br/> /// SchemaPrefixId will be resolved to "PR"<br/> /// SchemaPrefixUniqueId will be resolved to "abode"<br/> /// <br/> /// When <paramref name="tableName"/> is "testing", <br/> /// ShortName is fetched from database:migration:schemaPrefix:tables:testing:ShortName or database:schemaPrefix:tables:testing:ShortName<br/> /// GlobalId is fetched from database:migration:schemaPrefix:tables:testing:GlobalId or database:schemaPrefix:tables:testing:GlobalId<br/> /// </summary> /// <param name="migrationConfig"></param> /// <param name="tableName"></param> public ChangeLogContext(IDbMigrationConfig migrationConfig, string tableName) : this(migrationConfig) { ShortName = migrationConfig.GetTableConfigValue("schemaPrefix:tables:{tableName}:shortName", tableName) ?? ShortName; GlobalId = migrationConfig.GetTableConfigValue("schemaPrefix:tables:{tableName}:globalId", tableName) ?? GlobalId; KeyType = migrationConfig.GetTableConfigValue("schemaPrefix:tables:{tableName}:keyType", tableName) ?? KeyType; AnonymousOperation = migrationConfig.GetTableConfigValue("schemaPrefix:tables:{tableName}:anonymousOperation", tableName) ?? AnonymousOperation; if (ShortName.IsNotEmpty() && migrationConfig != null) { ShortName = ShortName.GetPrefixedName(SchemaPrefixId); } var skipTriggersAndViews = migrationConfig.GetTableConfigValue("schemaPrefix:triggersAndViewsGeneration:tables:{tableName}", tableName); if (skipTriggersAndViews.IsEmpty()) { return; } if (Enum.TryParse(skipTriggersAndViews, true, out TriggersAndViewsGeneration enumValue)) { EnabledTriggersAndViewsGeneration = enumValue; } }
/// <inheritdoc /> public ExampleVersionTableMetaData(IDbMigrationConfig dbMigrationConfig) : base(dbMigrationConfig) { }
/// <summary> /// Constructor /// </summary> /// <param name="dbMigrationConfig"></param> public VersionTableMetaData(IDbMigrationConfig dbMigrationConfig) { DbMigrationConfig = dbMigrationConfig; }
public static void DropSchema(this IDbMigrationConfig dbConfig, IEnumerable <Assembly> assembliesWithMigrationModels, IServiceCollection serviceCollection = null) { DropSchema(provider => dbConfig, assembliesWithMigrationModels, serviceCollection); }
public static IMigrationRunner GetMigrationRunner(this IDbMigrationConfig dbConfig, IEnumerable <Assembly> assembliesWithMigrationModels, IServiceCollection serviceCollection = null) { return(GetMigrationRunner(provider => dbConfig, assembliesWithMigrationModels, serviceCollection)); }
public ExampleVersionTable(IDbMigrationConfig dbMigrationConfig) { DbMigrationConfig = dbMigrationConfig; }
public VersionTable(IDbMigrationConfig dbConfig) { DbConfig = dbConfig; }