Fork me on GitHub
#sql
<
2020-08-25
>
mister_m00:08:30

This might be the wrong forum - but how are folks managing database migrations these days? I want to create some tables, some constraints, have the sql that created these tables live under source control, and also track changes to these tables with additional scripts that can be applied on top of the previous. Is flyway popular/simple/ok/not wildly confusing?

mister_m01:08:26

I've also read a bit about sqitch - I'd be interfacing with postgres primarily

seancorfield01:08:40

We built our own migration tool in Clojure (because, reasons) but most folks use Ragtime or Migratus @radicalmatt

sandqvist05:08:29

I use flyway at work for a multi-tenant SaaS sw. Flyway takes a list of schemas to run the (same) migrations on, which simplifies adding tenants. We use a schema per tenant. It also has Java migrations that you can of course call Clojure from.

sandqvist05:08:41

The automatic schema addition is convenient. I have a test fixture that first deletes the "test" tenant's schema and then runs migrations on it, creating a new one. It's an easy way to keep the test database clean.

kulminaator17:08:01

recently moved stuff to flyway as well. it seems to work for the basic needs.

seancorfield01:08:25

I've seen at least one person using Flyway from Clojure but it doesn't seem very common.

seancorfield01:08:45

https://github.com/yogthos/migratus and https://github.com/weavejester/ragtime -- if I were to pick one of those, it would be Migratus, since the sequential migration numbers in Ragtime can be problematic (Migratus' README explains why).

thanks2 3
iarenaza14:08:07

Humm, as far as I know, sequential migration numbers are only needed in Ragtime if you want to split a SQL migration in several files so each file has only one SQL sentence (according to https://github.com/weavejester/ragtime/wiki/SQL-Migrations#sql). But if you use the --;; marker to separate SQL sentences, you can have them in a single file. In this case, the name of the file doesn't need to include sequential migration numbers. Files just need to end in .up.sql and .down.sql, but the "prefix" can be whatever you like, as long as they sort lexicographically in the order you need. That is, you could use timestamps as migration ids, like you do in Migratus. Am I wrong?

seancorfield17:08:08

I'd have to dig into how Ragtime records which migrations have been applied and which haven't, but it is relying on conventions, whereas Migratus records all the actual migrations applied I believe (so you don't have problems if you create migrations on branches that don't get merged until later). I haven't used either, so I'm basing my comments on other people's discussions about running into that problem (with Ragtime specifically and sequential migrations in general). Migratus has a utility to create the migration files for you so you don't have to manually figure out the right name.

iarenaza22:08:42

Oh, now I see what you meant! You were talking about the order of the recorded migrations that are already applied. And not about how to order the files with the new migrations that you want to apply. In that case, Ragtime imposes what you called "sequential migration numbers" by default, using the so-called "raise-error migration strategy" (See https://github.com/weavejester/ragtime/wiki/Concepts#strategies). But you can choose between several strategies. I think ragtime.strategy/apply-new is almost the same as what you say Migratus does.

seancorfield01:08:45

(our bespoke system at work uses sequential numbers, so I know that pain well, and it had its origins in pre-Clojure days for our company, so I just converted it to Clojure rather than switch completely to an existing one).