dbml icon indicating copy to clipboard operation
dbml copied to clipboard

Database migration

Open TheKnarf opened this issue 6 years ago • 5 comments

Is there any way to use the dbml cli tool to generate sql migration scripts?

TheKnarf avatar Jan 17 '20 18:01 TheKnarf

I'm thinking perhaps that dbml could have a migrations as a part of the dbml dsl. Something like:

Table users {
  id int [pk]
  name varchar
}

Migration 17012020-1 {
  addColumn(table=users) { id int [pk] }
  renameColumn(table=users, from=fullname, to=name)
}

And then from the cli tool one could run something like:

$ dbml migrate "17012020-1" user.dbml
ALTER TABLE users
ADD id int;
ALTER TABLE users
RENAME COLUMN "fullname" TO "name";

TheKnarf avatar Jan 17 '20 19:01 TheKnarf

If the cli tool also had some way of taking an old dbml file and a new one, compare it and output a new dbml file with a migration added. Something like:

Old:

Table users {
  fullname varchar
}

New:

Table users {
  fullname varchar
}
$ dbml generate-migration "17012020-1" users_old.dml users.dml
Table users {
  id int [pk]
  name varchar
}

Migration 17012020-1 {
  addColumn(table=users) { id int [pk] }
  dropColumn(table=users, column=fullname)
  addColumn(table=users) { name varchar }
}

Then a user can go in and edit that migration, since that the tool couldn't know that we wanted a rename and not dropping fullname and making a new column name. After the edit one can then run the dbml migrate command to get the sql script.

TheKnarf avatar Jan 17 '20 19:01 TheKnarf

And with annotations like mentioned in #53 one could annotate some columns if we want the migration generator to know that its a rename and not a new column.

TheKnarf avatar Jan 17 '20 19:01 TheKnarf

References to some other tools in the Node ecosystem that deal with database migrations:

  • https://github.com/rickbergfalk/postgrator
  • https://www.npmjs.com/package/sql-migrations
  • https://db-migrate.readthedocs.io/en/latest/Getting%20Started/usage/

(Mostly noting them for my own sake)

TheKnarf avatar Jan 17 '20 20:01 TheKnarf

References to some other tools in the Node ecosystem that deal with database migrations:

* https://github.com/rickbergfalk/postgrator

* https://www.npmjs.com/package/sql-migrations

* https://db-migrate.readthedocs.io/en/latest/Getting%20Started/usage/

(Mostly noting them for my own sake)

To add to this: https://github.com/prisma/specs/tree/master/schema, which pairs with https://github.com/prisma/migrate

mendelk avatar Feb 14 '20 17:02 mendelk