superfly / litefs

FUSE-based file system for replicating SQLite databases across a cluster of machines
Apache License 2.0
3.78k stars 89 forks source link

Issue running automatic database schema migrations. #204

Closed VioletBuse closed 1 year ago

VioletBuse commented 1 year ago

Running FROM flyio/litefs:0.2.0 AS litefs

Hi, so I'm trying to run automatic schema migrations with litefs in a fly.io project. The project has three regions/instances. I have a bash script that checks if the instance is the primary, and if it is, it runs the migration command:

#!/usr/bin/env bash

PRIMARY=$LITEFS_DIR/.primary
if [[ -f "$PRIMARY" ]]; then
    PRIMARYFILE=$(cat "$PRIMARY")

    if [[ $PRIMARYFILE == $HOSTNAME* ]]; then
    echo "This is the primary. Migrating."
    (npx prisma migrate deploy)
    else
    echo "Not Primary. Not Migrating."
    fi

else
    echo "No .primary file found. Migrating."
    (npx prisma migrate deploy)
fi

The problem is that when I deploy an update, the first two instances to get the update all report that they are not the primary (since that last one is), but then when the last instance gets the update it is no longer the primary and thus the migration never gets executed. What can I do to ensure the migration is ran?

benbjohnson commented 1 year ago

@JulianBuse Good question. Currently, you'd need to run the migration out-of-band from the deployment to ensure it's on the primary. We do have write forwarding (https://github.com/superfly/litefs/issues/56) planned for v0.3.1 to make this easier so you can run a write transaction from any node—even if they're not the primary. You'll want to run most writes near the primary for performance reasons but it'd be fine to run infrequent migrations from any node.