tetratto/manual_migrations/uploads.js

44 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2025-08-21 00:30:29 -04:00
import postgres from "npm:postgres";
import { parse } from "npm:smol-toml";
const config = parse(await Deno.readTextFile(Deno.cwd() + "/tetratto.toml"), {
integersAsBigInt: true,
});
const db = postgres({
user: config.database.user,
password: config.database.password,
database: config.database.name,
hostname: config.database.url.split(":")[0],
port: config.database.url.split(":")[1],
});
const whats = {};
const alts = {};
for (const row of await db`SELECT * FROM uploads;`) {
whats[row.id] = row.what.replaceAll('"', "");
alts[row.id] = row.alt;
}
await db`ALTER TABLE uploads DROP COLUMN IF EXISTS what;`;
await db`ALTER TABLE uploads DROP COLUMN IF EXISTS alt;`;
await db`ALTER TABLE uploads ADD COLUMN IF NOT EXISTS bucket TEXT NOT NULL DEFAULT '';`;
await db`ALTER TABLE uploads ADD COLUMN IF NOT EXISTS metadata TEXT NOT NULL DEFAULT '';`;
let i = 0;
for (const row of await db`SELECT * FROM uploads;`) {
await db`DELETE FROM uploads WHERE id = ${BigInt(row.id)}`;
await db`INSERT INTO uploads VALUES (${BigInt(row.id)}, ${BigInt(row.created)}, ${BigInt(row.owner)}, DEFAULT, ${JSON.stringify(
{
what: whats[row.id],
alt: whats[row.alt],
},
)});`;
i += 1;
console.log(`done ${i}`);
}
await db.end();