How-to guides

How to generate SQL DDL from a data model

Produce a DDL script from a model, for a chosen database, or produce the migration between the model and what is already deployed.

When this is useful

  • A design has been agreed and needs to become a real schema.
  • A model has changed and you need the migration rather than a fresh create script.
  • You want the SQL to be a product of the reviewed model rather than something written alongside it.

Before you start

  • A model that passes validation. Generating DDL from a model you have not validated exports the problems along with everything else.

Step 1: Validate first

Run Validate Model, and the naming check if your organisation has standards. Both are cheaper to satisfy before the SQL exists.

Data ▸ Validate ModelData ▸ Naming Standards

Step 2: Export DDL for your database

DDL export offers PostgreSQL, MySQL, BigQuery and SQL Server. Pick the one you are deploying to — the dialects differ in ways that matter.

Diagram ▸ DDL (SQL)Diagram ▸ PostgreSQLDiagram ▸ MySQLDiagram ▸ BigQueryDiagram ▸ SQL Server

Step 3: Or generate the migration instead

Where the schema already exists, Delta DDL produces the difference between the model and the deployed state — the migration — rather than a script that assumes an empty database.

Data ▸ Delta DDL / Migration

Step 4: Review before running it

Read the generated script before executing it anywhere that matters. Generated DDL is a proposal from the model; the model can be wrong, and a destructive statement is destructive regardless of what produced it.

What happens next

The script goes into your normal migration process. Nothing here executes against your database.

Example

A billing model validated, exported as PostgreSQL DDL, reviewed in a pull request, and applied through the team's existing migration tooling — the schema and its documentation produced from the same source.

Tips

  • Regenerate rather than hand-editing the script. A hand edit is a change the model does not know about, and the next Delta DDL will not account for it.
  • Export to the dialect you deploy to, not the one you model in.

Limitations

  • Yantra Studio generates the script; running it is your process.
  • DDL export and Delta DDL are separately enabled.

Related how-to guides

Related questions

The capability behind this

Put your architecture under version control

Model it, govern it, and keep it true to the system it describes.