-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathstatus.ts
More file actions
90 lines (83 loc) · 3.4 KB
/
status.ts
File metadata and controls
90 lines (83 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { OptionValues } from 'commander'
import { SiteInfo } from './database.js'
import BaseCommand from '../base-command.js'
import { getAccount, getExtension, getSiteConfiguration } from './utils.js'
import { NEON_DATABASE_EXTENSION_SLUG } from './constants.js'
import prettyjson from 'prettyjson'
import { chalk, log } from '../../utils/command-helpers.js'
export const status = async (_options: OptionValues, command: BaseCommand) => {
const siteInfo = command.netlify.siteInfo as SiteInfo
if (!command.siteId) {
throw new Error(`The project must be linked with netlify link before initializing a database.`)
}
if (!siteInfo.account_id) {
throw new Error(`No account id found for site ${command.siteId}`)
}
if (!command.netlify.api.accessToken) {
throw new Error(`You must be logged in with netlify login to check the status of the database`)
}
const netlifyToken = command.netlify.api.accessToken.replace('Bearer ', '')
const account = await getAccount(command, { accountId: siteInfo.account_id })
let databaseUrlEnv: Awaited<ReturnType<typeof command.netlify.api.getEnvVar>> | undefined
let unpooledDatabaseUrlEnv: Awaited<ReturnType<typeof command.netlify.api.getEnvVar>> | undefined
try {
databaseUrlEnv = await command.netlify.api.getEnvVar({
accountId: siteInfo.account_id,
siteId: command.siteId,
key: 'NETLIFY_DATABASE_URL',
})
} catch {
// no-op, env var does not exist, so we just continue
}
try {
unpooledDatabaseUrlEnv = await command.netlify.api.getEnvVar({
accountId: siteInfo.account_id,
siteId: command.siteId,
key: 'NETLIFY_DATABASE_URL_UNPOOLED',
})
} catch {
// no-op, env var does not exist, so we just continue
}
const extension = await getExtension({
accountId: account.id,
netlifyToken: netlifyToken,
slug: NEON_DATABASE_EXTENSION_SLUG,
})
let siteConfig
try {
siteConfig = await getSiteConfiguration({
siteId: command.siteId,
accountId: siteInfo.account_id,
slug: NEON_DATABASE_EXTENSION_SLUG,
netlifyToken: netlifyToken,
})
} catch {
// no-op, site config does not exist or extension not installed
}
log(
prettyjson.render({
'Current team': account.name,
'Current site': siteInfo.name,
[extension?.name ? `${extension.name} extension` : 'Database extension']: extension?.installedOnTeam
? 'installed on team'
: chalk.red('not installed on team'),
// @ts-expect-error -- siteConfig is not typed
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
['Database status']: siteConfig?.config?.connectedDatabase ? 'connected to site' : chalk.red('not connected'),
['Environment variables']: '',
[' NETLIFY_DATABASE_URL']: databaseUrlEnv?.key === 'NETLIFY_DATABASE_URL' ? 'saved' : chalk.red('not set'),
[' NETLIFY_DATABASE_URL_UNPOOLED']:
unpooledDatabaseUrlEnv?.key === 'NETLIFY_DATABASE_URL_UNPOOLED' ? 'saved' : chalk.red('not set'),
}),
)
// Show deprecation warning if a legacy extension DB is detected
if (databaseUrlEnv?.key === 'NETLIFY_DATABASE_URL') {
log()
log(
chalk.yellow(
'Warning: This site has a database from the legacy Netlify DB extension, which has been deprecated. Netlify DB is now available as a built-in feature.',
),
)
log(`For migration instructions, visit ${chalk.cyan('https://ntl.fyi/db-migration')}`)
}
}