Blog/Guides

What the generated migration actually does

How a diagram's changes become migration SQL for six engines, and the places where each engine needs something other than ALTER TABLE.

Guides8 min read

A diagram is a picture of the schema you want. A migration is the set of statements that gets a real database from the schema it has to that one. Bright Web Data Modeller writes the second from the first, and this post walks through what it writes and where it has to be careful.

Where migrations come from

Any time two versions of a schema meet, the app works out the difference: merging a branch, restoring an old version, applying an AI suggestion, pulling a change from a linked database. It matches tables, columns and relationships by name, lists each difference as its own item for review, and writes migration SQL for the items you keep.

So the SQL you see in the migration tab is always for exactly the change you are about to make, nothing more. Untick an item in the list on the left and its statements disappear from the script on the right.

Seeing it in the app

The most common route is a branch merge:

  1. Open the Branches panel in the editor's sidebar and click Merge. It needs at least two branches and edit access.
  2. The Merge and review window opens. Pick the branch with your changes under Merge from and the branch they go into under Merge into.
  3. Every change is listed on the left, all ticked to start with. On the right, the Result on tab previews the target branch with the changes applied.
  4. Switch to the Migration SQL tab. Up is the forward script; Down reverses it.
  5. The dialect select beside them starts on the diagram's own engine (and its host, if you've set one in Diagram settings). Pick another to see the same change written for a different database.

Copy puts the script on your clipboard and Download saves it as migration.up.sql or migration.down.sql.

A worked example

Say main has two PostgreSQL tables: customers (id serial, email text, unique) and orders (id serial, customer_id integer, total numeric(10,2)). Nobody has declared the foreign key between them yet.

On a branch, you add an enum order_status with the values pending, paid and shipped, give orders a status column of that type, not null with a default of 'pending', draw the relationship from orders.customer_id to customers.id with ON DELETE CASCADE, and add a check named orders_total_positive and an index on customer_id.

The review lists five items: the enum, the column, the check, the index and the relationship. With all five ticked, the Up script for PostgreSQL is:

CREATE TYPE "order_status" AS ENUM ('pending', 'paid', 'shipped');

ALTER TABLE "orders" ADD COLUMN "status" "order_status" DEFAULT 'pending' NOT NULL;

ALTER TABLE "orders" ADD CONSTRAINT "fk_orders_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "orders" ADD CONSTRAINT "orders_total_positive" CHECK (total >= 0);

CREATE INDEX "orders_customer_id_idx" ON "orders" ("customer_id");

The Down script undoes it in reverse dependency order, so the type is dropped last, after the column that uses it:

DROP INDEX "orders_customer_id_idx";

ALTER TABLE "orders" DROP CONSTRAINT "orders_total_positive";

ALTER TABLE "orders" DROP CONSTRAINT "fk_orders_customer_id";

ALTER TABLE "orders" DROP COLUMN "status";

DROP TYPE "order_status";

The order matters

A migration that's correct statement by statement can still fail if the statements run in the wrong order. The generator sorts every statement into a fixed sequence of phases rather than writing them in the order you made the changes. A few of the rules that sequence encodes:

  • New schemas are created before anything goes in them, and enum types before the tables and columns that use them.
  • Tables are created before the foreign keys that point at them, and foreign keys are dropped before the tables they point to.
  • Constraints and indexes are all dropped before any are added. A changed check or index is dropped and added back under the same name, so it never collides with itself.
  • Policies are dropped before the columns they read.
  • Triggers are dropped before the functions they call, and created after the tables and functions exist.
  • A function whose body changed is replaced with CREATE OR REPLACE. If its return type changed, Postgres won't allow that, so it's dropped and created again.
  • Views are dropped first and created last, because they read the tables in between.

The Down script is built from the inverse of each change and goes through the same phases, so it applies cleanly too. Where it can't know enough to reverse something, such as a dropped table whose definition it doesn't carry, it writes a comment saying so instead of a statement.

Each engine has its own rules

The same change can need quite different SQL depending on the database. The diagram stores which engine it targets, and the generator follows that engine's rules rather than writing generic SQL and hoping.

PostgreSQL gets the most complete output: enums as types, check constraints, partial indexes, row level security and policies, functions and triggers, and comments with COMMENT ON.

MySQL and MariaDB have no enum types, so the values go on the column itself. The same change from the example becomes:

ALTER TABLE `orders` ADD COLUMN `status` ENUM('pending', 'paid', 'shipped') DEFAULT 'pending' NOT NULL;

There's no separate comment statement either, so a column's comment is written inline, which means restating the whole column definition. Changing a column's type uses MODIFY COLUMN, and a foreign key is dropped with DROP FOREIGN KEY.

SQL Server needs GO between batches, so the migration is split where it has to be. Defaults are named constraints, so the new column comes out as:

ALTER TABLE [orders] ADD [status] NVARCHAR(7) CONSTRAINT [DF_orders_status] DEFAULT 'pending' NOT NULL;
GO

