#963·migrate

Extension created in 1st migration file not available in 2nd migration file.

Author: nehalagrawal-hcCreated Jul 28, 2023Updated Sep 23, 2026

Describe the Bug

This is how my migration files look

  1. 000001_init_schema.up.sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
CREATE TABLE public.tenant (
    id uuid NOT NULL,
    created timestamp with time zone NOT NULL,
    last_updated timestamp with time zone NOT NULL,
    .....
    .....
    .....
);
ALTER TABLE ONLY public.tenant
    ADD CONSTRAINT tenant_name_unq UNIQUE (name);
  1. 000002_add_default_data.up.sql
INSERT INTO public.tenant ("id", "name", "type", "state", "created", "last_updated")
VALUES (uuid_generate_v4(), 'default', 'INTERNAL', 'NEW', now(), now())
ON CONFLICT ON CONSTRAINT tenant_name_unq
DO NOTHING;

When I run both migrations together migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up

It will throw this error (I verified uuid_ossp shows up in pg_extension table.)

2023/07/28 17:23:27 Read and execute 2/u add_default_data
2023/07/28 17:24:27 error: migration failed: function uuid_generate_v4() does not exist (column 9) in line 8:
(details: pq: function uuid_generate_v4() does not exist)

However, if I run these 2 migrations separately

migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up 1

followed by

migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up 2

It succeeds without any error.

Migrate Version - I use the migrate/migrate docker image from docker hub to run these migrations.

ARG MIGRATE_VERSION=v4.15.1
FROM migrate/migrate:${MIGRATE_VERSION}