Earlier quoted context omitted.
+1 for code-first migrations. It's a bit of a learning curve, but the effort is worth it imo.
I prefer dB first. Create an SQL project that defines the schema. It automatically compares the schema with your current dB schema and auto generates a migration script for you. Then update entities from the dB. Few steps involved but avoids writing and maintaing migrations yourself.
Ask HN: How do you organize and manage database migrations?
51–60 of 61 posts
Re: Ask HN: How do you organize and manage database migrations?
#52Right now it only supports SQLite, MySQL, and PostgreSQL.
Re: Ask HN: How do you organize and manage database migrations?
#53We built it ourselves. * We have migrations written in SQL files named like 45_add_column_x_to_view_y.sql * We track the migrations in git and we do merge requests and reviews * We include a comment in each migration containing the original DDL of the object (1. it makes it easier for the reviewer to see what's changing, 2. it gives us a way to rollback, though it never happened so far) * We track the applied migrati…
Re: Ask HN: How do you organize and manage database migrations?
#54I like how Wordpress does simple database-first migration: just call `dbDelta()` function with your new table schema in sql format (CREATE TABLE ...) and it'll figure out a way to update your table to fit your new schema. I wonder if there is any similar db migration tool in python that work like this, seem perfect for small projects that don't want to use an ORM.
https://codex.wordpress.org/Creating_Tables_with_Plugins#Cre...
Re: Ask HN: How do you organize and manage database migrations?
#55Honestly? I don't know of any tool that handles database migrations as well or as easily as Ruby on Rails and ActiveRecord. I have literally spun up a barebones Rails app just to manage my schema and migrations for a completely separate Python/PostgreSQL project before. https://guides.rubyonrails.org/v5.2/active_record_migrations...
I second this - migrations in Rails are fantastic. Even if you aren't using Rails you can get the styles and source code here https://edgeguides.rubyonrails.org/active_record_migrations.... Large open source projects such as GitLab source code are also treasures to discover migration rules. If you have a millions of users with high traffic tables you may want to check some migration guides for downtime, data migratio…
One of the points was that migrations should always be reversible. I’ve struggled with this in the past when it comes to migrations that change or drop data. How do you write a downgrade script that recalls the previous data?
I’ve given up with downgrades now. I make sure to take a dB backup or snapshot before running an upgrade. I’ve never had to test this solution in a tense situation though...
Re: Ask HN: How do you organize and manage database migrations?
#56Same general concept as most others use, apply sql files by date, track what’s applied in the db itself, deploy the sql files with your code.
Re: Ask HN: How do you organize and manage database migrations?
#57I have used sqitch http://sqitch.org/ in the past and found it good for postgres. It supports revert and verify too.
Re: Ask HN: How do you organize and manage database migrations?
#58Earlier quoted context omitted.
I second this - migrations in Rails are fantastic. Even if you aren't using Rails you can get the styles and source code here https://edgeguides.rubyonrails.org/active_record_migrations.... Large open source projects such as GitLab source code are also treasures to discover migration rules. If you have a millions of users with high traffic tables you may want to check some migration guides for downtime, data migratio…
That gitlab link was a good read, thanks! One of the points was that migrations should always be reversible. I’ve struggled with this in the past when it comes to migrations that change or drop data. How do you write a downgrade script that recalls the previous data? I’ve given up with downgrades now. I make sure to take a dB backup or snapshot before running an upgrade. I’ve never had to test this solution in a tens…
Re: Ask HN: How do you organize and manage database migrations?
#59We're heavy Postgres users and take full advantage of some of its features which are not very well supported by Django. Quite a number of our migrations involve `migrations.RunSQL` operations. That's fine. We've also developed a package [1] that extends Django with a lot of Postgres specific features. For example, recently we added support for partitioned tables. We can now set those up as normal Django models.
In order to reduce the number of f'ups, we also built a Github bot that inspects PR's for potentially disastrous migrations. For example, the Github bot will yell at you if you try to rename a field or blindly drop a table. The Github bot also makes sure your migration doesn't conflict with another migration that was just merged.