Repository navigation
Open
Labels
capabilityA huge capability implementation beyond features and enhancements.maintainers-onlyReserved for maintainers. Please do not submit a PR unless discussed and approved first.priorityTop priority feature or things to dotodoThings to be done in the future
Description
User Story
As a RepoDB developer moving data between databases,
I want to copy a table's schema (columns, keys, indexes, constraints) from a source database to a destination database,
so that the destination table exists and matches the source before the data is copied, even across different database engines.
Why This Matters
As RepoDB is expanding to a Productivity Platform, and to support dynamic schema extraction and creation, we are required to deliver the Schema Mover capability. Without it, data copy operations can only target tables that already exist, and users must hand-write DDL for every destination database.
Proposed Architecture
Source connection Destination connection
| ^
v |
SchemaReaderMapper --> ISchemaReader --> TableSchema --> ISchemaComposer <-- SchemaComposerMapper
(by connection type) (reads catalog) (neutral model) (emits DDL) (by connection type)
|
CopySchemaTo (orchestrates, returns CopySchemaResult)
- The reader and composer are resolved independently by connection type, so any source can target any destination (e.g. SQL Server to PostgreSQL) once both providers exist.
TableSchemais the only contract between them, so providers never depend on each other.CopySchemaToreads the source schema, composes the DDL statements and runs them, in order, on the destination.- The schema mover is separate from the data mover (
CopyDataTo/MoveDataTo) and can be used before it.
Schema/
RepoDb.Schema.Core/ interfaces, models, mappers, CopySchemaTo, CopySchemaResult
RepoDb.Schema.SqlServer/ reader, composer, type resolvers, bootstrap
RepoDb.Schema.<Provider>/ same shape per database engine
Sample Usage
using var source = new SqlConnection(sourceConnectionString);
using var destination = new NpgsqlConnection(destinationConnectionString);
// Copy the schema of a table (by name or by entity type)
var result = source.CopySchemaTo("Customer",
destination,
tableExistenceBehavior: CopySchemaExistsBehavior.SkipOnExists);
// Async and entity-based variants
var result = await source.CopySchemaToAsync<Customer>(destination).ConfigureAwait(false);Proposed Objects
| Object | Role |
|---|---|
ISchemaReader |
Reads a TableSchema and each of its parts (columns, primary key, indexes, foreign keys, unique and check constraints). Also TableExists, GetTables, GetDependencyOrder. Sync and async. |
ISchemaComposer |
Turns a TableSchema into DDL: ComposeSchema, ComposeCreateTable, ComposeCreateIndex, ComposeAddForeignKey, ComposeAddColumn, ComposeDropTable, ComposeTypeName. |
SchemaReaderMapper / SchemaComposerMapper |
Register and resolve a reader / composer per DbConnection type. |
TableSchema |
Table name, schema name, columns, primary key, indexes, foreign keys, unique and check constraints. |
ColumnInfo, PrimaryKeyInfo, IndexInfo, ForeignKeyInfo, UniqueConstraintInfo, CheckConstraintInfo |
Neutral descriptions of each part of a table. |
CopySchemaExistsBehavior |
What to do when the destination table exists: SkipOnExists, AlignOnExists, ThrowOnExists, DropOnExists. |
CopySchemaOutcome |
What happened: Created, Skipped, Aligned, Dropped. |
CopySchemaResult |
Source and destination info, timings, object counts, generated Script, AddedColumns, AddedIndexes, Warnings. |
<Provider>SchemaReader / <Provider>SchemaComposer |
Per-engine implementations, starting with SQL Server. |
<Provider>SchemaBootstrap |
Initialize() registers the provider's reader and composer in the mappers. |
Scope
| # | Item | Notes |
|---|---|---|
| 1 | Core contracts, models and mappers | ISchemaReader, ISchemaComposer, TableSchema and its parts, both mappers. |
| 2 | CopySchemaTo (sync and async) |
Applies CopySchemaExistsBehavior; returns CopySchemaResult. |
| 3 | SQL Server provider | Reader, composer, type resolvers, bootstrap. |
| 4 | Further providers | PostgreSQL, MySQL and SQLite first. |
| 5 | Cross-engine type mapping | Map each column type to the destination engine; add a Warnings entry when the mapping is lossy. |
| 6 | Multi-table copy in dependency order | Use GetDependencyOrder so foreign keys are created after their parent tables. |
| 7 | Destination name override | Allow a different table or schema name on the destination. |
| 8 | MoveSchemaTo |
Copy the schema, then drop the source table only after success. |
| 9 | Tests and documentation | Unit tests for composers and mappers; integration tests per provider; READMEs. |
Acceptance Criteria
-
CopySchemaToreturns the correctCopySchemaOutcomefor all fourCopySchemaExistsBehaviorvalues. - A SQL Server table copied to SQL Server matches the source: columns, identity, defaults, primary key, indexes, foreign keys, unique and check constraints.
- A missing reader or composer mapping throws
MissingMappingExceptionwith a clear message. -
DropOnExistsis documented as destructive and covered by a test. - At least one non-SQL Server provider works as a destination.
- Sync and async paths behave the same.
Metadata
Metadata
Assignees
Labels
capabilityA huge capability implementation beyond features and enhancements.maintainers-onlyReserved for maintainers. Please do not submit a PR unless discussed and approved first.priorityTop priority feature or things to dotodoThings to be done in the future
Activity
Added the Unit Tests for RepoDb.Schema.Core. Ref: #1366
Added the Unit and Integration Tests for RepoDb.Schema.SqlServer. Ref: …
Added the capability to move schemas by array of table names. Ref: #1366
Added the error handling for copying schemas for multiple tables. Ref: …
More updates for the RepoDb.Schema.Core and SqlServer. Ref: #1366
Renamed the file SqlServerNames.cs to Helper.cs. Ref: #1366
Moved the logic from SQL Server into base. Sort the properties on the…
Moved the logic from SQL Server into base. Ref: #1366
Renamed the filename to SqlServerNames. Ref: #1366
40 remaining items
Added the CreateSchema extension method. #1366
Added the 'SaveDefinitionAsXml', 'SaveDefinitionAsYaml' and 'SaveDefi…
Added the 'ReadDefinitionFromJson', 'ReadDefinitionFromXml' and 'Read…
Added the 'LoadDefinitionFromJson', 'LoadDefinitionFromXml' and 'Load…
Code structuring and formatting. #1366 #1367
Code structuring and refactoring to make it Clean Code and is adherin…
Added the ValidateDefinitionFromXml, ValidateDefinitionFromJson and V…
Renamed the SchemaDefinitionResult to SchemaDefinitionInfo. #1366
Renamed the SchemaDefinitionResult to SchemaDefinitionInfo. #1366
Added the ValidateDefinition. Also added the 'throwOnError' argument …
Added the SchemaMappingMode enumeration so the user can set which mod…
Minor ordering of the argument for SchemaMappingMode enumeration. #1366
Improved the validation messages of the ValidateDefinition extension …
Moved the FormatKind() method to the RepoDb.<DbProvider>.Schema packa…
Made the SchemaDialectMapper registration via TDbConnection generic t…