V10: fix content version cleanup dto migration (#13788)

* Add DoesPrimaryKeyExist method

* Add primary key name to dto

* Add extension method PrimaryKeyExists

* Dont use sql_master table

* Implement DoesPrimaryKeyExists

* Rename without plural s

* Refactor to get proper primary key name and not column name

* Use primary key name and not column name

* Dont filter tables in memory

* Remove .Where

* Don't select all, select only sql

* throw NotImplementedException by default

* USe virtual instead of abstract to avoid breaking change

* Reinstert primary key attribute

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2023-02-07 13:46:14 +01:00
parent e3135bd97e
commit 3d47cac9ac
7 changed files with 47 additions and 9 deletions

View File

@@ -272,6 +272,13 @@ where tbl.[name]=@0 and col.[name]=@1;",
return !constraintName.IsNullOrWhiteSpace();
}
public override bool DoesPrimaryKeyExist(IDatabase db, string tableName, string primaryKeyName)
{
IEnumerable<SqlPrimaryKey>? keys = db.Fetch<SqlPrimaryKey>($"select * from sysobjects where xtype='pk' and parent_obj in (select id from sysobjects where name='{tableName}')")
.Where(x => x.Name == primaryKeyName);
return keys.FirstOrDefault() is not null;
}
public override bool DoesTableExist(IDatabase db, string tableName)
{
var result =
@@ -453,4 +460,9 @@ _sqlInspector ??= new SqlInspectionUtilities();
}
#endregion
private class SqlPrimaryKey
{
public string Name { get; set; } = null!;
}
}