Changing that default later drops DF_orders_status by name before adding the new one, and dropping the column drops its default first. Views use CREATE OR ALTER VIEW. Comments become MS_Description extended properties.

Oracle folds unquoted names to upper case. The generator leaves lowercase names bare so they read naturally, and uses Oracle's own types: NUMBER, VARCHAR2, CLOB. Oracle has no ON UPDATE action on a foreign key, so the example's relationship ends at ON DELETE CASCADE.

SQLite is the interesting one. It can add a column, but most other changes (changing a column's type, adding a foreign key or a check, dropping a constraint) aren't possible with ALTER TABLE. For those, the generator rebuilds the table: it creates a new table with the target definition, copies the rows across, drops the old table and renames the new one into place, then recreates its indexes. For the example, orders gains a foreign key and a check, so it's rebuilt:

-- Run this outside a transaction: inside one SQLite ignores PRAGMA foreign_keys, and dropping a rebuilt table would fire its children's ON DELETE actions.
PRAGMA foreign_keys = OFF;

-- sqlite has no enum types; order_status lives in its columns' types.

-- Rebuild "orders": SQLite can't change it in place.

CREATE TABLE "_new_orders" (
  "id" INTEGER NOT NULL,
  "customer_id" INTEGER NOT NULL,
  "total" NUMERIC(10,2) NOT NULL,
  "status" VARCHAR(7) DEFAULT 'pending' NOT NULL,
  PRIMARY KEY ("id"),
  CHECK ("status" IN ('pending', 'paid', 'shipped')),
  CONSTRAINT "orders_total_positive" CHECK (total >= 0),
  CONSTRAINT "fk_orders_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE CASCADE ON UPDATE NO ACTION
);

INSERT INTO "_new_orders" ("id", "customer_id", "total") SELECT "id", "customer_id", "total" FROM "orders";

DROP TABLE "orders";

ALTER TABLE "_new_orders" RENAME TO "orders";

CREATE INDEX "orders_customer_id_idx" ON "orders" ("customer_id");

PRAGMA foreign_key_check;

PRAGMA foreign_keys = ON;

Only the columns both tables share are copied, so the new status column takes its default on every existing row. The enum becomes a check on the allowed values. Any views are dropped before the rebuild and created again after it. Our tests run these rebuilds in a real SQLite engine to make sure the data survives.

On Cloudflare D1, foreign keys can only be deferred, not turned off, so dropping the old table would fire its children's ON DELETE actions. Where a rebuild would do that, the generator writes a comment naming the child tables and asks you to make that change by hand.

Getting it into your repository

You can copy the SQL, download it, or commit it to a linked repository from the migration tab, on any host that takes commits (GitHub, GitLab, Azure DevOps, Bitbucket, Gitea and Forgejo; a plain Git server is read only). With nothing linked, the bar above the SQL offers Connect a repository.

Once a repository is linked, pick it in the target select and check the filename. The suggested name follows the repository's migration format: a timestamped V…__data_modeller.sql for Flyway, a …/migration.sql folder for Prisma, ….up.sql or ….down.sql for golang-migrate. Where the tool needs another step, such as a Drizzle journal entry or a line in sqitch.plan, a note beside the field says so. The file name must be a plain .sql name, optionally inside one folder.

Commit to (followed by the host's name) asks you to confirm the path and branch, then adds that one file to your migrations folder with the message Apply migration: <name> (or Revert migration: for a Down script). It never force-pushes.

If your migrations live in a format other than plain SQL files, the app can still read them: Supabase, Prisma, Drizzle, Flyway, golang-migrate, dbmate, Sqitch, Atlas, Liquibase and more. That's how a branch can be built from the schema at any commit of your repository, which is a good way to check that the diagram and the code agree.

What it doesn't do

The generator writes schema changes. It doesn't write data migrations: if you split a name column into first_name and last_name, the SQL adds and drops columns, and moving the data across is still yours to write.

Renames are the same story. Rename customers.email to email_address and the review shows two items, a column added and a column removed, and the script follows them:

ALTER TABLE "customers" ADD COLUMN "email_address" TEXT NOT NULL UNIQUE;

ALTER TABLE "customers" DROP COLUMN "email";

Run that against a table with rows and the NOT NULL add fails, and if it didn't, the drop would throw the addresses away. Review those items carefully before you merge, and replace the pair with an ALTER TABLE ... RENAME COLUMN of your own where that's what you meant.

More guides

  • Give your schema tool a read-only login

    14 November 2026 · 6 min read

    A tool that reads table definitions shouldn't hold a login that can read every row. How the app writes a catalog-only login for each engine, and what it does with the connection.

  • Row level security you can read at a glance

    12 November 2026 · 6 min read

    Postgres policies live in migration files where nobody looks at them. Here is how to keep them next to the tables they protect.

  • Review schema changes the way you review code

    8 November 2026 · 5 min read

    A dropped column deserves the same scrutiny as a deleted function. Branches and merge requests for your data model, and what a reviewer sees.

Try it on your own schema

One diagram with two branches, no card needed. Upgrade when your team needs more.