Skip to content

Commit 955b5fd

Browse files
rsslldnphyclaude
andcommitted
Add database migration system for generating and running migrations
Introduces a complete migration workflow: - Schema diffing engine that compares TypeScript config against the live database - SQL migration file generation with timestamped naming - Safety checks inspired by strong_migrations (warns on drops, unsafe type casts, etc.) - Migration runner with PostgreSQL advisory locking for concurrent safety - Migration tracking table (_orm_migrations) with checksum verification - Support for non-transactional migrations via -- orm:no-transaction directive - CLI commands: orm generate migration, orm db migrate, orm db migrate status - Programmatic API: migrate.generate(), migrate.run(), migrate.status() Also enhances getForeignKeys to return onUpdate/onDelete actions and getUniqueConstraints to return structured column data and nullsNotDistinct. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 38ab217 commit 955b5fd

66 files changed

Lines changed: 5218 additions & 54 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(pnpm --filter @casekit/orm-cli --filter @casekit/orm-migrate test)"
5+
]
6+
}
7+
}

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: CI
22
env:
33
DO_NOT_TRACK: 1
44

5+
permissions:
6+
contents: read
7+
packages: read
8+
pull-requests: write
9+
510
on:
611
push:
712
branches: [main]
@@ -43,6 +48,7 @@ jobs:
4348
with:
4449
node-version-file: ".tool-versions"
4550
cache: "pnpm"
51+
registry-url: "https://npm.pkg.github.com"
4652

4753
- name: Get pnpm store directory
4854
shell: bash
@@ -51,6 +57,8 @@ jobs:
5157
5258
- name: Install dependencies
5359
run: pnpm install --frozen-lockfile
60+
env:
61+
NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
5462

5563
- name: Run tests
5664
run: pnpm test

.github/workflows/deploy-docs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88

99
permissions:
1010
contents: read
11+
packages: read
1112
pages: write
1213
id-token: write
1314

@@ -27,9 +28,12 @@ jobs:
2728
with:
2829
node-version: 20
2930
cache: pnpm
31+
registry-url: "https://npm.pkg.github.com"
3032

3133
- name: Install dependencies
3234
run: pnpm install
35+
env:
36+
NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
3337

3438
- name: Build docs
3539
run: pnpm --filter @casekit/orm-docs build
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: Publish Preview Packages
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches-ignore:
7+
- "dependabot/**"
8+
paths:
9+
- "packages/**"
10+
11+
jobs:
12+
publish:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
packages: write
17+
18+
steps:
19+
- uses: actions/checkout@v6
20+
21+
- name: Install pnpm
22+
uses: pnpm/action-setup@v4
23+
with:
24+
run_install: false
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v6
28+
with:
29+
node-version-file: ".tool-versions"
30+
cache: "pnpm"
31+
registry-url: "https://npm.pkg.github.com"
32+
33+
- name: Install dependencies
34+
run: pnpm install --frozen-lockfile
35+
env:
36+
NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
37+
38+
- name: Build packages
39+
run: pnpm build
40+
41+
- name: Generate version string
42+
id: version
43+
run: |
44+
BRANCH="${GITHUB_REF_NAME}"
45+
# Sanitize branch name for npm (replace / with -)
46+
BRANCH_SAFE=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//')
47+
SHORT_SHA="${GITHUB_SHA::7}"
48+
VERSION="0.0.0-${BRANCH_SAFE}.${SHORT_SHA}"
49+
echo "version=$VERSION" >> $GITHUB_OUTPUT
50+
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
51+
echo "Publishing version: $VERSION"
52+
53+
- name: Update package versions
54+
run: pnpm -r exec npm version ${{ steps.version.outputs.version }} --no-git-tag-version
55+
56+
- name: Publish packages
57+
run: pnpm -r publish --access public --tag preview --no-git-checks
58+
env:
59+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
61+
- name: Summary
62+
run: |
63+
echo "## Published Packages" >> $GITHUB_STEP_SUMMARY
64+
echo "" >> $GITHUB_STEP_SUMMARY
65+
echo "Version: \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
66+
echo "" >> $GITHUB_STEP_SUMMARY
67+
echo "Install with:" >> $GITHUB_STEP_SUMMARY
68+
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
69+
echo "pnpm add @casekit/orm@${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
70+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
71+
72+
cleanup:
73+
runs-on: ubuntu-latest
74+
needs: publish
75+
permissions:
76+
packages: write
77+
strategy:
78+
matrix:
79+
package:
80+
- orm
81+
- orm-cli
82+
- orm-schema
83+
- orm-config
84+
- orm-migrate
85+
- orm-testing
86+
- orm-fixtures
87+
- sql
88+
- toolbox
89+
90+
steps:
91+
- name: Delete old ${{ matrix.package }} versions
92+
uses: actions/delete-package-versions@v5
93+
with:
94+
package-name: ${{ matrix.package }}
95+
package-type: npm
96+
min-versions-to-keep: 50
97+
delete-only-pre-release-versions: true
98+
token: ${{ secrets.GITHUB_TOKEN }}

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@casekit:registry=https://npm.pkg.github.com

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ npm add @casekit/orm @casekit/orm-cli @casekit/orm-migrate pg zod
2121
### 1. Initialize your project
2222

