drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Allow to specify a particular schema name in the migrate() config for postgres

Open
#908 7 comments 21 reactions 1 assignee Claimed by @AndriiSherman View on GitHub
enhancement priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Allow users to specify a target schema name for running migrations in postgres. Currently the `migrate()` intrinsically always target `public` schema. This enhancement will enable us to do migrations in multi-tenant use cases where each schema must have the same migrations applied to be in sync with the application requirements. Please take a look at the following code that is written as per the current functionality supported.

```
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import postgres from 'postgres'
import * as dotenv from 'dotenv'

dotenv.config({ path: './.env.local' })

const sql = postgres(process.env.DATABASE_URL!, { max: 1 })
const db = drizzle(sql)

const main = async () => {
await migrate(db, { migrationsFolder: 'drizzle' })
await sql.end()
process.exit(0)
}

main()
```
I propose to extend the migrate config to have additional property of `schema` or `schemas` to specify a single target schema or an array of schema names. In the multiple schema names scenario the `migrate()` function can iterate on the array and apply the migrations one by one on each schema.
The following code snippets show the proposed enhancements.
1. Default behaviour, targets `public` schema
```
await migrate(db, { migrationsFolder: 'drizzle' })
```
2. Target one specific schema
```
await migrate(db, { migrationsFolder: 'drizzle', schema: 'cust1' })
```
3. Target multiple schemas
```
await migrate(db, { migrationsFolder: 'drizzle', schema: ['cust1', 'cust2', 'cust3'] })
```
4. Target multiple schemas including 'public'
```
await migrate(db, { migrationsFolder: 'drizzle', schema: ['public', 'cust1', 'cust2', 'cust3'] })
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.