-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}); |