fediglam/src/main/migrations.zig

193 lines
5.5 KiB
Zig
Raw Normal View History

2022-09-15 01:12:07 +00:00
const std = @import("std");
2022-07-30 07:26:35 +00:00
const sql = @import("sql");
const util = @import("util");
const DateTime = util.DateTime;
2022-07-30 07:26:35 +00:00
pub const Migration = struct {
2022-09-15 01:12:07 +00:00
name: [:0]const u8,
2022-07-30 07:26:35 +00:00
up: []const u8,
down: []const u8,
};
2022-10-04 05:41:22 +00:00
fn execStmt(tx: anytype, stmt: []const u8, alloc: std.mem.Allocator) !void {
2022-09-15 01:12:07 +00:00
const stmt_null = try std.cstr.addNullByte(alloc, stmt);
defer alloc.free(stmt_null);
2022-10-02 05:18:24 +00:00
try tx.exec(stmt_null, {}, null);
2022-07-30 07:26:35 +00:00
}
2022-10-04 05:41:22 +00:00
fn execScript(db: anytype, script: []const u8, alloc: std.mem.Allocator) !void {
2022-10-13 06:19:59 +00:00
const tx = try db.beginOrSavepoint();
errdefer tx.rollback();
var iter = util.SqlStmtIter.from(script);
while (iter.next()) |stmt| {
2022-10-13 06:19:59 +00:00
try execStmt(tx, stmt, alloc);
2022-07-30 07:26:35 +00:00
}
2022-10-13 06:19:59 +00:00
try tx.commitOrRelease();
2022-07-30 07:26:35 +00:00
}
2022-09-25 08:10:30 +00:00
fn wasMigrationRan(db: anytype, name: []const u8, alloc: std.mem.Allocator) !bool {
2022-10-04 02:41:59 +00:00
return if (db.queryRow(
std.meta.Tuple(&.{i32}),
"SELECT COUNT(*) FROM migration WHERE name = $1 LIMIT 1",
.{name},
alloc,
)) |row| row[0] != 0 else |err| switch (err) {
error.NoRows => false,
else => error.DatabaseFailure,
};
2022-07-30 07:26:35 +00:00
}
2022-10-04 05:41:22 +00:00
pub fn up(db: anytype) !void {
2022-09-15 01:12:07 +00:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
std.log.info("Running migrations...", .{});
try execScript(db, create_migration_table, gpa.allocator());
2022-07-30 07:26:35 +00:00
for (migrations) |migration| {
const tx = try db.begin();
errdefer tx.rollback();
const was_ran = try wasMigrationRan(tx, migration.name, gpa.allocator());
2022-09-15 01:12:07 +00:00
if (!was_ran) {
std.log.info("Running migration {s}", .{migration.name});
try execScript(tx, migration.up, gpa.allocator());
try tx.insert("migration", .{
.name = migration.name,
.applied_at = DateTime.now(),
}, gpa.allocator());
2022-07-30 07:26:35 +00:00
}
try tx.commit();
2022-07-30 07:26:35 +00:00
}
}
const create_migration_table =
\\CREATE TABLE IF NOT EXISTS
\\migration(
\\ name TEXT NOT NULL PRIMARY KEY,
\\ applied_at TIMESTAMPTZ NOT NULL
2022-09-05 09:15:16 +00:00
\\);
2022-07-30 07:26:35 +00:00
;
// NOTE: Until the first public release, i may collapse multiple
// migrations into a single one. this will require db recreation
const migrations: []const Migration = &.{
.{
.name = "accounts and actors",
2022-07-30 07:26:35 +00:00
.up =
\\CREATE TABLE actor(
2022-10-04 02:41:59 +00:00
\\ id UUID NOT NULL PRIMARY KEY,
2022-07-30 07:26:35 +00:00
\\ username TEXT NOT NULL,
\\
\\ created_at TIMESTAMPTZ NOT NULL
2022-09-05 08:52:49 +00:00
\\);
2022-07-30 07:26:35 +00:00
\\
\\CREATE TABLE account(
\\ id UUID NOT NULL PRIMARY KEY REFERENCES actor(id),
2022-09-07 23:14:52 +00:00
\\
\\ kind TEXT NOT NULL CHECK (kind IN ('admin', 'user')),
2022-09-07 23:14:52 +00:00
\\ email TEXT
2022-09-05 08:52:49 +00:00
\\);
2022-07-30 07:26:35 +00:00
\\
2022-10-04 02:41:59 +00:00
\\CREATE TABLE password(
\\ account_id UUID NOT NULL PRIMARY KEY REFERENCES account(id),
2022-07-30 07:26:35 +00:00
\\
\\ hash BLOB NOT NULL,
\\ changed_at TIMESTAMPTZ NOT NULL
2022-09-05 08:52:49 +00:00
\\);
2022-07-30 07:26:35 +00:00
,
.down =
2022-10-04 02:41:59 +00:00
\\DROP TABLE password;
\\DROP TABLE account;
\\DROP TABLE actor;
2022-07-30 07:26:35 +00:00
,
},
.{
.name = "notes",
.up =
\\CREATE TABLE note(
2022-10-04 02:41:59 +00:00
\\ id UUID NOT NULL,
2022-07-30 07:26:35 +00:00
\\
\\ content TEXT NOT NULL,
\\ author_id UUID NOT NULL REFERENCES actor(id),
2022-07-30 07:26:35 +00:00
\\
\\ created_at TIMESTAMPTZ NOT NULL
2022-09-07 23:14:52 +00:00
\\);
2022-07-30 07:26:35 +00:00
,
.down = "DROP TABLE note;",
},
.{
.name = "note reactions",
.up =
\\CREATE TABLE reaction(
2022-10-04 02:41:59 +00:00
\\ id UUID NOT NULL PRIMARY KEY,
2022-07-30 07:26:35 +00:00
\\
\\ author_id UUID NOT NULL REFERENCES actor(id),
2022-10-04 02:41:59 +00:00
\\ note_id UUID NOT NULL REFERENCES note(id),
2022-07-30 07:26:35 +00:00
\\
\\ created_at TIMESTAMPTZ NOT NULL
2022-09-07 23:14:52 +00:00
\\);
2022-07-30 07:26:35 +00:00
,
.down = "DROP TABLE reaction;",
},
.{
2022-10-04 02:41:59 +00:00
.name = "account tokens",
2022-07-30 07:26:35 +00:00
.up =
\\CREATE TABLE token(
2022-09-07 23:14:52 +00:00
\\ hash TEXT NOT NULL PRIMARY KEY,
\\ account_id UUID NOT NULL REFERENCES account(id),
2022-07-30 07:26:35 +00:00
\\
\\ issued_at TIMESTAMPTZ NOT NULL
2022-09-07 23:14:52 +00:00
\\);
2022-07-30 07:26:35 +00:00
,
.down = "DROP TABLE token;",
},
.{
2022-10-04 02:41:59 +00:00
.name = "account invites",
2022-07-30 07:26:35 +00:00
.up =
\\CREATE TABLE invite(
2022-10-04 02:41:59 +00:00
\\ id UUID NOT NULL PRIMARY KEY,
2022-07-30 07:26:35 +00:00
\\
\\ name TEXT NOT NULL,
2022-09-08 06:56:29 +00:00
\\ code TEXT NOT NULL UNIQUE,
\\ created_by UUID NOT NULL REFERENCES account(id),
2022-07-30 07:26:35 +00:00
\\
\\ max_uses INTEGER,
\\
\\ created_at TIMESTAMPTZ NOT NULL,
2022-09-15 01:12:07 +00:00
\\ expires_at TIMESTAMPTZ,
2022-09-08 06:56:29 +00:00
\\
2022-10-04 02:41:59 +00:00
\\ kind TEXT NOT NULL CHECK (kind in ('system_user', 'community_owner', 'user'))
2022-09-07 23:14:52 +00:00
\\);
\\ALTER TABLE account ADD COLUMN invite_id UUID REFERENCES invite(id);
2022-07-30 07:26:35 +00:00
,
.down =
\\ALTER TABLE account DROP COLUMN invite_id;
2022-07-30 07:26:35 +00:00
\\DROP TABLE invite;
,
},
2022-08-02 04:33:23 +00:00
.{
.name = "communities",
.up =
\\CREATE TABLE community(
2022-10-04 02:41:59 +00:00
\\ id UUID NOT NULL PRIMARY KEY,
2022-08-02 04:33:23 +00:00
\\
2022-10-04 02:41:59 +00:00
\\ owner_id UUID REFERENCES account(id),
2022-08-02 04:33:23 +00:00
\\ name TEXT NOT NULL,
\\ host TEXT NOT NULL UNIQUE,
\\ scheme TEXT NOT NULL CHECK (scheme IN ('http', 'https')),
2022-09-29 21:52:01 +00:00
\\ kind TEXT NOT NULL CHECK (kind in ('admin', 'local')),
2022-08-02 04:33:23 +00:00
\\
\\ created_at TIMESTAMPTZ NOT NULL
2022-09-05 09:15:16 +00:00
\\);
\\ALTER TABLE actor ADD COLUMN community_id UUID REFERENCES community(id);
2022-10-04 02:41:59 +00:00
\\ALTER TABLE invite ADD COLUMN community_id UUID REFERENCES community(id);
2022-08-02 04:33:23 +00:00
,
.down =
2022-09-29 21:52:01 +00:00
\\ALTER TABLE invite DROP COLUMN community_id;
\\ALTER TABLE actor DROP COLUMN community_id;
2022-08-02 04:33:23 +00:00
\\DROP TABLE community;
,
},
2022-07-30 07:26:35 +00:00
};