Skip to content

Commit 444bfb9

Browse files
authored
feat: execute db commands on running instance (#8002)
1 parent 3439cd4 commit 444bfb9

File tree

8 files changed

+307
-137
lines changed

8 files changed

+307
-137
lines changed

package-lock.json

Lines changed: 162 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@netlify/build": "35.8.7",
6565
"@netlify/build-info": "10.3.1",
6666
"@netlify/config": "24.4.3",
67-
"@netlify/dev": "4.15.0",
67+
"@netlify/dev": "4.16.0",
6868
"@netlify/dev-utils": "4.4.0",
6969
"@netlify/edge-bundler": "14.9.14",
7070
"@netlify/edge-functions": "3.0.3",
@@ -138,6 +138,7 @@
138138
"p-map": "7.0.3",
139139
"p-wait-for": "6.0.0",
140140
"parallel-transform": "1.2.0",
141+
"pg": "^8.11.0",
141142
"parse-github-url": "1.0.3",
142143
"prettyjson": "1.2.5",
143144
"raw-body": "3.0.1",
@@ -182,6 +183,7 @@
182183
"@types/node": "22.18.11",
183184
"@types/node-fetch": "2.6.13",
184185
"@types/parallel-transform": "1.1.4",
186+
"@types/pg": "^8.11.0",
185187
"@types/parse-github-url": "1.0.3",
186188
"@types/picomatch": "4.0.2",
187189
"@types/prettyjson": "0.0.33",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Client } from 'pg'
2+
3+
import type { SQLExecutor } from '@netlify/dev'
4+
import { NetlifyDev } from '@netlify/dev'
5+
import { LocalState } from '@netlify/dev-utils'
6+
7+
import { PgClientExecutor } from './pg-client-executor.js'
8+
9+
interface DBConnection {
10+
executor: SQLExecutor
11+
cleanup: () => Promise<void>
12+
}
13+
14+
export async function connectToDatabase(buildDir: string): Promise<DBConnection> {
15+
const state = new LocalState(buildDir)
16+
const connectionString = state.get('dbConnectionString')
17+
18+
if (connectionString) {
19+
const client = new Client({ connectionString })
20+
await client.connect()
21+
return {
22+
executor: new PgClientExecutor(client),
23+
cleanup: () => client.end(),
24+
}
25+
}
26+
27+
const netlifyDev = new NetlifyDev({
28+
projectRoot: buildDir,
29+
aiGateway: { enabled: false },
30+
blobs: { enabled: false },
31+
edgeFunctions: { enabled: false },
32+
environmentVariables: { enabled: false },
33+
functions: { enabled: false },
34+
geolocation: { enabled: false },
35+
headers: { enabled: false },
36+
images: { enabled: false },
37+
redirects: { enabled: false },
38+
staticFiles: { enabled: false },
39+
serverAddress: null,
40+
})
41+
42+
await netlifyDev.start()
43+
44+
const { db } = netlifyDev
45+
if (!db) {
46+
await netlifyDev.stop()
47+
throw new Error('Local database failed to start. Set EXPERIMENTAL_NETLIFY_DB_ENABLED=1 to enable.')
48+
}
49+
50+
return {
51+
executor: db,
52+
cleanup: () => netlifyDev.stop(),
53+
}
54+
}

src/commands/database/migrate.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { NetlifyDev } from '@netlify/dev'
1+
import { applyMigrations } from '@netlify/dev'
22

33
import { log, logJson } from '../../utils/command-helpers.js'
44
import BaseCommand from '../base-command.js'
5+
import { connectToDatabase } from './db-connection.js'
56

67
export interface MigrateOptions {
78
to?: string
@@ -22,30 +23,10 @@ export const migrate = async (options: MigrateOptions, command: BaseCommand) =>
2223
)
2324
}
2425

25-
const netlifyDev = new NetlifyDev({
26-
projectRoot: buildDir,
27-
aiGateway: { enabled: false },
28-
blobs: { enabled: false },
29-
edgeFunctions: { enabled: false },
30-
environmentVariables: { enabled: false },
31-
functions: { enabled: false },
32-
geolocation: { enabled: false },
33-
headers: { enabled: false },
34-
images: { enabled: false },
35-
redirects: { enabled: false },
36-
staticFiles: { enabled: false },
37-
serverAddress: null,
38-
})
26+
const { executor, cleanup } = await connectToDatabase(buildDir)
3927

4028
try {
41-
await netlifyDev.start()
42-
43-
const { db } = netlifyDev
44-
if (!db) {
45-
throw new Error('Local database failed to start. Set EXPERIMENTAL_NETLIFY_DB_ENABLED=1 to enable.')
46-
}
47-
48-
const applied = await db.applyMigrations(migrationsDirectory, name)
29+
const applied = await applyMigrations(executor, migrationsDirectory, name)
4930

5031
if (json) {
5132
logJson({ migrations_applied: applied })
@@ -58,6 +39,6 @@ export const migrate = async (options: MigrateOptions, command: BaseCommand) =>
5839
}
5940
}
6041
} finally {
61-
await netlifyDev.stop()
42+
await cleanup()
6243
}
6344
}

0 commit comments

Comments
 (0)