Database migration
Is there any way to use the dbml cli tool to generate sql migration scripts?
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";
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.
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.
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)
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