Skip to content

Commit

Permalink
Merge pull request #157 from ixartz/drizzle-prod
Browse files Browse the repository at this point in the history
Improve Drizzle and Turso DX for production
  • Loading branch information
ixartz committed Aug 27, 2023
2 parents f927f73 + 964cfa1 commit 69fac9e
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 409 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
name: Build with ${{ matrix.node-version }}
runs-on: ubuntu-latest

env:
MIGRATE_DB: true

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -24,7 +27,7 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run build
- run: npx next build

test:
strategy:
Expand Down
7 changes: 1 addition & 6 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* eslint-disable-next-line import/no-extraneous-dependencies */
import dotenv from 'dotenv';
import type { Config } from 'drizzle-kit';

dotenv.config({
path: '.env',
});

/** @type {import('drizzle-kit').Config} */
export default {
out: './migrations',
schema: './src/models/schema.ts',
Expand Down
916 changes: 523 additions & 393 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "3.16.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "npm run db:migrate && next build",
"start": "next start",
"build-stats": "cross-env ANALYZE=true npm run build",
"clean": "rimraf .next .swc out coverage",
Expand All @@ -14,7 +14,8 @@
"commit": "cz",
"db:generate": "drizzle-kit generate:sqlite",
"db:push": "drizzle-kit push:sqlite",
"db:studio": "drizzle-kit studio",
"db:migrate": "tsx ./scripts/DbMigrate.ts",
"db:studio": "dotenv -c -- drizzle-kit studio",
"cypress": "cypress open",
"cypress:headless": "cypress run",
"e2e": "start-server-and-test dev http://localhost:3000 cypress",
Expand Down Expand Up @@ -62,7 +63,7 @@
"@testing-library/jest-dom": "^6.1.2",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.4",
"@types/node": "^20.5.4",
"@types/node": "^20.5.6",
"@types/react": "^18.2.21",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
Expand All @@ -71,18 +72,18 @@
"cross-env": "^7.0.3",
"cssnano": "^6.0.1",
"cypress": "^12.17.4",
"dotenv": "^16.3.1",
"dotenv-cli": "^7.3.0",
"drizzle-kit": "^0.19.13",
"encoding": "^0.1.13",
"eslint": "^8.47.0",
"eslint": "^8.48.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-next": "^13.4.19",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-cypress": "^2.14.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.2.3",
"eslint-plugin-jest-dom": "^5.0.2",
"eslint-plugin-jest-dom": "^5.1.0",
"eslint-plugin-jest-formatting": "^3.1.0",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-prettier": "^5.0.0",
Expand All @@ -91,7 +92,7 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-storybook": "^0.6.13",
"eslint-plugin-tailwindcss": "^3.13.0",
"eslint-plugin-testing-library": "^6.0.0",
"eslint-plugin-testing-library": "^6.0.1",
"eslint-plugin-unused-imports": "^3.0.0",
"http-server": "^14.1.1",
"husky": "^8.0.3",
Expand All @@ -106,7 +107,8 @@
"start-server-and-test": "^2.0.0",
"storybook": "^7.0.27",
"tailwindcss": "^3.3.3",
"typescript": "^5.1.6"
"tsx": "^3.12.7",
"typescript": "^5.2.2"
},
"config": {
"commitizen": {
Expand Down
27 changes: 27 additions & 0 deletions scripts/DbMigrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-console */
import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import { migrate } from 'drizzle-orm/libsql/migrator';

async function main() {
console.log('Migration started');

const client = createClient({
url: process.env.DATABASE_URL ?? '',
authToken: process.env.DATABASE_AUTH_TOKEN ?? '',
});

const db = drizzle(client);

await migrate(db, { migrationsFolder: './migrations' });

console.log('Migration completed');

process.exit(0);
}

main().catch((error) => {
console.error('Migration failed');
console.log(error);
process.exit(1);
});
2 changes: 2 additions & 0 deletions src/app/guestbook/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ const Guestbook = async () => {
);
};

export const dynamic = 'force-dynamic';

export default Guestbook;
4 changes: 3 additions & 1 deletion src/libs/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ const client = createClient({

export const db = drizzle(client);

await migrate(db, { migrationsFolder: './migrations' });
if (Env.NODE_ENV !== 'production' || Env.MIGRATE_DB) {
await migrate(db, { migrationsFolder: './migrations' });
}
2 changes: 2 additions & 0 deletions src/libs/Env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const Env = createEnv({
CLERK_SECRET_KEY: z.string().nonempty(),
DATABASE_URL: z.string().nonempty(),
DATABASE_AUTH_TOKEN: z.string().optional(),
MIGRATE_DB: z.coerce.boolean().optional(),
},
client: {
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().nonempty(),
Expand All @@ -23,6 +24,7 @@ export const Env = createEnv({
NODE_ENV: process.env.NODE_ENV,
DATABASE_URL: process.env.DATABASE_URL,
DATABASE_AUTH_TOKEN: process.env.DATABASE_AUTH_TOKEN,
MIGRATE_DB: process.env.MIGRATE_DB,
CLERK_SECRET_KEY: process.env.CLERK_SECRET_KEY,
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY:
process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
Expand Down

0 comments on commit 69fac9e

Please sign in to comment.