Skip to content

Commit

Permalink
--reset-db flag on keystone-next dev (keystonejs#5321)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Mar 31, 2021
1 parent 0f335ac commit 5c4b486
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-falcons-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': minor
---

Added `--reset-db` flag to `keystone-next dev` to reset the database before starting Keystone
58 changes: 34 additions & 24 deletions packages-next/keystone/src/lib/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function pushPrismaSchemaToDatabase(
let migration = await withMigrate(dbUrl, schemaPath, async migrate => {
if (shouldDropDatabase) {
await runMigrateWithDbUrl(dbUrl, () => migrate.engine.reset());
console.log('✨ Your database has been reset');
}
return runMigrateWithDbUrl(dbUrl, () =>
migrate.engine.schemaPush({
Expand All @@ -69,36 +70,45 @@ export async function pushPrismaSchemaToDatabase(
}

// TODO: don't have process.exit calls here
export async function devMigrations(dbUrl: string, prismaSchema: string, schemaPath: string) {
export async function devMigrations(
dbUrl: string,
prismaSchema: string,
schemaPath: string,
shouldDropDatabase: boolean
) {
return withMigrate(dbUrl, schemaPath, async migrate => {
// see if we need to reset the database
// note that the other action devDiagnostic can return is createMigration
// that doesn't necessarily mean that we need to create a migration
// it only means that we don't need to reset the database
const devDiagnostic = await runMigrateWithDbUrl(dbUrl, () => migrate.devDiagnostic());
// when the action is reset, the database is somehow inconsistent with the migrations so we need to reset it
// (not just some migrations need to be applied but there's some inconsistency)
if (devDiagnostic.action.tag === 'reset') {
const credentials = uriToCredentials(dbUrl);
console.log(`${devDiagnostic.action.reason}
if (shouldDropDatabase) {
await runMigrateWithDbUrl(dbUrl, () => migrate.reset());
console.log('✨ Your database has been reset');
} else {
// see if we need to reset the database
// note that the other action devDiagnostic can return is createMigration
// that doesn't necessarily mean that we need to create a migration
// it only means that we don't need to reset the database
const devDiagnostic = await runMigrateWithDbUrl(dbUrl, () => migrate.devDiagnostic());
// when the action is reset, the database is somehow inconsistent with the migrations so we need to reset it
// (not just some migrations need to be applied but there's some inconsistency)
if (devDiagnostic.action.tag === 'reset') {
const credentials = uriToCredentials(dbUrl);
console.log(`${devDiagnostic.action.reason}
We need to reset the ${credentials.type} database "${credentials.database}" at ${getDbLocation(
credentials
)}.`);
const confirmedReset = await confirmPrompt(
`Do you want to continue? ${chalk.red('All data will be lost')}.`
);
console.info(); // empty line
credentials
)}.`);
const confirmedReset = await confirmPrompt(
`Do you want to continue? ${chalk.red('All data will be lost')}.`
);
console.info(); // empty line

if (!confirmedReset) {
console.info('Reset cancelled.');
process.exit(0);
}
if (!confirmedReset) {
console.info('Reset cancelled.');
process.exit(0);
}

// Do the reset
await migrate.reset();
// Do the reset
await runMigrateWithDbUrl(dbUrl, () => migrate.reset());
}
}

let { appliedMigrationNames } = await runMigrateWithDbUrl(dbUrl, () =>
migrate.applyMigrations()
);
Expand Down
3 changes: 3 additions & 0 deletions packages-next/keystone/src/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function cli() {
{
flags: {
fix: { default: false, type: 'boolean' },
resetDb: { default: false, type: 'boolean' },
},
}
);
Expand All @@ -38,6 +39,8 @@ function cli() {
prisma(cwd, process.argv.slice(3));
} else if (command === 'postinstall') {
postinstall(cwd, flags.fix);
} else if (command === 'dev') {
dev(cwd, flags.resetDb);
} else {
commands[command](cwd);
}
Expand Down
16 changes: 13 additions & 3 deletions packages-next/keystone/src/scripts/run/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const devLoadingHTMLFilepath = path.join(
'dev-loading.html'
);

export const dev = async (cwd: string) => {
export const dev = async (cwd: string, shouldDropDatabase: boolean) => {
console.log('✨ Starting Keystone');

const server = express();
Expand All @@ -39,9 +39,19 @@ export const dev = async (cwd: string) => {

if (config.db.adapter === 'prisma_postgresql' || config.db.adapter === 'prisma_sqlite') {
if (config.db.useMigrations) {
await devMigrations(config.db.url, prismaSchema, getSchemaPaths(cwd).prisma);
await devMigrations(
config.db.url,
prismaSchema,
getSchemaPaths(cwd).prisma,
shouldDropDatabase
);
} else {
await pushPrismaSchemaToDatabase(config.db.url, prismaSchema, getSchemaPaths(cwd).prisma);
await pushPrismaSchemaToDatabase(
config.db.url,
prismaSchema,
getSchemaPaths(cwd).prisma,
shouldDropDatabase
);
}
}
}
Expand Down

0 comments on commit 5c4b486

Please sign in to comment.