Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sequence reset for Oracle #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Respawn.DatabaseTests/OracleTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Xunit.Abstractions;

#if ORACLE
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still required because of problems with the Oracle container.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reintroduced Oracle compilation flag

namespace Respawn.DatabaseTests
{
using System;
Expand Down Expand Up @@ -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<int>("select id_seq.nextval from dual");
await _database.ExecuteScalarAsync<int>("select id_seq.nextval from dual");
await _database.ExecuteScalarAsync<int>("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<int>("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;"))
Expand Down Expand Up @@ -538,4 +557,3 @@ public async Task DisposeAsync()
}
}
}
#endif
24 changes: 20 additions & 4 deletions Respawn/OracleDbAdapter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -191,14 +191,30 @@ private static IEnumerable<string> BuildCommands(GraphBuilder graph)
yield return $"EXECUTE IMMEDIATE 'ALTER TABLE {rel.ParentTable.GetFullName(QuoteCharacter)} ENABLE CONSTRAINT {QuoteCharacter}{rel.Name}{QuoteCharacter}';";
}
}
public string BuildReseedSql(IEnumerable<Table> tablesToDelete) => throw new System.NotImplementedException();

public string BuildReseedSql(IEnumerable<Table> 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<TemporalTable> tablesToTurnOffSystemVersioning) => throw new System.NotImplementedException();

public string BuildTurnOnSystemVersioningCommandText(IEnumerable<TemporalTable> tablesToTurnOnSystemVersioning) => throw new System.NotImplementedException();

public Task<bool> CheckSupportsTemporalTables(DbConnection connection)
{
return Task.FromResult(false);
Expand Down