drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.61k stars 649 forks source link

[FEATURE]: skip schema check in migrator #3584

Open a-ursino opened 1 day ago

a-ursino commented 1 day ago

Feature hasn't been suggested before.

Describe the enhancement you want to request

Feature: Provide a way to bypass the check CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)} when the default "public" schema is used in Postgres with

  await migrate(db, {
    migrationsSchema: 'public',
    migrationsFolder,
  });

Reason: It is possible that in high regulated environments the user executing the migration doesn't have the permissions for any operation on the schema, even checking if it exists (this is the reason why we use "public" in the first place). The above statement will trigger an error "permission denied" and abort the entire migration.

It should be quite easy just changing https://github.com/drizzle-team/drizzle-orm/blob/30e5347c5b363e4ae5e30be78ac0bf64feaf98d4/drizzle-orm/src/pg-core/dialect.ts#L85 with

if(migrationsSchema !=='public') {
    await session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);
}

The public schema should exists by default in Postgres https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PUBLIC

I can create a PR for it if you're happy with the approach.