A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here. An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk. 298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
5.1 KiB
TypeScript
114 lines
5.1 KiB
TypeScript
/**
|
|
* Brings the database up to date with the migrations under migrations/, and
|
|
* records each one it applies in `schema_migrations`. connection.ts runs it
|
|
* each time the server opens the database.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { DatabaseSync } from "node:sqlite";
|
|
import { up as up0001CommentLikes } from "./migrations/0001_comment_likes.ts";
|
|
import { up as up0002DumpThumbnail } from "./migrations/0002_dump_thumbnail.ts";
|
|
import { up as up0003DumpBacklinks } from "./migrations/0003_dump_backlinks.ts";
|
|
import { up as up0004UserRoles } from "./migrations/0004_user_roles.ts";
|
|
import { up as up0005DropIsAdmin } from "./migrations/0005_drop_is_admin.ts";
|
|
import { up as up0006Categories } from "./migrations/0006_categories.ts";
|
|
import { up as up0007PasswordResetTokens } from "./migrations/0007_password_reset_tokens.ts";
|
|
import { up as up0008ChatMessages } from "./migrations/0008_chat_messages.ts";
|
|
import { up as up0009ChatReply } from "./migrations/0009_chat_reply.ts";
|
|
import { up as up0010YoutubeEmbedStart } from "./migrations/0010_youtube_embed_start.ts";
|
|
import { up as up0011SplitFaviconThumbnail } from "./migrations/0011_split_favicon_thumbnail.ts";
|
|
import { up as up0012FixFaviconReclassification } from "./migrations/0012_fix_favicon_reclassification.ts";
|
|
import { up as up0013DumpUrlCanonical } from "./migrations/0013_dump_url_canonical.ts";
|
|
import { up as up0014ChatReadState } from "./migrations/0014_chat_read_state.ts";
|
|
import { up as up0015TokenRevocation } from "./migrations/0015_token_revocation.ts";
|
|
import { up as up0016PerformanceIndexes } from "./migrations/0016_performance_indexes.ts";
|
|
import { up as up0017CaseInsensitiveUsers } from "./migrations/0017_case_insensitive_users.ts";
|
|
import { up as up0018AttachmentResourceType } from "./migrations/0018_attachment_resource_type.ts";
|
|
import { up as up0019UserSettings } from "./migrations/0019_user_settings.ts";
|
|
import { up as up0020DigestSchedule } from "./migrations/0020_digest_schedule.ts";
|
|
import { up as up0021PageUrls } from "./migrations/0021_page_urls.ts";
|
|
import { up as up0022ReserveCommunity } from "./migrations/0022_reserve_community.ts";
|
|
import { up as up0023MentionEveryone } from "./migrations/0023_mention_everyone.ts";
|
|
import { up as up0024Federation } from "./migrations/0024_federation.ts";
|
|
|
|
interface Migration {
|
|
name: string;
|
|
up: (db: DatabaseSync) => void;
|
|
}
|
|
|
|
/**
|
|
* Lists every migration, in the order they run. A new one goes at the end,
|
|
* along with the same change to schema.sql, and a shipped one is never edited
|
|
* or moved. A database that connection.ts made from schema.sql records none of
|
|
* them, so each `up` must find its change already made and leave it be.
|
|
*/
|
|
const MIGRATIONS: Migration[] = [
|
|
{ name: "0001_comment_likes", up: up0001CommentLikes },
|
|
{ name: "0002_dump_thumbnail", up: up0002DumpThumbnail },
|
|
{ name: "0003_dump_backlinks", up: up0003DumpBacklinks },
|
|
{ name: "0004_user_roles", up: up0004UserRoles },
|
|
{ name: "0005_drop_is_admin", up: up0005DropIsAdmin },
|
|
{ name: "0006_categories", up: up0006Categories },
|
|
{ name: "0007_password_reset_tokens", up: up0007PasswordResetTokens },
|
|
{ name: "0008_chat_messages", up: up0008ChatMessages },
|
|
{ name: "0009_chat_reply", up: up0009ChatReply },
|
|
{ name: "0010_youtube_embed_start", up: up0010YoutubeEmbedStart },
|
|
{ name: "0011_split_favicon_thumbnail", up: up0011SplitFaviconThumbnail },
|
|
{
|
|
name: "0012_fix_favicon_reclassification",
|
|
up: up0012FixFaviconReclassification,
|
|
},
|
|
{ name: "0013_dump_url_canonical", up: up0013DumpUrlCanonical },
|
|
{ name: "0014_chat_read_state", up: up0014ChatReadState },
|
|
{ name: "0015_token_revocation", up: up0015TokenRevocation },
|
|
{ name: "0016_performance_indexes", up: up0016PerformanceIndexes },
|
|
{ name: "0017_case_insensitive_users", up: up0017CaseInsensitiveUsers },
|
|
{
|
|
name: "0018_attachment_resource_type",
|
|
up: up0018AttachmentResourceType,
|
|
},
|
|
{ name: "0019_user_settings", up: up0019UserSettings },
|
|
{ name: "0020_digest_schedule", up: up0020DigestSchedule },
|
|
{ name: "0021_page_urls", up: up0021PageUrls },
|
|
{ name: "0022_reserve_community", up: up0022ReserveCommunity },
|
|
{ name: "0023_mention_everyone", up: up0023MentionEveryone },
|
|
{ name: "0024_federation", up: up0024Federation },
|
|
];
|
|
|
|
/**
|
|
* Applies each migration `db` hasn't recorded yet, in order, each in its own
|
|
* transaction.
|
|
*
|
|
* @throws {Error} When a migration fails. That one is rolled back, the later
|
|
* ones don't run, and the server doesn't start.
|
|
*/
|
|
export function runMigrations(db: DatabaseSync): void {
|
|
db.exec(`CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
name TEXT PRIMARY KEY,
|
|
applied_at TEXT NOT NULL
|
|
);`);
|
|
|
|
const applied = new Set(
|
|
(db.prepare(`SELECT name FROM schema_migrations;`).all() as {
|
|
name: string;
|
|
}[]).map((r) => r.name),
|
|
);
|
|
|
|
for (const { name, up } of MIGRATIONS) {
|
|
if (applied.has(name)) continue;
|
|
db.exec("BEGIN;");
|
|
try {
|
|
up(db);
|
|
db.prepare(
|
|
`INSERT INTO schema_migrations (name, applied_at) VALUES (?, ?);`,
|
|
).run(name, new Date().toISOString());
|
|
db.exec("COMMIT;");
|
|
console.log(`[migrate] applied ${name}`);
|
|
} catch (err) {
|
|
db.exec("ROLLBACK;");
|
|
throw err;
|
|
}
|
|
}
|
|
}
|