0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00

seed on every push

This commit is contained in:
Fred K. Schott 2024-02-09 11:03:08 -08:00
parent 158061c8e2
commit 2a2539fbd2
4 changed files with 19 additions and 45 deletions

View file

@ -22,10 +22,8 @@ import {
import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
export async function cmd({ config, flags }: { config: AstroConfig; flags: Arguments }) {
const isSeedData = flags.seed;
const isDryRun = flags.dryRun;
const appToken = await getManagedAppTokenOrExit(flags.token);
const migration = await getMigrationStatus(config);
if (migration.state === 'no-migrations-found') {
console.log(MIGRATIONS_NOT_INITIALIZED);
@ -44,27 +42,22 @@ export async function cmd({ config, flags }: { config: AstroConfig; flags: Argum
appToken: appToken.token,
});
missingMigrations = data;
} catch (e) {
if (e instanceof Error) {
if (e.message.startsWith('{')) {
const { error: { code } = { code: '' } } = JSON.parse(e.message);
} catch (error) {
if (error instanceof Error) {
if (error.message.startsWith('{')) {
const { error: { code } = { code: '' } } = JSON.parse(error.message);
if (code === 'TOKEN_UNAUTHORIZED') {
console.error(MISSING_SESSION_ID_ERROR);
}
process.exit(1);
}
}
console.error(e);
console.error(error);
process.exit(1);
}
// exit early if there are no migrations to push
// push the database schema
if (missingMigrations.length === 0) {
console.log(MIGRATIONS_UP_TO_DATE);
// Preparing to seed for all runs
// process.exit(0);
}
// push the database schema
if (missingMigrations.length > 0) {
} else {
console.log(`Pushing ${missingMigrations.length} migrations...`);
await pushSchema({
migrations: missingMigrations,
@ -74,10 +67,9 @@ export async function cmd({ config, flags }: { config: AstroConfig; flags: Argum
});
}
// push the database seed data
if (isSeedData) {
console.info('Pushing data...');
await pushData({ config, appToken: appToken.token, isDryRun });
}
console.info('Pushing data...');
await pushData({ config, appToken: appToken.token, isDryRun });
// cleanup and exit
await appToken.destroy();
console.info('Push complete!');
}

View file

@ -54,7 +54,7 @@ export async function getMigrationStatus(config: AstroConfig): Promise<Migration
}
export const MIGRATIONS_CREATED = `${green('■ Migrations initialized!')}\n\n To execute your migrations, run\n ${cyan('astro db push')}`
export const MIGRATIONS_UP_TO_DATE = `${green('■ No migrations needed!')}\n\n Your data is all up to date.\n`
export const MIGRATIONS_UP_TO_DATE = `${green('■ No migrations needed!')}\n\n Your database is up to date.\n`
export const MIGRATIONS_NOT_INITIALIZED = `${yellow('▶ No migrations found!')}\n\n To scaffold your migrations folder, run\n ${cyan('astro db sync')}\n`
export const MIGRATION_NEEDED = `${yellow('▶ Changes detected!')}\n\n To create the necessary migration file, run\n ${cyan('astro db sync')}\n`

View file

@ -27,7 +27,7 @@ export default defineConfig({
db: {
studio: true,
collections: { Author, Themes },
async data({ seed }) {
async data({ seed, mode }) {
await seed(Author, [
{ name: 'Ben' },
{ name: 'Nate' },
@ -35,10 +35,13 @@ export default defineConfig({
{ name: 'Bjorn' },
{ name: 'Sarah' },
]);
await seed(Themes, [
{ name: 'dracula' },
{ name: 'monokai' },
]);
// Seed writable collections in dev mode, only
if (mode === 'dev') {
await seed(Themes, [
{ name: 'dracula' },
{ name: 'monokai' },
]);
}
},
},
});

View file

@ -1,21 +0,0 @@
import { Author, db } from 'astro:db';
const authors: Array<typeof Author.$inferInsert> = [
{
name: 'Ben',
},
{
name: 'Nate',
},
{
name: 'Erika',
},
{
name: 'Bjorn',
},
{
name: 'Sarah',
},
];
await db.insert(Author).values(authors);