Skip to content

Commit

Permalink
fix(data-migrate): provide option to use db in dist instead of src (r…
Browse files Browse the repository at this point in the history
…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
jtoar committed May 24, 2023
1 parent 1807430 commit a7334cf
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 261 deletions.
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'
)}`
)
}
91 changes: 5 additions & 86 deletions packages/cli/src/commands/dataMigrate/install.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
import path from 'path'

import execa from 'execa'
import fs from 'fs-extra'
import { Listr } from 'listr2'
import terminalLink from 'terminal-link'

import { errorTelemetry } from '@redwoodjs/telemetry'

import { getPaths } from '../../lib'
import c from '../../lib/colors'

const MODEL = `model RW_DataMigration {
version String @id
name String
startedAt DateTime
finishedAt DateTime
}`

const POST_INSTALL_INSTRUCTIONS = `${c.warning(
"Don't forget to apply your migration when ready:"
)}
${c.bold('yarn rw prisma migrate dev')}
`

// Creates dataMigrations directory
const createPath = () => {
return fs.outputFileSync(
path.join(getPaths().api.dataMigrations, '.keep'),
''
)
}

// Appends RW_DataMigration model to schema.prisma
const appendModel = () => {
const schemaPath = getPaths().api.dbSchema
const schema = fs.readFileSync(schemaPath).toString()
const newSchema = `${schema}\n${MODEL}\n`

return fs.writeFileSync(schemaPath, newSchema)
}

// Create a new migration
const save = async () => {
return await execa(
'yarn rw',
['prisma migrate dev', '--name create_data_migrations', '--create-only'],
{
cwd: getPaths().api.base,
shell: true,
}
)
}

export const command = 'install'
export const description = 'Add the RW_DataMigration model to your schema'
export const builder = (yargs) => {

export function builder(yargs) {
yargs.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
Expand All @@ -64,36 +12,7 @@ export const builder = (yargs) => {
)
}

export const handler = async () => {
const tasks = new Listr(
[
{
title: `Creating dataMigrations directory...`,
task: createPath,
},
{
title: 'Adding RW_DataMigration model to schema.prisma...',
task: await appendModel,
},
{
title: 'Create db migration...',
task: await save,
},
{
title: 'One more thing...',
task: (_ctx, task) => {
task.title = `Next steps:\n ${POST_INSTALL_INSTRUCTIONS}`
},
},
],
{ 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)
}
export async function handler(options) {
const { handler } = await import('./installHandler')
return handler(options)
}
79 changes: 79 additions & 0 deletions packages/cli/src/commands/dataMigrate/installHandler.js
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
}`

0 comments on commit a7334cf

Please sign in to comment.