Skip to content

Commit

Permalink
Added schema support for Postgresql. Fixes #197
Browse files Browse the repository at this point in the history
  • Loading branch information
droyad committed Feb 26, 2018
1 parent f1f19af commit 0f183a4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
46 changes: 40 additions & 6 deletions src/dbup-postgresql/PostgresqlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ public static class PostgresqlExtensions
/// <returns>
/// A builder for a database upgrader designed for PostgreSQL databases.
/// </returns>
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString)
{
return PostgresqlDatabase(new PostgresqlConnectionManager(connectionString));
}
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString)
=> PostgresqlDatabase(supported, connectionString, null);

/// <summary>
/// Creates an upgrader for PostgreSQL databases.
/// </summary>
/// <param name="supported">Fluent helper type.</param>
/// <param name="connectionString">PostgreSQL database connection string.</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(this SupportedDatabases supported, string connectionString, string schema)
=> PostgresqlDatabase(new PostgresqlConnectionManager(connectionString), schema);

/// <summary>
/// Creates an upgrader for PostgreSQL databases.
Expand All @@ -35,11 +45,22 @@ public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases su
/// A builder for a database upgrader designed for PostgreSQL databases.
/// </returns>
public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager)
=> PostgresqlDatabase(connectionManager, null);

/// <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, null, () => c.VariablesEnabled, c.ScriptPreprocessors, () => c.Journal));
builder.Configure(c => c.Journal = new PostgresqlTableJournal(() => c.ConnectionManager, () => c.Log, null, "schemaversions"));
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;
}
Expand Down Expand Up @@ -137,4 +158,17 @@ public static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase s
logger.WriteInformation(@"Created database {0}", databaseName);
}
}

/// <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;
}
}
5 changes: 2 additions & 3 deletions src/dbup-postgresql/PostgresqlScriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public class PostgresqlScriptExecutor : ScriptExecutor
}

protected override string GetVerifySchemaSql(string schema)
{
throw new NotSupportedException();
}
=> $@"CREATE SCHEMA IF NOT EXISTS {schema}";


protected override void ExecuteCommandsWithinExceptionHandler(int index, SqlScript script, Action excuteCommand)
{
Expand Down
3 changes: 3 additions & 0 deletions src/dbup-tests/ApprovalFiles/dbup-postgresql.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

public static class PostgresqlExtensions
{
public static DbUp.Builder.UpgradeEngineBuilder JournalToPostgresqlTable(this DbUp.Builder.UpgradeEngineBuilder builder, string schema, string table) { }
public static DbUp.Builder.UpgradeEngineBuilder PostgresqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString) { }
public static DbUp.Builder.UpgradeEngineBuilder PostgresqlDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString, string schema) { }
public static DbUp.Builder.UpgradeEngineBuilder PostgresqlDatabase(DbUp.Engine.Transactions.IConnectionManager connectionManager) { }
public static DbUp.Builder.UpgradeEngineBuilder PostgresqlDatabase(DbUp.Engine.Transactions.IConnectionManager connectionManager, string schema) { }
public static void PostgresqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString) { }
public static void PostgresqlDatabase(this DbUp.SupportedDatabasesForEnsureDatabase supported, string connectionString, DbUp.Engine.Output.IUpgradeLog logger) { }
}
Expand Down

0 comments on commit 0f183a4

Please sign in to comment.