diff --git a/providers/drizzle/README.md b/providers/drizzle/README.md new file mode 100644 index 0000000..4183dc8 --- /dev/null +++ b/providers/drizzle/README.md @@ -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" +``` \ No newline at end of file diff --git a/providers/drizzle/atlas.hcl b/providers/drizzle/atlas.hcl new file mode 100644 index 0000000..f8d5c03 --- /dev/null +++ b/providers/drizzle/atlas.hcl @@ -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"] +} \ No newline at end of file diff --git a/providers/drizzle/atlas/drizzle_objects.sql b/providers/drizzle/atlas/drizzle_objects.sql new file mode 100644 index 0000000..dd05ac1 --- /dev/null +++ b/providers/drizzle/atlas/drizzle_objects.sql @@ -0,0 +1,2 @@ +-- Create "echo" function +CREATE FUNCTION "echo" (text) RETURNS text LANGUAGE sql AS $$ SELECT $1; $$; diff --git a/providers/drizzle/atlas/migrations/20250102081546.sql b/providers/drizzle/atlas/migrations/20250102081546.sql new file mode 100644 index 0000000..d274ff1 --- /dev/null +++ b/providers/drizzle/atlas/migrations/20250102081546.sql @@ -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")); diff --git a/providers/drizzle/atlas/migrations/20250102090452.sql b/providers/drizzle/atlas/migrations/20250102090452.sql new file mode 100644 index 0000000..1805784 --- /dev/null +++ b/providers/drizzle/atlas/migrations/20250102090452.sql @@ -0,0 +1,2 @@ +-- Create "posts" table +CREATE TABLE "posts" ("id" serial NOT NULL, "content" text NULL, "author_id" integer NULL, PRIMARY KEY ("id")); diff --git a/providers/drizzle/atlas/migrations/20250102095509.sql b/providers/drizzle/atlas/migrations/20250102095509.sql new file mode 100644 index 0000000..dd05ac1 --- /dev/null +++ b/providers/drizzle/atlas/migrations/20250102095509.sql @@ -0,0 +1,2 @@ +-- Create "echo" function +CREATE FUNCTION "echo" (text) RETURNS text LANGUAGE sql AS $$ SELECT $1; $$; diff --git a/providers/drizzle/atlas/migrations/atlas.sum b/providers/drizzle/atlas/migrations/atlas.sum new file mode 100644 index 0000000..5e60e8c --- /dev/null +++ b/providers/drizzle/atlas/migrations/atlas.sum @@ -0,0 +1,4 @@ +h1:kUstuMXCknAlNw/11Fz5Pwk0b3wIPssYfI5DRzFu1cs= +20250102081546.sql h1:ifHgYsFBXit5e6H3eGK0hi+anS5yKsHzG3zHloB11hE= +20250102090452.sql h1:yEZfqrozMKLL0tyCMS91A5wa0APFyvmSIBNAm4GvXK8= +20250102095509.sql h1:qzbmjk+KK4mkLGaU7nlEBp5AaAnQfD5VY2/159sxji4= diff --git a/providers/drizzle/drizzle.config.ts b/providers/drizzle/drizzle.config.ts new file mode 100644 index 0000000..d926552 --- /dev/null +++ b/providers/drizzle/drizzle.config.ts @@ -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!, + }, +}); \ No newline at end of file diff --git a/providers/drizzle/schema.ts b/providers/drizzle/schema.ts new file mode 100644 index 0000000..eff7903 --- /dev/null +++ b/providers/drizzle/schema.ts @@ -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'), +}); \ No newline at end of file