Data

TableSchema and database structure upgrades: compare, plan, execute

This post explains how a .NET ERP framework applies TableSchema definitions to the physical database without migration scripts. Each upgrade compares the live structure with the definition, then separates compare, plan and execute so the mode (ALTER or rebuild) can be checked before running. It is useful for developers who maintain schemas that already hold production data.

DEV Community: #dotnet Jeff Tsai
Published
Reading time
15 min read
agent run #16
agent run #16
Go to original dev.to

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Key points

  • Each upgrade compares the live database with the TableSchema definition instead of running versioned scripts, so reruns are safe.
  • The plan step reads the database without writing and reports ALTER, rebuild or no change before anything runs.
  • A rebuild copies data into a temporary table and renames it; on large tables it can take tens of minutes to hours.
  • Lengthening a string or moving between numeric types stays an ALTER; crossing type families forces a rebuild.
  • Columns missing from the definition are kept, so column drops are never automatic.

The author describes how a TableSchema definition in a .NET ERP framework controls the physical database structure. Instead of versioned migration scripts, each upgrade compares the live database with the definition, computes the difference and then executes it. The difference is treated as a plan first, which states whether the change is an ALTER or a full table rebuild. A rebuild can mean a maintenance window on large tables.

The post also covers why native column types are kept out of the definition, why staged ALTER steps run in separate transactions, and why rerunning after a failure is preferred over a rollback that cannot be trusted. Column removal is intentionally not automated. The original article has the full walkthrough and the sample definitions.

Real-world use

  • Checking whether widening a column on a table with tens of millions of rows will force a rebuild before scheduling downtime.
  • Maintaining one schema definition that deploys to SQL Server, PostgreSQL and SQLite in a multi-database ERP product.
  • Realigning a production database that someone edited by hand, using a rerunnable upgrade instead of hand-written scripts.

Why it matters

You can read whether a schema change needs downtime from the plan output before deploying, rather than discovering it mid-run. Closing the gap between definition and database with a rerunnable comparison, instead of an ordered script sequence, makes production deployments more predictable.

This summary was generated by the agent from the original post on DEV Community: #dotnet. The full text and all rights belong to its author and publisher. Go to original

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message