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 principalSchema replacement. #7

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 88 additions & 0 deletions ef-migration-runtimeschema.Tests/RewriteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,93 @@ protected override void Up(MigrationBuilder migrationBuilder)
Assert.Equal(final, str, ignoreLineEndingDifferences: true);
}


[Fact]
public void ShouldReplaceSchemaInForeignKeyTableConstraint()
{
var classString = @"
public partial class AddUserName : Migration
{

/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: ""OrganizationUser"",
schema: _schema.Schema,
columns: table => new
{
OrganizationsId = table.Column<string>(type: ""text"", nullable: false),
UsersId = table.Column<string>(type: ""text"", nullable: false)
},
constraints: table =>
{
table.PrimaryKey(""PK_OrganizationUser"", x => new { x.OrganizationsId, x.UsersId });
table.ForeignKey(
name: ""FK_OrganizationUser_Organization_OrganizationsId"",
column: x => x.OrganizationsId,
principalSchema: ""defaultSchema"",
principalTable: ""Organization"",
principalColumn: ""Id"",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: ""FK_OrganizationUser_Users_UsersId"",
column: x => x.UsersId,
principalSchema: ""defaultSchema"",
principalTable: ""Users"",
principalColumn: ""Id"",
onDelete: ReferentialAction.Cascade);
});
}";

var final = @"
public partial class AddUserName : Migration
{
private readonly InterfaceName _schema;

/// <inheritdoc />
public AddUserName(InterfaceName schema)
{
_schema = schema;
}

/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: ""OrganizationUser"",
schema: _schema.Schema,
columns: table => new
{
OrganizationsId = table.Column<string>(type: ""text"", nullable: false),
UsersId = table.Column<string>(type: ""text"", nullable: false)
},
constraints: table =>
{
table.PrimaryKey(""PK_OrganizationUser"", x => new { x.OrganizationsId, x.UsersId });
table.ForeignKey(
name: ""FK_OrganizationUser_Organization_OrganizationsId"",
column: x => x.OrganizationsId,
principalSchema: _schema.Schema,
principalTable: ""Organization"",
principalColumn: ""Id"",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: ""FK_OrganizationUser_Users_UsersId"",
column: x => x.UsersId,
principalSchema: _schema.Schema,
principalTable: ""Users"",
principalColumn: ""Id"",
onDelete: ReferentialAction.Cascade);
});
}";

var result = MigrationCommand.RewriteSyntaxtNode(interfaceName, "testPath", classString);
var str = result.ToFullString();

Assert.Equal(final, str, ignoreLineEndingDifferences: true);
}


}
}
3 changes: 3 additions & 0 deletions src/ef-migration-runtimeschema/SchemaRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class SchemaRewriter : CSharpSyntaxRewriter
if (node.NameColon?.Name.Identifier.Text == "schema")
return CreateSchemaRefnode(node);

if (node.NameColon?.Name.Identifier.Text == "principalSchema")
return CreateSchemaRefnode(node);


//Replace Schema Name for migrationBuilder.EnsureSchema()
if (node.NameColon?.Name.Identifier.Text == "name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageTags>efcore cli</PackageTags>
<PackageProjectUrl>https://github.com/jdevillard/ef-migration-runtime-schema</PackageProjectUrl>
<RepositoryUrl>https://github.com/jdevillard/ef-migration-runtime-schema</RepositoryUrl>
<PackageVersion>1.0.1</PackageVersion>
<PackageVersion>1.0.2</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down