Skip to content

Commit

Permalink
#36 Support to drop temporal tables in SQL Server.
Browse files Browse the repository at this point in the history
  • Loading branch information
lecaillon committed Feb 23, 2018
1 parent 5b970e7 commit 51c0bee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
55 changes: 46 additions & 9 deletions src/Evolve/Dialect/SQLServer/SQLServerSchema.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Linq;
using System;
using System.Linq;
using Evolve.Connection;

namespace Evolve.Dialect.SQLServer
{
public class SQLServerSchema : Schema
{
private long _version = 0;

public SQLServerSchema(string schemaName, WrappedConnection wrappedConnection) : base(schemaName, wrappedConnection)
{
}
Expand Down Expand Up @@ -54,11 +57,12 @@ public override bool Erase()
DropDefaultConstraints();
DropProcedures();
DropViews();
DropSystemVersioning(); // SQLServerVersion >= 13
DropTables();
DropFunctions();
DropTypes();
DropSynonyms();
DropSequences(); // SQLServer >= 11
DropSequences(); // SQLServerVersion >= 11

return true;
}
Expand Down Expand Up @@ -108,6 +112,25 @@ protected void DropViews()
});
}

private void DropSystemVersioning()
{
if (SQLServerVersion < 13)
{
return;
}

string sql = "SELECT t.name as TABLE_NAME " +
"FROM sys.tables t " +
"INNER JOIN sys.schemas s ON s.schema_id = t.schema_id " +
$"WHERE s.name = '{Name}' " +
"AND t.temporal_type = 2";

_wrappedConnection.QueryForListOfString(sql).ToList().ForEach(table =>
{
_wrappedConnection.ExecuteNonQuery($"ALTER TABLE [{Name}].[{table}] SET (SYSTEM_VERSIONING = OFF)");
});
}

protected void DropTables()
{
string sql = $"SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='BASE TABLE' AND table_schema = '{Name}'";
Expand Down Expand Up @@ -146,20 +169,34 @@ protected void DropSynonyms()

protected void DropSequences()
{
string sqlversion = "SELECT CAST (CASE WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '8%' THEN 8 " +
"WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '9%' THEN 9 " +
"WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '10%' THEN 10 " +
"ELSE CAST(LEFT(CAST(SERVERPROPERTY ('productversion') as VARCHAR), 2) as int) " +
"END AS int)";

if (_wrappedConnection.QueryForLong(sqlversion) < 11)
if (SQLServerVersion < 11)
{
return;
}

string sql = $"SELECT sequence_name FROM INFORMATION_SCHEMA.SEQUENCES WHERE sequence_schema = '{Name}'";
_wrappedConnection.QueryForListOfString(sql).ToList().ForEach(s =>
{
_wrappedConnection.ExecuteNonQuery($"DROP SEQUENCE [{Name}].[{s}]");
});
}

private long SQLServerVersion
{
get
{
if (_version > 0)
{
return _version;
}

_version = _wrappedConnection.QueryForLong("SELECT CAST (CASE WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '8%' THEN 8 " +
"WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '9%' THEN 9 " +
"WHEN CAST(SERVERPROPERTY ('productversion') as VARCHAR) LIKE '10%' THEN 10 " +
"ELSE CAST(LEFT(CAST(SERVERPROPERTY ('productversion') as VARCHAR), 2) as int) " +
"END AS int)");
return _version;
}
}
}
}
1 change: 1 addition & 0 deletions src/Evolve/Evolve.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Every time you build your project, it will automatically ensure that your databa
<releaseNotes>- Full .NET Core 2.0 support.
- Add a new configuration parameter: Evolve.CommandTimeout.
- Normalize line endings when calculating checksums. (Thx to @mhelleborg)
- Support to drop temporal tables in SQL Server.
- New Cake build script.
- Add new Evolve package integration tests.
- Fix Linux evolve.json not found for lowercase file name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ CREATE TABLE TEST
DATE_TEST date NOT NULL,

FOREIGN KEY (DATE_TEST) REFERENCES CALENDRIER (JOUR) ON DELETE CASCADE,
)
);

CREATE TABLE dbo.Employee
(
[EmployeeID] int NOT NULL PRIMARY KEY CLUSTERED
, [Name] nvarchar(100) NOT NULL
, [Position] varchar(100) NOT NULL
, [Department] varchar(100) NOT NULL
, [Address] nvarchar(1024) NOT NULL
, [AnnualSalary] decimal (10,2) NOT NULL
, [ValidFrom] datetime2 (2) GENERATED ALWAYS AS ROW START
, [ValidTo] datetime2 (2) GENERATED ALWAYS AS ROW END
, PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory));

0 comments on commit 51c0bee

Please sign in to comment.