-
Notifications
You must be signed in to change notification settings - Fork 452
feat!: add support for new Netlify Database primitive #8212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fbad04f
tmp: use unreleased @netlify/dev package to validate
pieh 094bee2
feat!: remove db extension commands and unflag database primitive comβ¦
pieh 4d36191
test: adjust spawnAsync mocking
pieh 44c9561
test: update snapshot for --help
pieh 2361208
tmp: update unreleased @netlify/dev
pieh 6f18dd1
fix: preserve honoring --skipGitignore
pieh 0c65e7e
test: allow e2e tests to use local tarball versions of dependencies
pieh 32092b7
docs: update cli command reference
pieh 019d487
fix: don't try to reject already resolved promise in spawnAsync
pieh a2175bc
test: remove debug console.log
pieh 1d2313a
docs: fix generating subcommands docs
pieh d2f3f3f
docs: don't generate top-level status:hooks docs into dabatase status
pieh 3661009
fix: use published @netlify/dev
pieh d8d60f4
Merge remote-tracking branch 'origin/main' into feat/unflag-database
pieh 27240cc
Merge branch 'main' into feat/unflag-database
pieh 692b167
Merge branch 'main' into feat/unflag-database
pieh 0cf185e
Merge branch 'main' into feat/unflag-database
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,211 +1,143 @@ | ||
| import { Option } from 'commander' | ||
| import inquirer from 'inquirer' | ||
|
|
||
| import BaseCommand from '../base-command.js' | ||
| import type { DatabaseBoilerplateType, DatabaseInitOptions } from './legacy/db-init.js' | ||
| import type { MigrationNewOptions } from './db-migration-new.js' | ||
| import type { MigrationPullOptions } from './db-migration-pull.js' | ||
| import type { MigrationsResetOptions } from './db-migrations-reset.js' | ||
| import type { DatabaseStatusOptions } from './db-status.js' | ||
|
|
||
| const supportedBoilerplates = new Set<DatabaseBoilerplateType>(['drizzle']) | ||
|
|
||
| export const createDatabaseCommand = (program: BaseCommand) => { | ||
| if (process.env.EXPERIMENTAL_NETLIFY_DB_ENABLED !== '1') { | ||
| const dbCommand = program | ||
| .command('db') | ||
| .alias('database') | ||
| .description(`Provision a production ready Postgres database with a single command`) | ||
| .addExamples(['netlify db status', 'netlify db init', 'netlify db init --help']) | ||
|
|
||
| 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('./legacy/db-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('./legacy/db-status.js') | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
| await status(options, command) | ||
| }) | ||
| } | ||
|
|
||
| if (process.env.EXPERIMENTAL_NETLIFY_DB_ENABLED === '1') { | ||
| const dbCommand = program | ||
| .command('database') | ||
| .alias('db') | ||
| .description(`Provision a production ready Postgres database with a single command`) | ||
| .addExamples([ | ||
| 'netlify database status', | ||
| 'netlify database migrations apply', | ||
| 'netlify database migrations pull', | ||
| 'netlify database migrations new', | ||
| 'netlify database reset', | ||
| ]) | ||
|
|
||
| dbCommand | ||
| .command('status') | ||
| .description('Check the status of the database, including applied and pending migrations') | ||
| .option('-b, --branch <branch>', 'Netlify branch name to query; defaults to the local development database') | ||
| .option( | ||
| '--show-credentials', | ||
| 'Include the full connection string (including username and password) in the output', | ||
| false, | ||
| ) | ||
| .option('--json', 'Output result as JSON') | ||
| .action(async (options: DatabaseStatusOptions, command: BaseCommand) => { | ||
| const { statusDb } = await import('./db-status.js') | ||
| await statusDb(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database status', | ||
| 'netlify database status --show-credentials', | ||
| 'netlify database status --json', | ||
| 'netlify database status --branch my-feature-branch', | ||
| ]) | ||
|
|
||
| dbCommand | ||
| .command('init') | ||
| .description('Interactive setup: install the package, scaffold a starter migration, and verify the database') | ||
| .option('-y, --yes', 'Non-interactive mode. Accepts the defaults for every prompt.', false) | ||
| .action(async (options: { yes?: boolean }, command: BaseCommand) => { | ||
| const { initDatabase } = await import('./db-init.js') | ||
| await initDatabase(options, command) | ||
| }) | ||
| .addExamples(['netlify database init', 'netlify database init --yes']) | ||
|
|
||
| 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('./db-connect.js') | ||
| await connect(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database connect', | ||
| 'netlify database connect --query "SELECT * FROM users"', | ||
| 'netlify database connect --json --query "SELECT * FROM users"', | ||
| 'netlify database 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('./db-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('./db-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('./db-migration-new.js') | ||
| await migrationNew(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database migrations new', | ||
| 'netlify database 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('./db-migration-pull.js') | ||
| await migrationPull(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database migrations pull', | ||
| 'netlify database migrations pull --branch staging', | ||
| 'netlify database migrations pull --branch', | ||
| 'netlify database migrations pull --force', | ||
| ]) | ||
|
|
||
| migrationsCommand | ||
| .command('reset') | ||
| .description('Delete local migration files that have not been applied yet') | ||
| .option('-b, --branch <branch>', 'Target a remote preview branch instead of the local development database') | ||
| .option('--json', 'Output result as JSON') | ||
| .action(async (options: MigrationsResetOptions, command: BaseCommand) => { | ||
| const { migrationsReset } = await import('./db-migrations-reset.js') | ||
| await migrationsReset(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database migrations reset', | ||
| 'netlify database migrations reset --branch my-feature-branch', | ||
| ]) | ||
| } | ||
| const dbCommand = program | ||
| .command('database') | ||
| .alias('db') | ||
| .description(`Provision a production ready Postgres database with a single command`) | ||
| .addExamples([ | ||
| 'netlify database status', | ||
| 'netlify database migrations apply', | ||
| 'netlify database migrations pull', | ||
| 'netlify database migrations new', | ||
| 'netlify database reset', | ||
| ]) | ||
|
|
||
| dbCommand | ||
| .command('status') | ||
| .description('Check the status of the database, including applied and pending migrations') | ||
| .option('-b, --branch <branch>', 'Netlify branch name to query; defaults to the local development database') | ||
| .option( | ||
| '--show-credentials', | ||
| 'Include the full connection string (including username and password) in the output', | ||
| false, | ||
| ) | ||
| .option('--json', 'Output result as JSON') | ||
| .action(async (options: DatabaseStatusOptions, command: BaseCommand) => { | ||
| const { statusDb } = await import('./db-status.js') | ||
| await statusDb(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database status', | ||
| 'netlify database status --show-credentials', | ||
| 'netlify database status --json', | ||
| 'netlify database status --branch my-feature-branch', | ||
| ]) | ||
|
|
||
| dbCommand | ||
| .command('init') | ||
| .description('Interactive setup: install the package, scaffold a starter migration, and verify the database') | ||
| .option('-y, --yes', 'Non-interactive mode. Accepts the defaults for every prompt.', false) | ||
| .action(async (options: { yes?: boolean }, command: BaseCommand) => { | ||
| const { initDatabase } = await import('./db-init.js') | ||
| await initDatabase(options, command) | ||
| }) | ||
| .addExamples(['netlify database init', 'netlify database init --yes']) | ||
|
|
||
| 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('./db-connect.js') | ||
| await connect(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database connect', | ||
| 'netlify database connect --query "SELECT * FROM users"', | ||
| 'netlify database connect --json --query "SELECT * FROM users"', | ||
| 'netlify database 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('./db-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('./db-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('./db-migration-new.js') | ||
| await migrationNew(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database migrations new', | ||
| 'netlify database 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('./db-migration-pull.js') | ||
| await migrationPull(options, command) | ||
| }) | ||
| .addExamples([ | ||
| 'netlify database migrations pull', | ||
| 'netlify database migrations pull --branch staging', | ||
| 'netlify database migrations pull --branch', | ||
| 'netlify database migrations pull --force', | ||
| ]) | ||
|
|
||
| migrationsCommand | ||
| .command('reset') | ||
| .description('Delete local migration files that have not been applied yet') | ||
| .option('-b, --branch <branch>', 'Target a remote preview branch instead of the local development database') | ||
| .option('--json', 'Output result as JSON') | ||
| .action(async (options: MigrationsResetOptions, command: BaseCommand) => { | ||
| const { migrationsReset } = await import('./db-migrations-reset.js') | ||
| await migrationsReset(options, command) | ||
| }) | ||
| .addExamples(['netlify database migrations reset', 'netlify database migrations reset --branch my-feature-branch']) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.