public async Task create_a_schema_on_the_fly_for_migrations() { await theConnection.OpenAsync(); await theConnection.DropSchema("one"); await theConnection.DropSchema("two"); var table1 = new Table("one.table1"); table1.AddColumn <string>("name").AsPrimaryKey(); var table2 = new Table("two.table2"); table2.AddColumn <string>("name").AsPrimaryKey(); var migration = await SchemaMigration.Determine(theConnection, new ISchemaObject[] { table1, table2 }); migration.Difference.ShouldBe(SchemaPatchDifference.Create); await migration.ApplyAll(theConnection, new DdlRules(), AutoCreate.CreateOrUpdate); (await table1.FetchExisting(theConnection)).ShouldNotBeNull(); (await table2.FetchExisting(theConnection)).ShouldNotBeNull(); }
protected override async Task <bool> execute(IDocumentStore store, PatchInput input) { try { await store.Schema.AssertDatabaseMatchesConfigurationAsync().ConfigureAwait(false); input.WriteLine(ConsoleColor.Green, "No differences were detected between the Marten configuration and the database"); return(true); } catch (SchemaValidationException) { var patch = await store.Schema.CreateMigrationAsync().ConfigureAwait(false); input.WriteLine(ConsoleColor.Green, "Wrote a patch file to " + input.FileName); var rules = store.Options.As <StoreOptions>().Advanced.DdlRules; rules.IsTransactional = input.TransactionalScriptFlag; rules.WriteTemplatedFile(input.FileName, (r, w) => patch.WriteAllUpdates(w, r, AutoCreate.CreateOrUpdate)); var dropFile = input.DropFlag ?? SchemaMigration.ToDropFileName(input.FileName); input.WriteLine(ConsoleColor.Green, "Wrote the drop file to " + dropFile); rules.WriteTemplatedFile(dropFile, (r, w) => patch.WriteAllRollbacks(w, r)); return(true); } }
private async Task AssertRollbackIsSuccessful(params Table[] otherTables) { await ResetSchema(); foreach (var table in otherTables) { await CreateSchemaObjectInDatabase(table); } await CreateSchemaObjectInDatabase(initial); await Task.Delay(100.Milliseconds()); var delta = await configured.FindDelta(theConnection); var migration = new SchemaMigration(new ISchemaObjectDelta[] { delta }); await migration.ApplyAll(theConnection, new DdlRules(), AutoCreate.CreateOrUpdate); await Task.Delay(100.Milliseconds()); await migration.RollbackAll(theConnection, new DdlRules()); var delta2 = await initial.FindDelta(theConnection); delta2.Difference.ShouldBe(SchemaPatchDifference.None); }
public async Task WriteMigrationFileByTypeAsync(string directory) { var system = new FileSystem(); system.DeleteDirectory(directory); system.CreateDirectory(directory); var features = _features.AllActiveFeatures(_tenant).ToArray(); writeDatabaseSchemaGenerationScript(directory, system, features); using var conn = _tenant.CreateConnection(); await conn.OpenAsync(); foreach (var feature in features) { var migration = await SchemaMigration.Determine(conn, feature.Objects); if (migration.Difference == SchemaPatchDifference.None) { continue; } var file = directory.AppendPath(feature.Identifier + ".sql"); DdlRules.WriteTemplatedFile(file, (r, w) => { migration.WriteAllUpdates(w, r, AutoCreate.CreateOrUpdate); }); } }
public static string UpdateSql(this SchemaMigration migration) { var writer = new StringWriter(); migration.WriteAllUpdates(writer, new PostgresqlMigrator(), AutoCreate.CreateOrUpdate); return(writer.ToString()); }
public static string RollbackSql(this SchemaMigration migration) { var writer = new StringWriter(); migration.WriteAllRollbacks(writer, new PostgresqlMigrator()); return(writer.ToString()); }
public async Task can_rollback_a_function_change() { await ResetSchema(); await CreateSchemaObjectInDatabase(theHiloTable); var function = Function.ForSql(theFunctionBody); await CreateSchemaObjectInDatabase(function); var different = Function.ForSql(theEvenDifferentBody); var delta = await different.FindDelta(theConnection); var migration = new SchemaMigration(delta); await migration.ApplyAll(theConnection, new DdlRules(), AutoCreate.CreateOrUpdate); await migration.RollbackAll(theConnection, new DdlRules()); // Should be back to the original function var lastDelta = await function.FindDelta(theConnection); lastDelta.Difference.ShouldBe(SchemaPatchDifference.None); }
public async Task <SchemaMigration> CreateMigrationAsync() { var @objects = _features.AllActiveFeatures(_tenant).SelectMany(x => x.Objects).ToArray(); using var conn = _tenant.CreateConnection(); await conn.OpenAsync(); return(await SchemaMigration.Determine(conn, @objects)); }
private async Task writeAndApplyPatch(AutoCreate autoCreate, DocumentTable table) { var migration = await SchemaMigration.Determine(_conn, new ISchemaObject[] { table }); if (migration.Difference != SchemaPatchDifference.None) { await migration.ApplyAll(_conn, new DdlRules(), autoCreate); } }
public async Task <SchemaMigration> CreateMigrationAsync(Type documentType) { var mapping = _features.MappingFor(documentType); using var conn = _tenant.CreateConnection(); await conn.OpenAsync(); var migration = await SchemaMigration.Determine(conn, mapping.Schema.Objects); return(migration); }
public void Configuration(IAppBuilder app) { SchemaMigration.Execute(); HttpConfiguration httpConfiguration = new HttpConfiguration(); ConfigureAuth(app); WebApiConfig.Register(httpConfiguration); app.UseWebApi(httpConfiguration); // Perhaps there's a better place for this call? scheduler.Start(new System.Threading.TimerCallback(ImportLatestLogs), 60); }
public static void ResetDatabase() { FileInfo dbFile = new FileInfo("../../../Domain/Database/CodeReaction.sqlite"); if (dbFile.Exists == false) { throw new FileNotFoundException(); } dbFile.CopyTo("CodeReaction.sqlite", true); SchemaMigration.Execute(); }
public async Task rollback_a_function_creation() { await ResetSchema(); await CreateSchemaObjectInDatabase(theHiloTable); var function = Function.ForSql(theFunctionBody); var delta = await function.FindDelta(theConnection); var migration = new SchemaMigration(delta); await migration.ApplyAll(theConnection, new DdlRules(), AutoCreate.CreateOrUpdate); (await theConnection.FunctionExists(function.Identifier)).ShouldBeTrue(); await migration.RollbackAll(theConnection, new DdlRules()); (await theConnection.FunctionExists(function.Identifier)).ShouldBeFalse(); }
public async Task can_create_extension() { await ResetSchema(); var extension = new Extension("plv8"); await DropSchemaObjectInDatabase(extension); var migration = await SchemaMigration.Determine(theConnection, extension); migration.Difference.ShouldBe(SchemaPatchDifference.Create); await this.CreateSchemaObjectInDatabase(extension); migration = await SchemaMigration.Determine(theConnection, extension); migration.Difference.ShouldBe(SchemaPatchDifference.None); }
public void MigrateAll() { using (var db = new DbCodeReview()) { try { db.SchemaVersion.Any(); Assert.Fail("Schema vesions exist !"); } catch { } } SchemaMigration.Execute(); using (var db = new DbCodeReview()) { Assert.AreEqual(Index.Migrations.Keys.Max(), db.SchemaVersion.Max(v => v.Number)); Assert.AreEqual(Index.Migrations.Count, db.SchemaVersion.Count()); } }
public async Task WriteMigrationFileAsync(string filename) { if (!Path.IsPathRooted(filename)) { filename = AppContext.BaseDirectory.AppendPath(filename); } var patch = await CreateMigrationAsync(); DdlRules.WriteTemplatedFile(filename, (r, w) => { patch.WriteAllUpdates(w, r, AutoCreate.All); }); var dropFile = SchemaMigration.ToDropFileName(filename); DdlRules.WriteTemplatedFile(dropFile, (r, w) => { patch.WriteAllRollbacks(w, r); }); }
public async Task rollback_existing() { await ResetSchema(); await CreateSchemaObjectInDatabase(theHiloTable); var function = Function.ForSql(theFunctionBody); await CreateSchemaObjectInDatabase(function); var toRemove = Function.ForRemoval(function.Identifier); var delta = await toRemove.FindDelta(theConnection); delta.Difference.ShouldBe(SchemaPatchDifference.Update); var migration = new SchemaMigration(delta); await migration.ApplyAll(theConnection, new DdlRules(), AutoCreate.CreateOrUpdate); await migration.RollbackAll(theConnection, new DdlRules()); (await theConnection.FunctionExists(toRemove.Identifier)).ShouldBeTrue(); }
public void translates_the_file_name() { SchemaMigration.ToDropFileName("update.sql").ShouldBe("update.drop.sql"); SchemaMigration.ToDropFileName("1.update.sql").ShouldBe("1.update.drop.sql"); SchemaMigration.ToDropFileName("folder\\1.update.sql").ShouldBe("folder\\1.update.drop.sql"); }