/**
* Copyright (C) 2026 SofĂa Aritz and contributors
*
* This file is part of Everest.
*
* Everest is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Everest is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Everest. If not, see .
*/
import { Kysely, sql } from "kysely";
import { Migration, Migrator } from "kysely/migration";
import { getDb } from ".";
const migrations: Record = {
"0001": {
async up(db: Kysely) {
await db.schema
.createTable("atproto_auth_state")
.addColumn("key", "text", (col) => col.primaryKey())
.addColumn("value", "text", (col) => col.notNull())
.execute();
await db.schema
.createTable("atproto_auth_session")
.addColumn("key", "text", (col) => col.primaryKey())
.addColumn("value", "text", (col) => col.notNull())
.execute();
},
async down(db: Kysely) {
await db.schema.dropTable("atproto_auth_state").execute();
await db.schema.dropTable("atproto_auth_session").execute();
},
},
"0002": {
async up(db: Kysely) {
await db.schema
.createTable("account")
.addColumn("did", "text", (col) => col.primaryKey())
.addColumn("handle", "text", (col) => col.notNull())
.addColumn("active", "boolean", (col) => col.notNull())
.execute();
await db.schema
.createTable("entry")
.addColumn("uri", "text", (col) => col.primaryKey())
.addColumn("authorDid", "text", (col) => col.notNull())
.addColumn("website", "text", (col) => col.notNull())
.addColumn("note", "text")
.addColumn("tags", sql`text[]`)
.addColumn("createdAt", "timestamptz", (col) => col.notNull())
.addColumn("indexedAt", "timestamptz", (col) => col.notNull())
.execute();
},
async down(db: Kysely) {
await db.schema.dropTable("account").execute();
await db.schema.dropTable("entry").execute();
},
},
"0003": {
async up(db: Kysely) {
await db.schema
.alterTable("account")
.addColumn("email", "text")
.execute();
},
async down(db: Kysely) {
await db.schema
.alterTable("account")
.dropColumn("account")
.execute();
},
},
"0004": {
async up(db: Kysely) {
await db.schema
.alterTable("entry")
.alterColumn("tags", (col) => col.setDefault(sql`'{}'::text[]`))
.execute();
await db.schema
.alterTable("entry")
.alterColumn("tags", (col) => col.setNotNull())
.execute();
},
async down(db: Kysely) {
await db.schema
.alterTable("entry")
.alterColumn("tags", (col) => col.dropNotNull())
.execute();
await db.schema
.alterTable("entry")
.alterColumn("tags", (col) => col.dropDefault())
.execute();
},
},
"0005": {
async up(db: Kysely) {
await db.schema
.alterTable("account")
.addColumn("avatar", "text")
.execute();
},
async down(db: Kysely) {
await db.schema
.alterTable("account")
.dropColumn("avatar")
.execute();
},
},
"0006": {
async up(db: Kysely) {
await db.schema
.createTable("follow")
.addColumn("uri", "text", (col) => col.primaryKey())
.addColumn("follower", "text", (col) => col.notNull())
.addColumn("following", "text", (col) => col.notNull())
.addColumn("createdAt", "timestamptz", (col) => col.notNull())
.addColumn("indexedAt", "timestamptz", (col) => col.notNull())
.execute();
},
async down(db: Kysely) {
await db.schema
.dropTable("follow")
.execute();
},
}
};
export function getMigrator() {
const db = getDb();
return new Migrator({
db,
provider: {
getMigrations: async () => migrations,
},
});
}