Skip to content

Commit 9c131ea

Browse files
committed
Added test of PostgreSQL EnsureClean with migration
1 parent bad8e27 commit 9c131ea

File tree

7 files changed

+589
-4
lines changed

7 files changed

+589
-4
lines changed

DataLayer/BookApp/EfCode/BookContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected override void
3838
* The steps are:
3939
* a) Make sure the default project is Test
4040
* b) Use the PMC command
41-
* Add-Migration Initial -Context BookContext -OutputDir BookApp
41+
* Add-Migration Initial -Project DataLayer -Context BookContext -OutputDir BookApp\Migrations
4242
*
4343
* If you want to start afresh then:
4444
* a) Delete the current database

DataLayer/BookApp/EfCode/DesignTimeContextFactory.cs renamed to DataLayer/BookApp/EfCode/PostgreSqlContextFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
namespace DataLayer.BookApp.EfCode
88
{
9-
public class DesignTimeContextFactory : IDesignTimeDbContextFactory<BookContext>
9+
public class PostgreSqlContextFactory : IDesignTimeDbContextFactory<BookContext>
1010
{
1111
private const string connectionString =
12-
"Server=(localdb)\\mssqllocaldb;Database=TestSupport;Trusted_Connection=True;MultipleActiveResultSets=true";
12+
"host=localhost;Database=TestSupport-Migrate;Username=postgres;Password=LetMeIn";
1313

1414
public BookContext CreateDbContext(string[] args)
1515
{
1616
var optionsBuilder =
1717
new DbContextOptionsBuilder<BookContext>();
18-
optionsBuilder.UseSqlServer(connectionString);
18+
optionsBuilder.UseNpgsql(connectionString);
1919

2020
return new BookContext(optionsBuilder.Options);
2121
}

DataLayer/BookApp/Migrations/20211109142634_Initial.Designer.cs

Lines changed: 205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
4+
5+
namespace DataLayer.BookApp.Migrations
6+
{
7+
public partial class Initial : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.CreateTable(
12+
name: "Authors",
13+
columns: table => new
14+
{
15+
AuthorId = table.Column<int>(type: "integer", nullable: false)
16+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
17+
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false)
18+
},
19+
constraints: table =>
20+
{
21+
table.PrimaryKey("PK_Authors", x => x.AuthorId);
22+
});
23+
24+
migrationBuilder.CreateTable(
25+
name: "Books",
26+
columns: table => new
27+
{
28+
BookId = table.Column<int>(type: "integer", nullable: false)
29+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
30+
Title = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
31+
Description = table.Column<string>(type: "text", nullable: true),
32+
PublishedOn = table.Column<DateTime>(type: "date", nullable: false),
33+
Publisher = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
34+
Price = table.Column<decimal>(type: "numeric(9,2)", nullable: false),
35+
ImageUrl = table.Column<string>(type: "character varying(512)", unicode: false, maxLength: 512, nullable: true),
36+
SoftDeleted = table.Column<bool>(type: "boolean", nullable: false)
37+
},
38+
constraints: table =>
39+
{
40+
table.PrimaryKey("PK_Books", x => x.BookId);
41+
});
42+
43+
migrationBuilder.CreateTable(
44+
name: "BookAuthor",
45+
columns: table => new
46+
{
47+
BookId = table.Column<int>(type: "integer", nullable: false),
48+
AuthorId = table.Column<int>(type: "integer", nullable: false),
49+
Order = table.Column<byte>(type: "smallint", nullable: false)
50+
},
51+
constraints: table =>
52+
{
53+
table.PrimaryKey("PK_BookAuthor", x => new { x.BookId, x.AuthorId });
54+
table.ForeignKey(
55+
name: "FK_BookAuthor_Authors_AuthorId",
56+
column: x => x.AuthorId,
57+
principalTable: "Authors",
58+
principalColumn: "AuthorId",
59+
onDelete: ReferentialAction.Cascade);
60+
table.ForeignKey(
61+
name: "FK_BookAuthor_Books_BookId",
62+
column: x => x.BookId,
63+
principalTable: "Books",
64+
principalColumn: "BookId",
65+
onDelete: ReferentialAction.Cascade);
66+
});
67+
68+
migrationBuilder.CreateTable(
69+
name: "PriceOffers",
70+
columns: table => new
71+
{
72+
PriceOfferId = table.Column<int>(type: "integer", nullable: false)
73+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
74+
NewPrice = table.Column<decimal>(type: "numeric(9,2)", nullable: false),
75+
PromotionalText = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
76+
BookId = table.Column<int>(type: "integer", nullable: false)
77+
},
78+
constraints: table =>
79+
{
80+
table.PrimaryKey("PK_PriceOffers", x => x.PriceOfferId);
81+
table.ForeignKey(
82+
name: "FK_PriceOffers_Books_BookId",
83+
column: x => x.BookId,
84+
principalTable: "Books",
85+
principalColumn: "BookId",
86+
onDelete: ReferentialAction.Cascade);
87+
});
88+
89+
migrationBuilder.CreateTable(
90+
name: "Review",
91+
columns: table => new
92+
{
93+
ReviewId = table.Column<int>(type: "integer", nullable: false)
94+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
95+
VoterName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
96+
NumStars = table.Column<int>(type: "integer", nullable: false),
97+
Comment = table.Column<string>(type: "text", nullable: true),
98+
BookId = table.Column<int>(type: "integer", nullable: false)
99+
},
100+
constraints: table =>
101+
{
102+
table.PrimaryKey("PK_Review", x => x.ReviewId);
103+
table.ForeignKey(
104+
name: "FK_Review_Books_BookId",
105+
column: x => x.BookId,
106+
principalTable: "Books",
107+
principalColumn: "BookId",
108+
onDelete: ReferentialAction.Cascade);
109+
});
110+
111+
migrationBuilder.CreateIndex(
112+
name: "IX_BookAuthor_AuthorId",
113+
table: "BookAuthor",
114+
column: "AuthorId");
115+
116+
migrationBuilder.CreateIndex(
117+
name: "IX_Books_PublishedOn",
118+
table: "Books",
119+
column: "PublishedOn");
120+
121+
migrationBuilder.CreateIndex(
122+
name: "IX_PriceOffers_BookId",
123+
table: "PriceOffers",
124+
column: "BookId",
125+
unique: true);
126+
127+
migrationBuilder.CreateIndex(
128+
name: "IX_Review_BookId",
129+
table: "Review",
130+
column: "BookId");
131+
}
132+
133+
protected override void Down(MigrationBuilder migrationBuilder)
134+
{
135+
migrationBuilder.DropTable(
136+
name: "BookAuthor");
137+
138+
migrationBuilder.DropTable(
139+
name: "PriceOffers");
140+
141+
migrationBuilder.DropTable(
142+
name: "Review");
143+
144+
migrationBuilder.DropTable(
145+
name: "Authors");
146+
147+
migrationBuilder.DropTable(
148+
name: "Books");
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)