Migra: Diff for PostgreSQL schemas
github.com
github.com
I would also like to share a very similar tool that is much less known:
https://github.com/bikeshedder/tusker
It's the same thing, except instead of requiring two DB URL's to do the diffing, you give it a "schema.sql" file and a folder path that has your migrations in it, and the path to the current DB.
Then it introspects the DB, looks at "schema.sql" + the migration files, and figures out what you've changed in "schema.sql" that isn't in your migrations yet and generates the diff as a new migration. Really amazing workflow.
If you have trouble visualizing it, the workflow would be something like:
1. DB is empty, migrations empty, schema.sql has "CREATE TABLE todo (id int, description text);"
2. You run the diff, it generates a migration that contains the CREATE TABLE statement
3. You modify "schema.sql", maybe adding a new column like "boolean is_completed" to "todo"
4. You re-run the diff, it sees the DB has the table, the migration for the table is present, but a new column is added.
So it generates an "ALTER TABLE .. ADD COLUMN .." migration.
5. Rinse, repeat.Tusker actually uses Migra to power its functionality: https://github.com/bikeshedder/tusker#how-does-it-work
Tusker's flow is somewhat similar to sqldef https://github.com/k0kubun/sqldef , although the internal mechanics are quite different. Migra/Tusker executes the SQL in a temporary location, introspects it, and diffs the introspected in-memory representation -- in other words, using the database directly as the canonical parser. In contrast, sqldef parses the SQL itself, builds an in-memory representation based on that, and then does the diff that way.
I'm the author of Skeema https://www.skeema.io which provides a similar declarative workflow for MySQL and MariaDB schema changes. Skeema uses an execute-and-introspect approach similar to Migra/Tusker, although each object is split out into its own .sql file for easier management in version control, with a multi-level directory hierarchy if you have multiple database instances and multiple schemas.
Skeema was conceptually inspired by Facebook's internal database schema change flow, as FB has used declarative schema management submission/review/execution company-wide for over a decade now. Skeema actually predates both Migra and sqldef slightly, although it did not influence them, all were developed separately.
In turn, Prisma Migrate and Vitess/PlanetScale declarative migrations were directly inspired by Skeema's approach, paradigms, and/or even direct use of source code in Vitess's case. (Although they're finally moving to a parser-based approach instead, which I recommended they do over a year ago, as it makes more sense for their use-case -- their whole product inherently requires a thorough SQL parser anyway... and ironically, sqldef is based on the Vitess SQL parser!)
What a twist! Might we ask what field you work in? Seems niche
Just to be clear, among the tools I mentioned, I'm the author of Skeema but not any of the others. Skeema is MySQL/MariaDB specific, but I often get asked about similar tools for Postgres, so I try to stay familiar with the landscape.
- Development slowed on migra for a quite a while, but I've recently returned to active development, and we also have some new maintainers coming on board - so some longstanding bugfixes and feature requests will finally get the attention they need.
- If you're interested in migra and better database workflows, you/your employer might also like migra's associated paid offering, https://databaseci.com/, which offers flexible subsetting for postgres databases </plug>
Migra – A schema diff tool for PostgreSQL - https://news.ycombinator.com/item?id=16673526 - March 2018 (29 comments)
(Reposts are fine after a year or so: https://news.ycombinator.com/newsfaq.html)
Either way i had zero issues with this setup. And I used a lot of postgres things including triggers, complex indecies, extensions, etc. All migrations were source-controlled and could be verified before commits, so it was pretty safe. When I needed to migrate a data, i'd just add code into the same migration file as the schema change. Brilliant project! thanks @djrobstep
And something that can begin collecting a history of DDL changes in a SQL Server database to compare stored procedure versions: https://github.com/unruledboy/SQLMonitor (among many other administrative features).
Of course the primary difference is the price of Visual Studio / the usage limitations of the Community edition.
I'm sort of confused. Is this for people who don't know how to write database migrations? Surely not. What is this for? Sorry for being stupid.
The more common use case is this idea in development—-experiment with different schemas manually and then use a tool like Migra to figure out what migration to write, without keeping in your head what changes you’ve made.
It's a (IMHO) better alternative to something like Alembic https://alembic.readthedocs.org/
Tusker (mentioned in other comment) does it this way; my company has used the technique very successfully for several years via some lightweight tooling built around migra.
I used a tool like this to produce a diff of our prod database vs. a freshly created database with our migrations applied. Saved me a ton of time.
- Autogenerate migrations. A diff tool can (not always but very often) generate the migration script you need automatically without reference a history of migration scripts.
- Test migrations and other changes. "OK, I've run my migration script - but does prod actually match my intended production state now?"
- Quickly iterating on database designs in local development. "Just added an int column to a table in my local dev database but i meant for it to be a bigint with a slightly different name - all I need to do is update my models and run sync again."