Skip to content

Commit

Permalink
providers: add drizzle example
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Jan 2, 2025
1 parent 335e505 commit a95852a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 0 deletions.
39 changes: 39 additions & 0 deletions providers/drizzle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Drizzle Example

The sample project demonstrates how to use Atlas as a migration engine for Drizzle.

## Prerequisites

- [Atlas CLI](https://docs.atlas.mongodb.com/reference/atlas-cli/install/)
- [Docker](https://docs.docker.com/get-docker/)

## How to use

1. Install dependencies

```bash
pnpm add drizzle-orm pg dotenv
pnpm add -D drizzle-kit tsx @types/pg
```


2. Modify the Drizzle schema `schema.ts`.
3. Run Atlas to plan the migration.

```bash
atlas migrate diff --env local
```

A new migration file will be created in the `atlas/migrations` directory.

4. Start the PostgreSQL database for applying migrations

```bash
docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:16
```

5. Apply the migration to target database

```bash
atlas migrate apply --env local --url "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable"
```
27 changes: 27 additions & 0 deletions providers/drizzle/atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
data "external_schema" "drizzle" {
program = [
"npx",
"drizzle-kit",
"export",
]
}

data "composite_schema" "drizzle-objects" {
schema "public" {
url = data.external_schema.drizzle.url
}
schema "public" {
url = "file://atlas/drizzle_objects.sql"
}
}

env "local" {
dev = "docker://postgres/16/dev?search_path=public"
schema {
src = data.composite_schema.drizzle-objects.url
}
migration {
dir = "file://atlas/migrations"
}
exclude = ["drizzle"]
}
2 changes: 2 additions & 0 deletions providers/drizzle/atlas/drizzle_objects.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Create "echo" function
CREATE FUNCTION "echo" (text) RETURNS text LANGUAGE sql AS $$ SELECT $1; $$;
2 changes: 2 additions & 0 deletions providers/drizzle/atlas/migrations/20250102081546.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Create "users" table
CREATE TABLE "users" ("id" integer NOT NULL GENERATED ALWAYS AS IDENTITY, "name" character varying(255) NOT NULL, "age" integer NOT NULL, "email" character varying(255) NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email_unique" UNIQUE ("email"));
2 changes: 2 additions & 0 deletions providers/drizzle/atlas/migrations/20250102090452.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Create "posts" table
CREATE TABLE "posts" ("id" serial NOT NULL, "content" text NULL, "author_id" integer NULL, PRIMARY KEY ("id"));
2 changes: 2 additions & 0 deletions providers/drizzle/atlas/migrations/20250102095509.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Create "echo" function
CREATE FUNCTION "echo" (text) RETURNS text LANGUAGE sql AS $$ SELECT $1; $$;
4 changes: 4 additions & 0 deletions providers/drizzle/atlas/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h1:kUstuMXCknAlNw/11Fz5Pwk0b3wIPssYfI5DRzFu1cs=
20250102081546.sql h1:ifHgYsFBXit5e6H3eGK0hi+anS5yKsHzG3zHloB11hE=
20250102090452.sql h1:yEZfqrozMKLL0tyCMS91A5wa0APFyvmSIBNAm4GvXK8=
20250102095509.sql h1:qzbmjk+KK4mkLGaU7nlEBp5AaAnQfD5VY2/159sxji4=
11 changes: 11 additions & 0 deletions providers/drizzle/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
out: './drizzle',
schema: './schema.ts',
dialect: 'postgresql',
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
14 changes: 14 additions & 0 deletions providers/drizzle/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { integer, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
});

export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id'),
});

0 comments on commit a95852a

Please sign in to comment.