ariga / atlas

Manage your database schema as code
https://atlasgo.io
Apache License 2.0
5.96k stars 265 forks source link

MultiTenancy - Large Migrations #3205

Open jmortlock opened 2 weeks ago

jmortlock commented 2 weeks ago

When running large migrations with multi-tenancy setup (MySql) we are hitting this error

Error: driver: bad connection

This happens after a long migration and before the start of the next one, I assume the "bad connection' has infact timed out.

a8m commented 2 weeks ago

Hey, @jmortlock,

Which command do you use in the multi-tenant setup? Also, what do you consider a "long" migration? How long has it been running?

jmortlock commented 2 weeks ago

Hi

I can see an example here after 40mins

Error: driver: bad connection
[mysql] 2024/10/28 20:35:11 connection.go:49: bad connection
  -- 12 sql statements
  -- 5 migrations
  -- 39m23.268774378s
  -------------------------

I am running this command to apply the migrations

atlas migrate apply --exec-order non-linear --env aws

Using the setup outlined in your docs, i.e. this sorta thing

env "aws" {
  src = "db/schema.hcl"
  for_each = toset(data.sql.tenants.values)
  url      = urlsetpath(data.runtimevar.url, each.value)
  migration { dir = "file://db/migrations" }
}

I suspect whats happening is because we are using RDS proxy service which has a much tighter default Idle timeout of 30mins (mysql default is 8hours).

Current workaround is to restart the migrations when the error is encountered.