-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathdatabase.ts
More file actions
198 lines (181 loc) · 7.25 KB
/
database.ts
File metadata and controls
198 lines (181 loc) · 7.25 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Option } from 'commander'
import inquirer from 'inquirer'
import BaseCommand from '../base-command.js'
import type { DatabaseBoilerplateType, DatabaseInitOptions } from './init.js'
import type { MigrationNewOptions } from './migration-new.js'
import type { MigrationPullOptions } from './migration-pull.js'
export type Extension = {
id: string
name: string
slug: string
hostSiteUrl: string
installedOnTeam: boolean
}
export type SiteInfo = {
id: string
name: string
account_id: string
admin_url: string
url: string
ssl_url: string
}
const supportedBoilerplates = new Set<DatabaseBoilerplateType>(['drizzle'])
export const createDatabaseCommand = (program: BaseCommand) => {
const dbCommand = program
.command('db')
.alias('database')
.description(`Provision a production ready Postgres database with a single command`)
.addExamples([
'netlify db status',
...(process.env.EXPERIMENTAL_NETLIFY_DB_ENABLED === '1'
? ['netlify db migrations apply', 'netlify db migrations pull', 'netlify db reset', 'netlify db migrations new']
: ['netlify db init', 'netlify db init --help']),
])
if (process.env.EXPERIMENTAL_NETLIFY_DB_ENABLED !== '1') {
dbCommand
.command('init')
.description(`Initialize a new database for the current site`)
.option(
'--assume-no',
'Non-interactive setup. Does not initialize any third-party tools/boilerplate. Ideal for CI environments or AI tools.',
false,
)
.addOption(
new Option('--boilerplate <tool>', 'Type of boilerplate to add to your project.').choices(
Array.from(supportedBoilerplates).sort(),
),
)
.option('--no-boilerplate', "Don't add any boilerplate to your project.")
.option('-o, --overwrite', 'Overwrites existing files that would be created when setting up boilerplate')
.action(async (_options: Record<string, unknown>, command: BaseCommand) => {
const { init } = await import('./init.js')
// Only prompt for drizzle if the user did not specify a boilerplate option, and if we're in
// interactive mode
if (_options.boilerplate === undefined && !_options.assumeNo) {
const answers = await inquirer.prompt<{ useDrizzle: boolean }>([
{
type: 'confirm',
name: 'useDrizzle',
message: 'Set up Drizzle boilerplate?',
},
])
if (answers.useDrizzle) {
command.setOptionValue('boilerplate', 'drizzle')
}
}
const options = _options as DatabaseInitOptions
if (options.assumeNo) {
options.boilerplate = false
options.overwrite = false
}
await init(options, command)
})
.addExamples([`netlify db init --assume-no`, `netlify db init --boilerplate=drizzle --overwrite`])
}
dbCommand
.command('status')
.description(`Check the status of the database`)
.action(async (options, command) => {
const { status } = await import('./status.js')
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await status(options, command)
})
if (process.env.EXPERIMENTAL_NETLIFY_DB_ENABLED === '1') {
dbCommand
.command('init')
.description('Initialize a new database for the current site')
.action(async (_options: Record<string, unknown>, _command: BaseCommand) => {
const { log, chalk } = await import('../../utils/command-helpers.js')
log()
log(
chalk.yellow(
'`netlify db init` is no longer available. Databases are now provisioned automatically when @netlify/db is detected in your project.',
),
)
log()
log('To get started, run:')
log(` ${chalk.cyan('npm install @netlify/db')}`)
log()
log(
`If you have an existing database from the Netlify DB extension, visit ${chalk.cyan(
'https://ntl.fyi/db-migration',
)} for migration instructions.`,
)
log()
})
dbCommand
.command('connect')
.description('Connect to the database')
.option('-q, --query <sql>', 'Execute a single query and exit')
.option(
'--json',
'Output query results as JSON. When used without --query, prints the connection details as JSON instead.',
)
.action(async (options: { query?: string; json?: boolean }, command: BaseCommand) => {
const { connect } = await import('./connect.js')
await connect(options, command)
})
.addExamples([
'netlify db connect',
'netlify db connect --query "SELECT * FROM users"',
'netlify db connect --json --query "SELECT * FROM users"',
'netlify db connect --json',
])
dbCommand
.command('reset')
.description('Reset the local development database, removing all data and tables')
.option('--json', 'Output result as JSON')
.action(async (options: { json?: boolean }, command: BaseCommand) => {
const { reset } = await import('./reset.js')
await reset(options, command)
})
const migrationsCommand = dbCommand.command('migrations').description('Manage database migrations')
migrationsCommand
.command('apply')
.description('Apply database migrations to the local development database')
.option('--to <name>', 'Target migration name or prefix to apply up to (applies all if omitted)')
.option('--json', 'Output result as JSON')
.action(async (options: { to?: string; json?: boolean }, command: BaseCommand) => {
const { migrate } = await import('./migrate.js')
await migrate(options, command)
})
migrationsCommand
.command('new')
.description('Create a new migration')
.option('-d, --description <description>', 'Purpose of the migration (used to generate the file name)')
.addOption(
new Option('-s, --scheme <scheme>', 'Numbering scheme for migration prefixes').choices([
'sequential',
'timestamp',
]),
)
.option('--json', 'Output result as JSON')
.action(async (options: MigrationNewOptions, command: BaseCommand) => {
const { migrationNew } = await import('./migration-new.js')
await migrationNew(options, command)
})
.addExamples([
'netlify db migrations new',
'netlify db migrations new --description "add users table" --scheme sequential',
])
migrationsCommand
.command('pull')
.description('Pull migrations and overwrite local migration files')
.option(
'-b, --branch [branch]',
"Pull migrations for a specific branch (defaults to 'production'; pass --branch with no value to use local git branch)",
)
.option('--force', 'Skip confirmation prompt', false)
.option('--json', 'Output result as JSON')
.action(async (options: MigrationPullOptions, command: BaseCommand) => {
const { migrationPull } = await import('./migration-pull.js')
await migrationPull(options, command)
})
.addExamples([
'netlify db migrations pull',
'netlify db migrations pull --branch staging',
'netlify db migrations pull --branch',
'netlify db migrations pull --force',
])
}
}