From ced579bafad0d1aae2d582a006c133790a5d2d6d Mon Sep 17 00:00:00 2001 From: Diego Bonura Date: Mon, 26 Jun 2023 16:57:00 +0200 Subject: [PATCH 1/2] Add sequence reset for Oracle --- Respawn.DatabaseTests/OracleTests.cs | 22 ++++++++++++++++++++-- Respawn/OracleDbAdapter.cs | 24 ++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Respawn.DatabaseTests/OracleTests.cs b/Respawn.DatabaseTests/OracleTests.cs index 30e081a..99e9bca 100644 --- a/Respawn.DatabaseTests/OracleTests.cs +++ b/Respawn.DatabaseTests/OracleTests.cs @@ -1,6 +1,5 @@ using Xunit.Abstractions; -#if ORACLE namespace Respawn.DatabaseTests { using System; @@ -476,6 +475,26 @@ public async Task ShouldIncludeSchemas() await DropUser(userB); } + [SkipOnCI] + public async Task ShouldResetSequences() + { + await _database.ExecuteAsync("create table \"foo\" (value int, primary key (value))"); + await _database.ExecuteAsync("CREATE SEQUENCE id_seq INCREMENT BY 1"); + await _database.ExecuteScalarAsync("select id_seq.nextval from dual"); + await _database.ExecuteScalarAsync("select id_seq.nextval from dual"); + await _database.ExecuteScalarAsync("select id_seq.nextval from dual"); + + var respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions + { + DbAdapter = DbAdapter.Oracle, + SchemasToInclude = new[] { _createdUser }, + WithReseed = true + }); + await respawner.ResetAsync(_connection); + + (await _database.ExecuteScalarAsync("select id_seq.nextval from dual")).ShouldBe(1); + } + private static async Task CreateUser(string userName) { using (var connection = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=10521))(CONNECT_DATA=(SID=xe)));User Id=system;Password=oracle;")) @@ -538,4 +557,3 @@ public async Task DisposeAsync() } } } -#endif \ No newline at end of file diff --git a/Respawn/OracleDbAdapter.cs b/Respawn/OracleDbAdapter.cs index 9ae4575..e0df1c3 100644 --- a/Respawn/OracleDbAdapter.cs +++ b/Respawn/OracleDbAdapter.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; +using Respawn.Graph; +using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Threading.Tasks; -using Respawn.Graph; namespace Respawn { @@ -191,14 +191,30 @@ private static IEnumerable BuildCommands(GraphBuilder graph) yield return $"EXECUTE IMMEDIATE 'ALTER TABLE {rel.ParentTable.GetFullName(QuoteCharacter)} ENABLE CONSTRAINT {QuoteCharacter}{rel.Name}{QuoteCharacter}';"; } } - public string BuildReseedSql(IEnumerable tablesToDelete) => throw new System.NotImplementedException(); + + public string BuildReseedSql(IEnumerable
tablesToDelete) + { + return @" + DECLARE + sql_txt VARCHAR2 (1000); + BEGIN + FOR s IN (select sequence_name, min_value from user_sequences) + LOOP + + sql_txt := 'ALTER SEQUENCE ' || s.sequence_name || ' restart start with ' || s.min_value; + + EXECUTE IMMEDIATE sql_txt; + END LOOP; + END; + "; + } public string BuildTemporalTableCommandText(RespawnerOptions options) => throw new System.NotImplementedException(); public string BuildTurnOffSystemVersioningCommandText(IEnumerable tablesToTurnOffSystemVersioning) => throw new System.NotImplementedException(); public string BuildTurnOnSystemVersioningCommandText(IEnumerable tablesToTurnOnSystemVersioning) => throw new System.NotImplementedException(); - + public Task CheckSupportsTemporalTables(DbConnection connection) { return Task.FromResult(false); From be6938dda047890c64d1c47210ecb8b75431d5ac Mon Sep 17 00:00:00 2001 From: Diego Bonura Date: Wed, 26 Jul 2023 18:51:26 +0200 Subject: [PATCH 2/2] Revert compilation flag for Oracle --- Respawn.DatabaseTests/OracleTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Respawn.DatabaseTests/OracleTests.cs b/Respawn.DatabaseTests/OracleTests.cs index 99e9bca..f4f0079 100644 --- a/Respawn.DatabaseTests/OracleTests.cs +++ b/Respawn.DatabaseTests/OracleTests.cs @@ -1,5 +1,6 @@ using Xunit.Abstractions; +#if ORACLE namespace Respawn.DatabaseTests { using System; @@ -557,3 +558,4 @@ public async Task DisposeAsync() } } } +#endif \ No newline at end of file