SchemaMigrator
Compares entity schemas (from SchemaRegistry) with a live database and generates migration DDL.
メソッド
private static void AppendCompressionDiffs(IEnumerable<Schema> entities, IReadOnlyList<DbTableCompressionInfo> dbCompressions, IDictionary<string, Dictionary<string, DbColumnInfo>> dbTables, List<SchemaDiff> diffs)
SchemaMigrator.AppendCompressionDiffs(entities, dbCompressions, dbTables, diffs)
Compares the packing each entity declares with the packing its table actually uses. A table the database does not report is one this dialect cannot pack, and is passed over.
private static void AppendNamingDiffs(IEnumerable<Schema> entities, IReadOnlyList<DbForeignKeyInfo> dbForeignKeys, IReadOnlyList<DbIndexInfo> dbIndexes, IReadOnlyList<DbColumnInfo> dbColumns, List<SchemaDiff> diffs)
SchemaMigrator.AppendNamingDiffs(entities, dbForeignKeys, dbIndexes, dbColumns, diffs)
Audits the names of live-database primary keys, foreign keys and indexes against SchemaNaming. Report-only — renaming a constraint is destructive, so none of the diffs produced here is ever turned into DDL by Schema}).
public static Task ApplyAsync(DbConnection connection, string migrationSql, ISqlDialect dialect, CancellationToken cancellationToken)
result = SchemaMigrator.ApplyAsync(connection, migrationSql, dialect, cancellationToken)
Applies migration SQL directly to the database.
- connection
- Open database connection.
- migrationSql
- SQL to execute.
- dialect
- Dialect whose BatchSeparator is used to split the SQL into batches before execution. If the separator is empty, the SQL is sent as a single command.
- cancellationToken
- Cancellation token.
public static IReadOnlyList<SchemaDiff> Compare(IEnumerable<Schema> entities, IReadOnlyList<DbColumnInfo> dbColumns, ISqlDialect dialect, bool skipComputed, IReadOnlyList<DbForeignKeyInfo> dbForeignKeys, IReadOnlyList<DbIndexInfo> dbIndexes, bool detectExtraTables, bool checkNaming, IReadOnlyList<DbTableCompressionInfo> dbCompressions)
result = SchemaMigrator.Compare(entities, dbColumns, dialect, skipComputed, dbForeignKeys, dbIndexes, detectExtraTables, checkNaming, dbCompressions)
Compares entity schemas with database column metadata and returns differences.
- entities
- Entity schemas to compare.
- dbColumns
- Database columns read via CancellationToken).
- dialect
- SQL dialect for type name resolution.
- skipComputed
- Skip computed (calculated) database columns.
- dbForeignKeys
- Foreign keys read via CancellationToken). When supplied, missing/extra FK constraints are surfaced as MissingForeignKey / ExtraForeignKey diffs. Pass (the default) to skip FK comparison and preserve the pre-FK behaviour.
- dbIndexes
- Indexes read via CancellationToken). When supplied, missing/extra single-column indexes are surfaced as MissingIndex / ExtraIndex diffs. Pass (the default) to skip index comparison and preserve the pre-index behaviour.
- detectExtraTables
- When , tables present in the database but not mapped by any entity are surfaced as ExtraTable diffs. Off by default, because a partial entity set would otherwise flag every unrelated table.
- checkNaming
- When , primary keys, foreign keys and indexes whose database name does not follow SchemaNaming are surfaced as PrimaryKeyNameMismatch / ForeignKeyNameMismatch / IndexNameMismatch diffs. Off by default: a database that predates the convention violates it on nearly every object, which would drown the diffs a migration actually acts on. Requires the corresponding metadata ( / ) to be supplied.
戻り値: List of differences found.
public static Task<IReadOnlyList<SchemaDiff>> CompareAsync(IEnumerable<Schema> entities, DbConnection connection, ISqlDialect dialect, bool skipComputed, string tableSchema, bool detectExtraTables, bool checkNaming, CancellationToken cancellationToken)
result = SchemaMigrator.CompareAsync(entities, connection, dialect, skipComputed, tableSchema, detectExtraTables, checkNaming, cancellationToken)
Reads columns, foreign keys and indexes via in one shot and forwards to Compare(IEnumerable<Schema>,IReadOnlyList<DbColumnInfo>,ISqlDialect,bool,IReadOnlyList<DbForeignKeyInfo>,IReadOnlyList<DbIndexInfo>,bool,bool). Convenience for callers that want the full FK + index-aware comparison without orchestrating three metadata reads themselves.
private static void EmitCreateIndexesForTable(ISqlDialect dialect, StringBuilder sb, Schema schema, string tableName)
SchemaMigrator.EmitCreateIndexesForTable(dialect, sb, schema, tableName)
Emits CREATE INDEX statements declared on . One column may carry several [Index] attributes (its own single-column index plus participation in any number of named composites), so each SchemaColumnIndex entry across every column drives a separate index. Entries sharing Name are grouped into one composite ordered by Order; entries with Name each get their own single-column index named IX_{Table}_{Column}.
private static void EmitIndexByName(ISqlDialect dialect, StringBuilder sb, Schema schema, string tableName, string indexName)
SchemaMigrator.EmitIndexByName(dialect, sb, schema, tableName, indexName)
Emits one CREATE INDEX for the index identified by on . Used by the MissingIndex diff branch: the diff carries only the index name (composite name or the SchemaNaming fallback), and the schema supplies the participating columns / uniqueness via Indexes participations or the legacy IsUnique fallback.
public static string GenerateMigrationSql(ISqlDialect dialect, IReadOnlyList<SchemaDiff> diffs, IEnumerable<Schema> schemas)
result = SchemaMigrator.GenerateMigrationSql(dialect, diffs, schemas)
Generates migration SQL for the given differences.
- dialect
- SQL dialect.
- diffs
- Differences to generate SQL for.
- schemas
- Entity schemas (for column type lookup).
戻り値: Migration SQL script.
private static Dictionary<string, int> OrderNewTablesForCreate(IReadOnlyList<SchemaDiff> diffs, Dictionary<string, Schema> schemaMap)
result = SchemaMigrator.OrderNewTablesForCreate(diffs, schemaMap)
Assigns each new (MissingTable) table a creation rank such that a table is created after every other new table it references via a foreign key. Dependencies on already-existing tables are ignored (those are satisfied). Independent tables keep their original diff order; an FK cycle among new tables (only constructible on ALTER-capable dialects, where FKs are deferred anyway) is broken by falling back to the original order so the method always terminates.
private static Schema ResolveForeignKeyTarget(Type referencedEntityType)
result = SchemaMigrator.ResolveForeignKeyTarget(referencedEntityType)
Resolves the schema a foreign key should reference. A relation may target a view entity, but a database foreign key cannot reference a view — so a view is resolved to its backing table via its TableType.