Skip to content

Commit

Permalink
fix(efcore): correct column type mismatch with CSVs
Browse files Browse the repository at this point in the history
correct mismatch on CSVs, such as decimal(18, 2) vs decimal(18,2)
  • Loading branch information
jayharris committed Jan 22, 2022
1 parent 988bd36 commit 955a818
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 58 deletions.
Expand Up @@ -66,7 +66,7 @@ public class SchemaValidator {
}

var columnTypesMatch =
dbColumn.StoreType.Equals(entityColumn.ColumnType, StringComparison.OrdinalIgnoreCase);
dbColumn.StoreType.Replace(", ",",").Equals(entityColumn.ColumnType.Replace(", ",","), StringComparison.OrdinalIgnoreCase);
if (!columnTypesMatch) {
valErrors.Add(
$"Column type mismatch in {entityTable.TableName} for column {entityColumn.ColumnName}. Found: {dbColumn.StoreType.ToLowerInvariant()}, Expected {entityColumn.ColumnType.ToLowerInvariant()}");
Expand Down
Expand Up @@ -66,7 +66,7 @@ public class SchemaValidator {
}

var columnTypesMatch =
dbColumn.StoreType.Equals(persistedColumn.GetColumnType(), StringComparison.OrdinalIgnoreCase);
dbColumn.StoreType.Replace(", ",",").Equals(persistedColumn.GetColumnType().Replace(", ",","), StringComparison.OrdinalIgnoreCase);
if (!columnTypesMatch) {
valErrors.Add(
$"Column type mismatch in {persistedType.GetTableName()} for column {persistedColumn.GetColumnName()}. Found: {dbColumn.StoreType.ToLowerInvariant()}, Expected {persistedColumn.GetColumnType().ToLowerInvariant()}");
Expand Down
Expand Up @@ -70,7 +70,7 @@ public class SchemaValidator {
}

var columnTypesMatch =
dbColumn.StoreType.Equals(persistedColumn.GetColumnType(), StringComparison.OrdinalIgnoreCase);
dbColumn.StoreType.Replace(", ",",").Equals(persistedColumn.GetColumnType().Replace(", ",","), StringComparison.OrdinalIgnoreCase);
if (!columnTypesMatch) {
valErrors.Add(
$"Column type mismatch in {persistedType.GetTableName()} for column {persistedColumn.GetColumnName(StoreObjectIdentifier.Table(persistedType.GetTableName(), null))}. Found: {dbColumn.StoreType.ToLowerInvariant()}, Expected {persistedColumn.GetColumnType().ToLowerInvariant()}");
Expand All @@ -90,7 +90,7 @@ public class SchemaValidator {
}

var columnTypesMatch =
dbColumn.StoreType.Equals(persistedColumn.GetColumnType(), StringComparison.OrdinalIgnoreCase);
dbColumn.StoreType.Replace(", ",",").Equals(persistedColumn.GetColumnType().Replace(", ",","), StringComparison.OrdinalIgnoreCase);
if (!columnTypesMatch) {
valErrors.Add(
$"Column type mismatch in {persistedType.GetViewName()} for column {persistedColumn.GetColumnName(StoreObjectIdentifier.View(persistedType.GetViewName(), null))}. Found: {dbColumn.StoreType.ToLowerInvariant()}, Expected {persistedColumn.GetColumnType().ToLowerInvariant()}");
Expand Down
Expand Up @@ -109,11 +109,32 @@ public class MigrationsMissingForeignKeys : Migration {
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId", col => col.AsInt32().NotNullable().Indexed("IX_TableBasedEntity_RoleId"))
;
IfDatabase(dbType => string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsString(20000).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId"))
;
});
IfDatabase(dbType => !string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsDecimal(18, 2).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId"))
;
});

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");

Expand Down
Expand Up @@ -113,15 +113,38 @@ public class MigrationsMissingIndexes : Migration {
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;
IfDatabase(dbType => string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsString(20000).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});
IfDatabase(dbType => !string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsDecimal(18, 2).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");

Expand Down
Expand Up @@ -128,16 +128,40 @@ public class MigrationsMissingViews : Migration {
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;
IfDatabase(dbType => string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsString(20000).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});
IfDatabase(dbType => !string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsDecimal(18, 2).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});

Create.Table("TableBasedChildEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedChildEntity"))
Expand Down
Expand Up @@ -128,16 +128,40 @@ public class MigrationsWithIncorrectColumnNullability : Migration {
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;
IfDatabase(dbType => string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsString(20000).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});
IfDatabase(dbType => !string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsDecimal(18, 2).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");

Expand Down
Expand Up @@ -128,16 +128,40 @@ public class MigrationsWithIncorrectColumnTypes : Migration {
.WithColumn("Value", col => col.AsStringMax().Nullable())
;

Create.Table("TableBasedEntity")
.WithColumn("Id", col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId", "AspNetRoles", "Id")
.OnDelete(Rule.Cascade))
;
IfDatabase(dbType => string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsString(20000).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});
IfDatabase(dbType => !string.Equals(dbType, "SQLite-Test", StringComparison.InvariantCultureIgnoreCase))
.Delegate(() => {
Create.Table("TableBasedEntity")
.WithColumn("Id",
col => col.AsInt32().NotNullable().PrimaryKey("PK_TableBasedEntity"))
.WithColumn("Field", col => col.AsString(256).Nullable())
.WithColumn("NumberValue", col => col.AsDecimal(18, 3).NotNullable())
.WithColumn("RoleId",
col => col.AsInt32()
.NotNullable()
.Indexed("IX_TableBasedEntity_RoleId")
.ForeignKey("FK_TableBasedEntity_AspNetRoles_RoleId",
"AspNetRoles",
"Id")
.OnDelete(Rule.Cascade))
;
});

Execute.Sql(@"CREATE VIEW ViewBasedEntities AS SELECT Id, Field, RoleId FROM TableBasedEntity");

Expand Down

0 comments on commit 955a818

Please sign in to comment.