forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes npgsql#2787
- Loading branch information
Showing
2 changed files
with
198 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
test/EFCore.PG.Tests/Migrations/NpgsqlHistoryRepositoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations; | ||
|
||
public class NpgsqlHistoryRepositoryTest | ||
{ | ||
[ConditionalFact] | ||
public void GetCreateScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetCreateScript(); | ||
|
||
Assert.Equal( | ||
""" | ||
CREATE TABLE "__EFMigrationsHistory" ( | ||
"MigrationId" character varying(150) NOT NULL, | ||
"ProductVersion" character varying(32) NOT NULL, | ||
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId") | ||
); | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetCreateScript_works_with_schema() | ||
{ | ||
var sql = CreateHistoryRepository("my").GetCreateScript(); | ||
|
||
Assert.Equal( | ||
""" | ||
DO $EF$ | ||
BEGIN | ||
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'my') THEN | ||
CREATE SCHEMA my; | ||
END IF; | ||
END $EF$; | ||
CREATE TABLE my."__EFMigrationsHistory" ( | ||
"MigrationId" character varying(150) NOT NULL, | ||
"ProductVersion" character varying(32) NOT NULL, | ||
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId") | ||
); | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetCreateIfNotExistsScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetCreateIfNotExistsScript(); | ||
|
||
Assert.Equal( | ||
""" | ||
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" ( | ||
"MigrationId" character varying(150) NOT NULL, | ||
"ProductVersion" character varying(32) NOT NULL, | ||
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId") | ||
); | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetCreateIfNotExistsScript_works_with_schema() | ||
{ | ||
var sql = CreateHistoryRepository("my").GetCreateIfNotExistsScript(); | ||
|
||
Assert.Equal( | ||
""" | ||
DO $EF$ | ||
BEGIN | ||
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'my') THEN | ||
CREATE SCHEMA my; | ||
END IF; | ||
END $EF$; | ||
CREATE TABLE IF NOT EXISTS my."__EFMigrationsHistory" ( | ||
"MigrationId" character varying(150) NOT NULL, | ||
"ProductVersion" character varying(32) NOT NULL, | ||
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId") | ||
); | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetDeleteScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetDeleteScript("Migration1"); | ||
|
||
Assert.Equal( | ||
""" | ||
DELETE FROM "__EFMigrationsHistory" | ||
WHERE "MigrationId" = 'Migration1'; | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetInsertScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetInsertScript( | ||
new HistoryRow("Migration1", "7.0.0")); | ||
|
||
Assert.Equal( | ||
""" | ||
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") | ||
VALUES ('Migration1', '7.0.0'); | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetBeginIfNotExistsScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetBeginIfNotExistsScript("Migration1"); | ||
|
||
Assert.Equal( | ||
""" | ||
DO $EF$ | ||
BEGIN | ||
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = 'Migration1') THEN | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetBeginIfExistsScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetBeginIfExistsScript("Migration1"); | ||
|
||
Assert.Equal( | ||
""" | ||
DO $EF$ | ||
BEGIN | ||
IF EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = 'Migration1') THEN | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
[ConditionalFact] | ||
public void GetEndIfScript_works() | ||
{ | ||
var sql = CreateHistoryRepository().GetEndIfScript(); | ||
|
||
Assert.Equal( | ||
""" | ||
END IF; | ||
END $EF$; | ||
""", sql, ignoreLineEndingDifferences: true); | ||
} | ||
|
||
private static IHistoryRepository CreateHistoryRepository(string schema = null) | ||
=> new TestDbContext( | ||
new DbContextOptionsBuilder() | ||
.UseInternalServiceProvider(NpgsqlTestHelpers.Instance.CreateServiceProvider()) | ||
.UseNpgsql( | ||
new NpgsqlConnection("Host=localhost;Database=DummyDatabase"), | ||
b => b.MigrationsHistoryTable(HistoryRepository.DefaultTableName, schema)) | ||
.Options) | ||
.GetService<IHistoryRepository>(); | ||
|
||
private class TestDbContext : DbContext | ||
{ | ||
public TestDbContext(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Blog> Blogs { get; set; } | ||
|
||
[DbFunction("TableFunction")] | ||
public IQueryable<TableFunction> TableFunction() | ||
=> FromExpression(() => TableFunction()); | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
} | ||
} | ||
|
||
private class Blog | ||
{ | ||
public int Id { get; set; } | ||
} | ||
|
||
private class TableFunction | ||
{ | ||
public int Id { get; set; } | ||
public int BlogId { get; set; } | ||
public Blog Blog { get; set; } | ||
} | ||
} |