Skip to content

Commit 55e2cd0

Browse files
authored
add principalSchema replacement. (#7)
Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com>
1 parent 492c62a commit 55e2cd0

3 files changed

Lines changed: 92 additions & 1 deletion

File tree

ef-migration-runtimeschema.Tests/RewriteTest.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,93 @@ protected override void Up(MigrationBuilder migrationBuilder)
178178
Assert.Equal(final, str, ignoreLineEndingDifferences: true);
179179
}
180180

181+
182+
[Fact]
183+
public void ShouldReplaceSchemaInForeignKeyTableConstraint()
184+
{
185+
var classString = @"
186+
public partial class AddUserName : Migration
187+
{
188+
189+
/// <inheritdoc />
190+
protected override void Up(MigrationBuilder migrationBuilder)
191+
{
192+
migrationBuilder.CreateTable(
193+
name: ""OrganizationUser"",
194+
schema: _schema.Schema,
195+
columns: table => new
196+
{
197+
OrganizationsId = table.Column<string>(type: ""text"", nullable: false),
198+
UsersId = table.Column<string>(type: ""text"", nullable: false)
199+
},
200+
constraints: table =>
201+
{
202+
table.PrimaryKey(""PK_OrganizationUser"", x => new { x.OrganizationsId, x.UsersId });
203+
table.ForeignKey(
204+
name: ""FK_OrganizationUser_Organization_OrganizationsId"",
205+
column: x => x.OrganizationsId,
206+
principalSchema: ""defaultSchema"",
207+
principalTable: ""Organization"",
208+
principalColumn: ""Id"",
209+
onDelete: ReferentialAction.Cascade);
210+
table.ForeignKey(
211+
name: ""FK_OrganizationUser_Users_UsersId"",
212+
column: x => x.UsersId,
213+
principalSchema: ""defaultSchema"",
214+
principalTable: ""Users"",
215+
principalColumn: ""Id"",
216+
onDelete: ReferentialAction.Cascade);
217+
});
218+
}";
219+
220+
var final = @"
221+
public partial class AddUserName : Migration
222+
{
223+
private readonly InterfaceName _schema;
224+
225+
/// <inheritdoc />
226+
public AddUserName(InterfaceName schema)
227+
{
228+
_schema = schema;
229+
}
230+
231+
/// <inheritdoc />
232+
protected override void Up(MigrationBuilder migrationBuilder)
233+
{
234+
migrationBuilder.CreateTable(
235+
name: ""OrganizationUser"",
236+
schema: _schema.Schema,
237+
columns: table => new
238+
{
239+
OrganizationsId = table.Column<string>(type: ""text"", nullable: false),
240+
UsersId = table.Column<string>(type: ""text"", nullable: false)
241+
},
242+
constraints: table =>
243+
{
244+
table.PrimaryKey(""PK_OrganizationUser"", x => new { x.OrganizationsId, x.UsersId });
245+
table.ForeignKey(
246+
name: ""FK_OrganizationUser_Organization_OrganizationsId"",
247+
column: x => x.OrganizationsId,
248+
principalSchema: _schema.Schema,
249+
principalTable: ""Organization"",
250+
principalColumn: ""Id"",
251+
onDelete: ReferentialAction.Cascade);
252+
table.ForeignKey(
253+
name: ""FK_OrganizationUser_Users_UsersId"",
254+
column: x => x.UsersId,
255+
principalSchema: _schema.Schema,
256+
principalTable: ""Users"",
257+
principalColumn: ""Id"",
258+
onDelete: ReferentialAction.Cascade);
259+
});
260+
}";
261+
262+
var result = MigrationCommand.RewriteSyntaxtNode(interfaceName, "testPath", classString);
263+
var str = result.ToFullString();
264+
265+
Assert.Equal(final, str, ignoreLineEndingDifferences: true);
266+
}
267+
268+
181269
}
182270
}

src/ef-migration-runtimeschema/SchemaRewriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class SchemaRewriter : CSharpSyntaxRewriter
1313
if (node.NameColon?.Name.Identifier.Text == "schema")
1414
return CreateSchemaRefnode(node);
1515

16+
if (node.NameColon?.Name.Identifier.Text == "principalSchema")
17+
return CreateSchemaRefnode(node);
18+
1619

1720
//Replace Schema Name for migrationBuilder.EnsureSchema()
1821
if (node.NameColon?.Name.Identifier.Text == "name"

src/ef-migration-runtimeschema/ef-migration-runtime-schema.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageTags>efcore cli</PackageTags>
2323
<PackageProjectUrl>https://github.com/jdevillard/ef-migration-runtime-schema</PackageProjectUrl>
2424
<RepositoryUrl>https://github.com/jdevillard/ef-migration-runtime-schema</RepositoryUrl>
25-
<PackageVersion>1.0.1</PackageVersion>
25+
<PackageVersion>1.0.2</PackageVersion>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

0 commit comments

Comments
 (0)