Is there a built-in way in Drizzle ORM to generate the CREATE TABLE IF NOT EXISTS SQL directly from the pgTable schema definition?
Here’s an example of how I’m currently setting up an in-memory pglite database for testing:
import {lower} from '@/drizzle/db.utils';
import {pgTable, serial, text, timestamp, uniqueIndex} from 'drizzle-orm/pg-core';
export const emailSubmissionTable = pgTable(
'email_submission',
{
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
createdAt: timestamp('created_at').notNull().defaultNow(),
},
(table) => [uniqueIndex('unique_email_idx').on(lower(table.email))]
);
And here’s how I’m currently setting up the in-memory database:
import {emailSubmissionTable} from '@/drizzle/db';
import {PGlite} from '@electric-sql/pglite';
import {drizzle} from 'drizzle-orm/pglite';
import {beforeAll, describe} from 'vitest';
describe('Email Submission', () => {
let db;
beforeAll(async () => {
const inMemoryDbClient = new PGlite();
await inMemoryDbClient.exec(`
CREATE TABLE IF NOT EXISTS "email_submission" (
"id" serial PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "email_submission_email_unique" UNIQUE("email")
);
`);
db = drizzle(inMemoryDbClient, {schema: {emailSubmissionTable}});
});
// ...
});
Problem:
I want to automatically generate the raw CREATE TABLE IF NOT EXISTS SQL string from the pgTable schema definition, which would look like this:
CREATE TABLE IF NOT EXISTS "email_submission" (
"id" serial PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "unique_email_idx" UNIQUE("email")
);
The reason I need this is so I can pass the generated SQL string to the in-memory database for testing my queries. I am using diffTestSchemas from the Drizzle ORM repository to generate this, but it's not convenient.
What It Could Look Like:
Ideally, Drizzle ORM would provide a helper function that could generate the CREATE TABLE IF NOT EXISTS SQL directly from the pgTable schema definition, something like this:
import {generateCreateTableSQL} from 'drizzle-orm/utils'; // Hypothetical helper function
const inMemoryDbClient = new PGlite();
// Generate the SQL string from the pgTable schema
const sql = generateCreateTableSQL(emailSubmissionTable);
// Execute the generated SQL in the in-memory database
await inMemoryDbClient.exec(sql);
// Log the SQL string to see it
console.log(sql);
/* Output:
CREATE TABLE IF NOT EXISTS "email_submission" (
"id" serial PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "unique_email_idx" UNIQUE("email")
);
*/
This would make it much easier to generate the SQL string directly from the schema definition to test my queries, selects, insets, etc.