Skip to content
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

Add start --with-migrations tests #8360

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/core/src/scripts/tests/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'path';
import fs from 'fs-extra';
import execa from 'execa';
import { setSkipWatching } from '../run/dev';
import { ExitError } from '../utils';
import {
cliBinPath,
getFiles,
introspectDb,
recordConsole,
Expand Down Expand Up @@ -662,3 +664,89 @@ describe('useMigrations: true', () => {
`);
});
});

describe('start --with-migrations', () => {
async function startAndStopServer(tmp: string) {
const startResult = execa('node', [cliBinPath, 'start', '--no-ui', '--with-migrations'], {
reject: false,
all: true,
cwd: tmp,
});

let output = '';
try {
await Promise.race([
new Promise((resolve, reject) =>
setTimeout(() => reject(new Error(`timed out. output:\n${output}`)), 10000)
),
new Promise<void>(resolve => {
startResult.all!.on('data', data => {
output += data;
if (output.includes('Server listening on :3000 (http://localhost:3000/)')) {
resolve();
}
});
}),
]);
} finally {
startResult.kill();
}
return output;
}
test('apply existing migrations', async () => {
const prevCwd = await setupInitialProjectWithMigrations();
const { 'app.db': _ignore, ...migrations } = await getDatabaseFiles(prevCwd);
const tmp = await testdir({
...symlinkKeystoneDeps,
...migrations,
'keystone.js': basicWithMigrations,
});

await runCommand(tmp, 'build --no-ui');
const output = await startAndStopServer(tmp);
expect(await introspectDb(tmp, dbUrl)).toMatchInlineSnapshot(`
"datasource db {
provider = "sqlite"
url = "file:./app.db"
}

model Todo {
id String @id
title String @default("")
}
"
`);

const { migrationName } = await getGeneratedMigration(tmp, 1, 'init');

expect(
output.replace(new RegExp(migrationName, 'g'), 'migration_name').replace(/\d+ms/g, '0ms')
).toMatchInlineSnapshot(`
"✨ Starting Keystone
✨ Connecting to the database
✨ Applying database migrations
Applying migration \`migration_name\`
✨ Your database is now in sync with your Generated Migrations. Done in 0ms
✨ Creating server
✅ GraphQL API ready
⭐️ Server listening on :3000 (http://localhost:3000/)
"
`);
});
test('logs correctly when no migrations need applied', async () => {
const tmp = await setupInitialProjectWithMigrations();
await runCommand(tmp, 'build --no-ui');
const output = await startAndStopServer(tmp);

expect(output).toMatchInlineSnapshot(`
"✨ Starting Keystone
✨ Connecting to the database
✨ Applying database migrations
✨ The database is already in sync with your Generated Migrations.
✨ Creating server
✅ GraphQL API ready
⭐️ Server listening on :3000 (http://localhost:3000/)
"
`);
});
});