forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(data-migrate): provide option to use db in dist instead of src (r…
…edwoodjs#8375) * separate out up, install handlers * remove needless async * add missing @ * fix import * use values not entries * fix data structure * add back early return * rename to camel case * feat: support `--dist-path` flag * follow up fixes * apply suggestions from review
- Loading branch information
Showing
7 changed files
with
355 additions
and
261 deletions.
There are no files selected for viewing
13 changes: 9 additions & 4 deletions
13
packages/cli/src/commands/data-migrate.js → packages/cli/src/commands/dataMigrate.js
This file contains 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,15 +1,20 @@ | ||
import terminalLink from 'terminal-link' | ||
|
||
import * as installCommand from './dataMigrate/install' | ||
import * as upCommand from './dataMigrate/up' | ||
|
||
export const command = 'data-migrate <command>' | ||
export const aliases = ['dm', 'dataMigrate'] | ||
export const description = 'Migrate the data in your database' | ||
import terminalLink from 'terminal-link' | ||
|
||
export const builder = (yargs) => | ||
export function builder(yargs) { | ||
yargs | ||
.commandDir('./dataMigrate') | ||
.demandCommand() | ||
.command(installCommand) | ||
.command(upCommand) | ||
.epilogue( | ||
`Also see the ${terminalLink( | ||
'Redwood CLI Reference', | ||
'https://redwoodjs.com/docs/cli-commands#datamigrate' | ||
)}` | ||
) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import path from 'path' | ||
|
||
import execa from 'execa' | ||
import fs from 'fs-extra' | ||
import { Listr } from 'listr2' | ||
|
||
import { errorTelemetry } from '@redwoodjs/telemetry' | ||
|
||
import { getPaths } from '../../lib' | ||
import c from '../../lib/colors' | ||
|
||
const redwoodProjectPaths = getPaths() | ||
|
||
export async function handler() { | ||
const tasks = new Listr( | ||
[ | ||
{ | ||
title: `Creating the dataMigrations directory...`, | ||
task() { | ||
fs.outputFileSync( | ||
path.join(getPaths().api.dataMigrations, '.keep'), | ||
'' | ||
) | ||
}, | ||
}, | ||
{ | ||
title: 'Adding the RW_DataMigration model to schema.prisma...', | ||
task() { | ||
const dbSchemaPath = redwoodProjectPaths.api.dbSchema | ||
|
||
const dbSchema = fs.readFileSync(dbSchemaPath, 'utf-8') | ||
const newDbSchema = [dbSchema, RW_DATA_MIGRATION_MODEL, ''].join('\n') | ||
|
||
fs.writeFileSync(dbSchemaPath, newDbSchema) | ||
}, | ||
}, | ||
{ | ||
title: 'Creating the database migration...', | ||
task() { | ||
return execa.command( | ||
'yarn rw prisma migrate dev --name create_data_migrations --create-only', | ||
{ | ||
cwd: redwoodProjectPaths.api.base, | ||
} | ||
).stdout | ||
}, | ||
}, | ||
{ | ||
title: 'One more thing...', | ||
task(_ctx, task) { | ||
task.title = [ | ||
'Next steps:', | ||
c.warning( | ||
"Don't forget to apply your migration when you're ready:" | ||
), | ||
c.bold('yarn rw prisma migrate dev'), | ||
].join('\n') | ||
}, | ||
}, | ||
], | ||
{ rendererOptions: { collapseSubtasks: false }, exitOnError: true } | ||
) | ||
|
||
try { | ||
await tasks.run() | ||
} catch (e) { | ||
errorTelemetry(process.argv, e.message) | ||
console.error(c.error(e.message)) | ||
process.exit(e?.exitCode || 1) | ||
} | ||
} | ||
|
||
const RW_DATA_MIGRATION_MODEL = `\ | ||
model RW_DataMigration { | ||
version String @id | ||
name String | ||
startedAt DateTime | ||
finishedAt DateTime | ||
}` |
Oops, something went wrong.