2323
```bash
24-
npx orm init --directory ./src/db
24+
pnpm orm init --directory ./src/db
2525
```
2626

2727
### 2. Define your models
@@ -69,7 +69,7 @@ export const db = orm(config);
6969
### 4. Push schema to database
7070

7171
```bash
72-
npx orm db push
72+
pnpm orm db push
7373
```
7474

7575
### 5. Query with full type safety

docs/docs/cli/commands.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ All commands support these options:
2020
Initialize a new project with ORM configuration.
2121

2222
```bash
23-
npx orm init [options]
23+
pnpm orm init [options]
2424
```
2525

2626
### Options
@@ -33,7 +33,7 @@ npx orm init [options]
3333
### Example
3434

3535
```bash
36-
npx orm init --directory ./src/db
36+
pnpm orm init --directory ./src/db
3737
```
3838

3939
### Generated Files
@@ -48,12 +48,12 @@ npx orm init --directory ./src/db
4848
**DANGER** DO NOT USE THIS ON A PRODUCTION DATABASE, OR DATA LOSS MAY OCCUR.
4949
:::
5050

51-
Proper migration support will come in a future release.
51+
This command is for development only. For production environments, use [migrations](./migrations.md) instead.
5252

5353
Push the schema to the database, creating tables and constraints.
5454

5555
```bash
56-
npx orm db push
56+
pnpm orm db push
5757
```
5858

5959
This command:
@@ -66,7 +66,7 @@ This command:
6666
### Example
6767

6868
```bash
69-
npx orm db push
69+
pnpm orm db push
7070
```
7171

7272
Output:
@@ -85,7 +85,7 @@ Pushing schemas public to database
8585
Introspect the database and generate model files.
8686

8787
```bash
88-
npx orm db pull [options]
88+
pnpm orm db pull [options]
8989
```
9090

9191
### Options
@@ -99,13 +99,13 @@ npx orm db pull [options]
9999

100100
```bash
101101
# Pull from default schema
102-
npx orm db pull
102+
pnpm orm db pull
103103

104104
# Pull from specific schemas
105-
npx orm db pull --schema public --schema audit
105+
pnpm orm db pull --schema public --schema audit
106106

107107
# Force overwrite
108-
npx orm db pull --force
108+
pnpm orm db pull --force
109109
```
110110

111111
### Generated Files
@@ -134,7 +134,7 @@ src/db/models/
134134
Drop all schemas used by your models.
135135

136136
```bash
137-
npx orm db drop
137+
pnpm orm db drop
138138
```
139139

140140
:::warning
@@ -144,7 +144,7 @@ This is a destructive operation that deletes all data. Use with caution.
144144
### Example
145145

146146
```bash
147-
npx orm db drop
147+
pnpm orm db drop
148148
```
149149

150150
Output:
@@ -158,7 +158,7 @@ Output:
158158
Generate a skeleton model file.
159159

160160
```bash
161-
npx orm generate model <name> [options]
161+
pnpm orm generate model <name> [options]
162162
```
163163

164164
### Arguments
@@ -176,7 +176,7 @@ npx orm generate model <name> [options]
176176
### Example
177177

178178
```bash
179-
npx orm generate model user
179+
pnpm orm generate model user
180180
```
181181

182182
Creates `src/db/models/user.ts`:
@@ -197,22 +197,22 @@ And updates `src/db/models/index.ts` to export it.
197197

198198
```bash
199199
# 1. Initialize project
200-
npx orm init --directory ./src/db
200+
pnpm orm init --directory ./src/db
201201

202202
# 2. Write your models in ./src/db/models/
203203

204204
# 3. Push schema to database
205-
npx orm db push
205+
pnpm orm db push
206206
```
207207

208208
### Database-First (Generate Models from Database)
209209

210210
```bash
211211
# 1. Initialize project
212-
npx orm init --directory ./src/db
212+
pnpm orm init --directory ./src/db
213213

214214
# 2. Pull schema from existing database
215-
npx orm db pull --schema public
215+
pnpm orm db pull --schema public
216216

217217
# 3. Customize generated models as needed
218218
```
@@ -221,5 +221,5 @@ npx orm db pull --schema public
221221

222222
```bash
223223
# Drop and recreate
224-
npx orm db drop && npx orm db push
224+
pnpm orm db drop && pnpm orm db push
225225
```

docs/docs/cli/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ DB_PASSWORD=secret
148148
Specify a different config file:
149149

150150
```bash
151-
npx orm db push --config ./config/orm.config.ts
151+
pnpm orm db push --config ./config/orm.config.ts
152152
```
153153

154154
## Complete Example

0 commit comments

Comments
 (0)