Skip to content

Capability: Schema Mover #1366

Open
Open

Description

@mikependon

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.
  • TableSchema is the only contract between them, so providers never depend on each other.
  • CopySchemaTo reads 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

  • CopySchemaTo returns the correct CopySchemaOutcome for all four CopySchemaExistsBehavior values.
  • 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 MissingMappingException with a clear message.
  • DropOnExists is 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.

Activity

  1. self-assigned this
    on Oct 4, 2026
  2. added
    todo
    priority
    capability
    maintainers-only
    on Oct 4, 2026
  3. pinned this issue on Oct 4, 2026
  4. 40 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

capabilitymaintainers-onlyprioritytodo

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions