Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
const { slug, name: tableName } = extractValInfo(import.meta.url);
const migrations = [
`CREATE TABLE IF NOT EXISTS ${tableName} (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
email TEXT UNIQUE
);`,
`ALTER TABLE ${tableName} ADD COLUMN address TEXT;`,
`ALTER TABLE ${tableName} ADD COLUMN city TEXT;`,
];
const storage = await BlobPreset(slug, { version: 0 });
console.log(`Schema Version: ${storage.data.version}`);
if (storage.data.version == migrations.length) {
console.log("Up To Date!");
Deno.exit(0);
}
const statements = migrations.slice(storage.data.version);
console.log(`Running ${statements.length} migrations`);
await sqlite.batch(statements, "write");
storage.data.version = migrations.length;
await storage.write();
console.log("Success!");
December 19, 2023