Skip to content

Commit

Permalink
fix: change typ to 'local' | 'remote'
Browse files Browse the repository at this point in the history
  • Loading branch information
OXeu committed Jul 12, 2024
1 parent e843042 commit 57ba301
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
10 changes: 5 additions & 5 deletions scripts/dev-migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const SQL_DIR = path.join(__dirname, '..', 'server', 'sql');

// Change to the server/sql directory
process.chdir(SQL_DIR);

const migrationVersion = await getMigrationVersion(true, DB_NAME);
const isInfoExistResult = await isInfoExist(true, DB_NAME);
const typ = 'local';
const migrationVersion = await getMigrationVersion(typ, DB_NAME);
const isInfoExistResult = await isInfoExist(typ, DB_NAME);
// List all SQL files and sort them
const sqlFiles = fs
.readdirSync(SQL_DIR, { withFileTypes: true })
Expand Down Expand Up @@ -43,11 +43,11 @@ if (sqlFiles.length === 0) {
const lastVersion = parseInt(sqlFiles[sqlFiles.length - 1].split('-')[0]);
if (lastVersion > migrationVersion) {
// Update the migration version
await updateMigrationVersion(true, DB_NAME, lastVersion);
await updateMigrationVersion(typ, DB_NAME, lastVersion);
}
}

await fixTopField(true, DB_NAME, isInfoExistResult);
await fixTopField(typ, DB_NAME, isInfoExistResult);

// Back to the root directory (optional, as the script ends)
process.chdir(__dirname);
16 changes: 6 additions & 10 deletions scripts/fix-top-field.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { $ } from "bun"

export async function fixTopField(isLocal: boolean, db: string, isInfoExistResult: boolean) {
const typ = isLocal ? "local" : "remote"
export async function fixTopField(typ: 'local' | 'remote', db: string, isInfoExistResult: boolean) {
if (!isInfoExistResult) {
console.log("Legacy database, check top field")
const result = await $`bunx wrangler d1 execute ${db} --${typ} --json --command "SELECT name FROM pragma_table_info('feeds') WHERE name='top'"`.quiet().json()
Expand All @@ -16,8 +15,7 @@ export async function fixTopField(isLocal: boolean, db: string, isInfoExistResul
}
}

export async function isInfoExist(isLocal: boolean, db: string) {
const typ = isLocal ? "local" : "remote"
export async function isInfoExist(typ: 'local' | 'remote', db: string) {
const result = await $`bunx wrangler d1 execute ${db} --${typ} --json --command "SELECT name FROM sqlite_master WHERE type='table' AND name='info'"`.quiet().json()
if (result[0].results.length === 0) {
console.log("info table not exists")
Expand All @@ -28,13 +26,12 @@ export async function isInfoExist(isLocal: boolean, db: string) {
}
}

export async function getMigrationVersion(isLocal: boolean, db: string) {
const isInfoExistResult = await isInfoExist(isLocal, db)
export async function getMigrationVersion(typ: 'local' | 'remote', db: string) {
const isInfoExistResult = await isInfoExist(typ, db)
if (!isInfoExistResult) {
console.log("Legacy database, migration_version not exists")
return -1
}
const typ = isLocal ? "local" : "remote"
const result = await $`bunx wrangler d1 execute ${db} --${typ} --json --command "SELECT value FROM info WHERE key='migration_version'"`.quiet().json()
if (result[0].results.length === 0) {
console.log("migration_version not exists")
Expand All @@ -45,9 +42,8 @@ export async function getMigrationVersion(isLocal: boolean, db: string) {
}
}

export async function updateMigrationVersion(isLocal: boolean, db: string, version: number) {
const typ = isLocal ? "local" : "remote"
const exists = await isInfoExist(isLocal, db)
export async function updateMigrationVersion(typ: 'local' | 'remote', db: string, version: number) {
const exists = await isInfoExist(typ, db)
if (!exists) {
console.log("info table not exists, skip update migration_version")
throw new Error("info table not exists")
Expand Down
9 changes: 5 additions & 4 deletions scripts/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ if (existing) {

console.log(`----------------------------`)
console.log(`Migrating D1 "${DB_NAME}"`)
const migrationVersion = await getMigrationVersion(true, DB_NAME);
const isInfoExistResult = await isInfoExist(true, DB_NAME);
const typ = 'remote';
const migrationVersion = await getMigrationVersion(typ, DB_NAME);
const isInfoExistResult = await isInfoExist(typ, DB_NAME);

try {
const files = await readdir("./server/sql", { recursive: false })
Expand All @@ -127,7 +128,7 @@ try {
const lastVersion = parseInt(sqlFiles[sqlFiles.length - 1].split('-')[0]);
if (lastVersion > migrationVersion) {
// Update the migration version
await updateMigrationVersion(false, DB_NAME, lastVersion);
await updateMigrationVersion(typ, DB_NAME, lastVersion);
}
}
} catch (e: any) {
Expand All @@ -140,7 +141,7 @@ try {
console.log(`Migrated D1 "${DB_NAME}"`)
console.log(`----------------------------`)
console.log(`Patch D1`)
await fixTopField(true, DB_NAME, isInfoExistResult);
await fixTopField(typ, DB_NAME, isInfoExistResult);
console.log(`----------------------------`)
console.log(`Put secrets`)

Expand Down

0 comments on commit 57ba301

Please sign in to comment.