diff --git a/Respawn.DatabaseTests/OracleTests.cs b/Respawn.DatabaseTests/OracleTests.cs index 30e081a..f4f0079 100644 --- a/Respawn.DatabaseTests/OracleTests.cs +++ b/Respawn.DatabaseTests/OracleTests.cs @@ -476,6 +476,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;")) 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);