mirror of
https://github.com/withastro/astro.git
synced 2025-02-10 22:38:53 -05:00
cleanup migration commands
This commit is contained in:
parent
d54f47ca11
commit
962c425ceb
4 changed files with 91 additions and 87 deletions
|
@ -1,39 +1,45 @@
|
|||
import type { AstroConfig } from 'astro';
|
||||
import deepDiff from 'deep-diff';
|
||||
import { eq, sql } from 'drizzle-orm';
|
||||
import { readFile, readdir } from 'fs/promises';
|
||||
import { readFile } from 'fs/promises';
|
||||
import type { Arguments } from 'yargs-parser';
|
||||
import { appTokenError } from '../../../errors.js';
|
||||
import {
|
||||
getMigrations,
|
||||
initializeFromMigrations,
|
||||
} from '../../../migrations.js';
|
||||
import {
|
||||
STUDIO_ADMIN_TABLE_ROW_ID,
|
||||
adminTable,
|
||||
createRemoteDatabaseClient,
|
||||
getAstroStudioEnv,
|
||||
getRemoteDatabaseUrl,
|
||||
isAppTokenValid,
|
||||
getAstroStudioEnv
|
||||
} from '../../../utils.js';
|
||||
import { cmd as verifyCommand } from '../verify/index.js';
|
||||
import type { Arguments } from 'yargs-parser';
|
||||
const { diff } = deepDiff;
|
||||
|
||||
async function getMigrations() {
|
||||
const migrationFiles = await readdir('./migrations').catch((err) => {
|
||||
if (err.code === 'ENOENT') {
|
||||
return [];
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
return migrationFiles;
|
||||
}
|
||||
|
||||
export async function cmd({ config, flags }: { config: AstroConfig, flags: Arguments }) {
|
||||
await verifyCommand({ config, flags });
|
||||
|
||||
export async function cmd({ config }: { config: AstroConfig, flags: Arguments }) {
|
||||
const currentSnapshot = JSON.parse(JSON.stringify(config.db?.collections ?? {}));
|
||||
const allMigrationFiles = await getMigrations();
|
||||
if (allMigrationFiles.length === 0) {
|
||||
console.log('Project not yet initialized!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const prevSnapshot = await initializeFromMigrations(allMigrationFiles);
|
||||
const calculatedDiff = diff(prevSnapshot, currentSnapshot);
|
||||
if (calculatedDiff) {
|
||||
console.log('Changes detected!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const remoteDbUrl = getRemoteDatabaseUrl();
|
||||
const appToken = getAstroStudioEnv().ASTRO_STUDIO_APP_TOKEN;
|
||||
if (!appToken || !(await isAppTokenValid({ remoteDbUrl, appToken }))) {
|
||||
if (!appToken) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(appTokenError);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const db = createRemoteDatabaseClient(appToken);
|
||||
|
||||
// get all migrations from the DB
|
||||
|
@ -63,3 +69,4 @@ export async function cmd({ config, flags }: { config: AstroConfig, flags: Argum
|
|||
.set({ collections: JSON.stringify(currentSnapshot) })
|
||||
.where(eq(adminTable.id, STUDIO_ADMIN_TABLE_ROW_ID));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,43 +1,25 @@
|
|||
import type { AstroConfig } from 'astro';
|
||||
import deepDiff from 'deep-diff';
|
||||
import { mkdir, readFile, readdir, writeFile } from 'fs/promises';
|
||||
import { getMigrationQueries } from '../../queries.js';
|
||||
import { writeFile } from 'fs/promises';
|
||||
import type { Arguments } from 'yargs-parser';
|
||||
const { diff, applyChange } = deepDiff;
|
||||
import {
|
||||
getMigrations,
|
||||
initializeFromMigrations,
|
||||
initializeMigrationsDirectory,
|
||||
} from '../../../migrations.js';
|
||||
import { getMigrationQueries } from '../../queries.js';
|
||||
const { diff } = deepDiff;
|
||||
|
||||
async function getMigrations() {
|
||||
const migrationFiles = await readdir('./migrations').catch((err) => {
|
||||
if (err.code === 'ENOENT') {
|
||||
return [];
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
return migrationFiles;
|
||||
}
|
||||
|
||||
async function initialize(currentSnapshot: unknown) {
|
||||
await mkdir('./migrations');
|
||||
await writeFile('./migrations/0000_snapshot.json', JSON.stringify(currentSnapshot, undefined, 2));
|
||||
}
|
||||
|
||||
export async function cmd({ config }: { config: AstroConfig, flags: Arguments }) {
|
||||
export async function cmd({ config }: { config: AstroConfig; flags: Arguments }) {
|
||||
const currentSnapshot = JSON.parse(JSON.stringify(config.db?.collections ?? {}));
|
||||
const allMigrations = await getMigrations();
|
||||
if (allMigrations.length === 0) {
|
||||
await initialize(currentSnapshot);
|
||||
const allMigrationFiles = await getMigrations();
|
||||
if (allMigrationFiles.length === 0) {
|
||||
await initializeMigrationsDirectory(currentSnapshot);
|
||||
console.log('Project initialized!');
|
||||
return;
|
||||
}
|
||||
|
||||
const prevSnapshot = JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8'));
|
||||
for (const migration of allMigrations) {
|
||||
if (migration === '0000_snapshot.json') continue;
|
||||
const migrationContent = JSON.parse(await readFile(`./migrations/${migration}`, 'utf-8'));
|
||||
migrationContent.diff.forEach((change: any) => {
|
||||
applyChange(prevSnapshot, {}, change);
|
||||
});
|
||||
}
|
||||
|
||||
const prevSnapshot = await initializeFromMigrations(allMigrationFiles);
|
||||
const calculatedDiff = diff(prevSnapshot, currentSnapshot);
|
||||
if (!calculatedDiff) {
|
||||
console.log('No changes detected!');
|
||||
|
@ -49,7 +31,7 @@ export async function cmd({ config }: { config: AstroConfig, flags: Arguments })
|
|||
newCollections: currentSnapshot,
|
||||
});
|
||||
|
||||
const largestNumber = allMigrations.reduce((acc, curr) => {
|
||||
const largestNumber = allMigrationFiles.reduce((acc, curr) => {
|
||||
const num = parseInt(curr.split('_')[0]);
|
||||
return num > acc ? num : acc;
|
||||
}, 0);
|
||||
|
|
|
@ -1,50 +1,27 @@
|
|||
import type { AstroConfig } from 'astro';
|
||||
import deepDiff from 'deep-diff';
|
||||
import { mkdir, readFile, readdir, writeFile } from 'fs/promises';
|
||||
import type { Arguments } from 'yargs-parser';
|
||||
import {
|
||||
getMigrations,
|
||||
initializeFromMigrations,
|
||||
initializeMigrationsDirectory,
|
||||
} from '../../../migrations.js';
|
||||
const { diff, applyChange } = deepDiff;
|
||||
|
||||
async function getMigrations() {
|
||||
const migrationFiles = await readdir('./migrations').catch((err) => {
|
||||
if (err.code === 'ENOENT') {
|
||||
return [];
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
return migrationFiles;
|
||||
}
|
||||
async function initialize(currentSnapshot: unknown) {
|
||||
await mkdir('./migrations');
|
||||
await writeFile('./migrations/0000_snapshot.json', JSON.stringify(currentSnapshot, undefined, 2));
|
||||
}
|
||||
|
||||
export async function cmd({ config }: { config: AstroConfig, flags: Arguments }) {
|
||||
export async function cmd({ config }: { config: AstroConfig; flags: Arguments }) {
|
||||
const currentSnapshot = JSON.parse(JSON.stringify(config.db?.collections ?? {}));
|
||||
const allMigrations = await getMigrations();
|
||||
if (allMigrations.length === 0) {
|
||||
await initialize(currentSnapshot);
|
||||
console.log('Project initialized!');
|
||||
return;
|
||||
}
|
||||
|
||||
const prevSnapshot = JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8'));
|
||||
for (const migration of allMigrations) {
|
||||
if (migration === '0000_snapshot.json') continue;
|
||||
const migrationContent = JSON.parse(await readFile(`./migrations/${migration}`, 'utf-8'));
|
||||
migrationContent.diff.forEach((change: any) => {
|
||||
applyChange(prevSnapshot, {}, change);
|
||||
});
|
||||
const allMigrationFiles = await getMigrations();
|
||||
if (allMigrationFiles.length === 0) {
|
||||
console.log('Project not yet initialized!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const prevSnapshot = await initializeFromMigrations(allMigrationFiles);
|
||||
const calculatedDiff = diff(prevSnapshot, currentSnapshot);
|
||||
if (calculatedDiff) {
|
||||
console.log('Changes detected!');
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (calculatedDiff) {
|
||||
console.log('No changes detected!');
|
||||
return;
|
||||
}
|
||||
console.log('No changes detected.');
|
||||
return;
|
||||
}
|
||||
|
|
38
packages/db/src/migrations.ts
Normal file
38
packages/db/src/migrations.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import deepDiff from 'deep-diff';
|
||||
import { mkdir, readFile, readdir, writeFile } from 'fs/promises';
|
||||
const { diff, applyChange } = deepDiff;
|
||||
|
||||
export async function getMigrations(): Promise<string[]> {
|
||||
const migrationFiles = await readdir('./migrations').catch((err) => {
|
||||
if (err.code === 'ENOENT') {
|
||||
return [];
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
return migrationFiles;
|
||||
}
|
||||
|
||||
export async function loadMigration(migration: string): Promise<{ diff: any[]; db: any[] }> {
|
||||
return JSON.parse(await readFile(`./migrations/${migration}`, 'utf-8'));
|
||||
}
|
||||
|
||||
export async function loadInitialSnapshot(): Promise<any> {
|
||||
return JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8'));
|
||||
}
|
||||
|
||||
export async function initializeMigrationsDirectory(currentSnapshot: unknown) {
|
||||
await mkdir('./migrations');
|
||||
await writeFile('./migrations/0000_snapshot.json', JSON.stringify(currentSnapshot, undefined, 2));
|
||||
}
|
||||
|
||||
export async function initializeFromMigrations(allMigrationFiles: string[]) {
|
||||
const prevSnapshot = await loadInitialSnapshot();
|
||||
for (const migration of allMigrationFiles) {
|
||||
if (migration === '0000_snapshot.json') continue;
|
||||
const migrationContent = await loadMigration(migration);
|
||||
migrationContent.diff.forEach((change: any) => {
|
||||
applyChange(prevSnapshot, {}, change);
|
||||
});
|
||||
}
|
||||
return prevSnapshot;
|
||||
}
|
Loading…
Add table
Reference in a new issue