Files
stats/packages/db/code-migrations/migrate.ts
Carl-Gerhard Lindesvärd 81a7e5d62e feat: dashboard v2, esm, upgrades (#211)
* esm

* wip

* wip

* wip

* wip

* wip

* wip

* subscription notice

* wip

* wip

* wip

* fix envs

* fix: update docker build

* fix

* esm/types

* delete dashboard :D

* add patches to dockerfiles

* update packages + catalogs + ts

* wip

* remove native libs

* ts

* improvements

* fix redirects and fetching session

* try fix favicon

* fixes

* fix

* order and resize reportds within a dashboard

* improvements

* wip

* added userjot to dashboard

* fix

* add op

* wip

* different cache key

* improve date picker

* fix table

* event details loading

* redo onboarding completely

* fix login

* fix

* fix

* extend session, billing and improve bars

* fix

* reduce price on 10M
2025-10-16 12:27:44 +02:00

97 lines
2.5 KiB
TypeScript

import fs from 'node:fs';
import path from 'node:path';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { db } from '../index';
import { getIsDry, getIsSelfHosting, printBoxMessage } from './helpers';
async function migrate() {
const args = process.argv.slice(2);
const migration = args.filter((arg) => !arg.startsWith('--'))[0];
const migrationsDir = path.join(__dirname, '..', 'code-migrations');
const migrations = fs.readdirSync(migrationsDir).filter((file) => {
const version = file.split('-')[0];
return (
!Number.isNaN(Number.parseInt(version ?? '')) && file.endsWith('.ts')
);
});
const finishedMigrations = await db.codeMigration.findMany();
printBoxMessage('📋 Plan', [
'\t✅ Finished:',
...finishedMigrations.map(
(migration) => `\t- ${migration.name} (${migration.createdAt})`,
),
'',
'\t🔄 Will run now:',
...migrations
.filter(
(migration) =>
!finishedMigrations.some(
(finishedMigration) => finishedMigration.name === migration,
),
)
.map((migration) => `\t- ${migration}`),
]);
printBoxMessage('🌍 Environment', [
`POSTGRES: ${process.env.DATABASE_URL}`,
`CLICKHOUSE: ${process.env.CLICKHOUSE_URL}`,
]);
if (!getIsSelfHosting()) {
printBoxMessage('🕒 Migrations starts in 10 seconds', []);
await new Promise((resolve) => setTimeout(resolve, 10000));
}
if (migration) {
await runMigration(migrationsDir, migration);
} else {
for (const file of migrations) {
if (finishedMigrations.some((migration) => migration.name === file)) {
printBoxMessage('✅ Already Migrated ✅', [`${file}`]);
continue;
}
await runMigration(migrationsDir, file);
}
}
console.log('Migrations finished');
process.exit(0);
}
async function runMigration(migrationsDir: string, file: string) {
printBoxMessage('⚡️ Running Migration ⚡️ ', [`${file}`]);
try {
const migration = await import(path.join(migrationsDir, file));
await migration.up();
if (!getIsDry()) {
await db.codeMigration.upsert({
where: {
name: file,
},
update: {
name: file,
},
create: {
name: file,
},
});
}
} catch (error) {
printBoxMessage('❌ Migration Failed ❌', [
`Error running migration ${file}:`,
error,
]);
process.exit(1);
}
}
migrate();