From 8d0796910341fbbc5cc347fb2b23afa347f91942 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Mar 2023 14:21:05 -0600 Subject: [PATCH] chore: Never run v2 migrations on v1 database --- coderd/database/migrations/migrate.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/coderd/database/migrations/migrate.go b/coderd/database/migrations/migrate.go index 15d3d4869cd9f..4103513751e72 100644 --- a/coderd/database/migrations/migrate.go +++ b/coderd/database/migrations/migrate.go @@ -25,6 +25,13 @@ func setup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { return nil, nil, xerrors.Errorf("create iofs: %w", err) } + // migration_cursor is a v1 migration table. If this exists, we're on v1. + // Do no run v2 migrations on a v1 database! + row := db.QueryRowContext(ctx, "SELECT * FROM migration_cursor;") + if row.Err() == nil { + return nil, nil, xerrors.Errorf("currently connected to a Coder v1 database, aborting database setup") + } + // there is a postgres.WithInstance() method that takes the DB instance, // but, when you close the resulting Migrate, it closes the DB, which // we don't want. Instead, create just a connection that will get closed