Skip to content

Migrate schema definition and migrations to Drizzle #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 26, 2025
Merged
6 changes: 3 additions & 3 deletions apps/chat/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ services:
user: postgres
restart: always
healthcheck:
test: 'pg_isready -U user --dbname=postgres'
test: 'pg_isready -U user --dbname=onechat'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a diff between the database name in the .env you provided earlier and the actual setup, so I adjusted it to match the environment variables. @natew

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are creating a Database onechat inside init.sql, i dont think you intended to make the onechat as an default DB. Sticking to postgres as default one is correct (or at least should have different name than onechat if you want to create onechat inside init.sql)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense @cermakondrej

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah you are right, thank you! @cermakondrej

interval: 1s
timeout: 5s
retries: 10
ports:
- 5432:5432
environment:
POSTGRES_USER: user
POSTGRES_DB: postgres
POSTGRES_DB: onechat
POSTGRES_PASSWORD: password
command: |
postgres
Expand All @@ -24,7 +24,7 @@ services:
-c hot_standby_feedback=on
volumes:
- pgdata:/var/lib/postgresql/data
- ./src/postgres/:/docker-entrypoint-initdb.d
- ./src/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql

seeder:
image: oven/bun:latest
Expand Down
11 changes: 11 additions & 0 deletions apps/chat/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: 'postgresql',
schema: './src/zero/tables.ts',
out: './src/zero/migrations',
dbCredentials: {
url: process.env.ZERO_UPSTREAM_DB as string,
},
strict: true,
verbose: true,
})
17 changes: 12 additions & 5 deletions apps/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "module",
"scripts": {
"android": "expo run:android",
"backend": "yarn docker up",
"backend": "docker compose up -d pgdb search",
"backend:clean": "docker compose down --volumes; rm /tmp/onechat_replica.db || true",
"backend:run-clean": "yarn backend:clean; yarn backend",
"build:icon": "tauri icon ./src-tauri/icons/icon.png",
Expand All @@ -24,7 +24,10 @@
"seed": "tsx ./src/postgres/seed.ts",
"serve": "one serve",
"zero": "yarn zero:build-schema && zero-cache --schema-file ./src/zero/zero-schema.json",
"zero:build-schema": "zero-build-schema -p ./src/zero/schema-build.ts -o ./src/zero/zero-schema.json"
"zero:build-schema": "zero-build-schema -p ./src/zero/schema-build.ts -o ./src/zero/zero-schema.json",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate --config=./drizzle.config.ts && docker compose up seeder",
"db:drop": "drizzle-kit drop --schema=./src/zero/tables.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.709.0",
Expand Down Expand Up @@ -53,10 +56,13 @@
"@vxrn/emitter": "workspace:*",
"better-auth": "1.0.23-beta.3",
"burnt": "^0.12.2",
"dotenv": "^16.4.7",
"drizzle-orm": "^0.38.4",
"drizzle-zero": "0.2.2",
"expo": "~52.0.17",
"flexsearch": "^0.7.43",
"one": "workspace:*",
"pg": "^8.13.1",
"pg": "^8.11.0",
"react": "^19.0.0",
"react-hotkeys-hook": "^4.6.1",
"react-native": "^0.76.5",
Expand All @@ -68,14 +74,15 @@
"tamagui": "^1.121.11",
"ts-deepmerge": "^7.0.2",
"valtio": "^2.1.2",
"virtua": "^0.39.2"
"virtua": "^0.39.3"
},
"devDependencies": {
"@react-native-community/cli": "15.1.3",
"@tamagui/vite-plugin": "^1.121.11",
"@tauri-apps/cli": "^2",
"@types/aws-lambda": "8.10.146",
"@types/pg": "^8.11.10",
"@types/pg": "^8.10.2",
"drizzle-kit": "^0.30.2",
"sst": "^3.4.46",
"tsx": "^4.19.0",
"unicode-emoji": "^2.6.0"
Expand Down
3 changes: 2 additions & 1 deletion apps/chat/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Start [Docker](https://orbstack.dev).

Then, in separate tabs run:

```
```bash
yarn backend
yarn db:migrate # this will also seed the db
yarn zero
yarn dev
```
8 changes: 8 additions & 0 deletions apps/chat/src/postgres/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT 'CREATE DATABASE onechat'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'onechat')\gexec

SELECT 'CREATE DATABASE onechat_cvr'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'onechat_cvr')\gexec

SELECT 'CREATE DATABASE onechat_cdb'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'onechat_cdb')\gexec
Loading
Loading