From 3e6d5ce32253f9ff11d7214cefd4b6535674bad2 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 13 Apr 2026 19:11:41 -0400 Subject: [PATCH] user sessions table update --- .../down.sql | 6 +++ .../up.sql | 7 +++ .../user_tables/user_sessions.sql | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 _database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/down.sql create mode 100644 _database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/up.sql create mode 100644 _database/sql/migrations_squashed/user_tables/user_sessions.sql diff --git a/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/down.sql b/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/down.sql new file mode 100644 index 0000000000..d1928f8746 --- /dev/null +++ b/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/down.sql @@ -0,0 +1,6 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE user_sessions + DROP COLUMN maybe_impersonation_user_token; diff --git a/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/up.sql b/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/up.sql new file mode 100644 index 0000000000..e7d9b57160 --- /dev/null +++ b/_database/sql/migrations/2026-04-13-230305-0000_alter_user_sessions_impersonation/up.sql @@ -0,0 +1,7 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE user_sessions + ADD COLUMN maybe_impersonation_user_token VARCHAR(32) DEFAULT NULL + AFTER user_token; diff --git a/_database/sql/migrations_squashed/user_tables/user_sessions.sql b/_database/sql/migrations_squashed/user_tables/user_sessions.sql new file mode 100644 index 0000000000..c9fb14b12b --- /dev/null +++ b/_database/sql/migrations_squashed/user_tables/user_sessions.sql @@ -0,0 +1,50 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +CREATE TABLE user_sessions ( + -- Not used for anything except replication. + id BIGINT(20) NOT NULL AUTO_INCREMENT, + + -- Session entropy + token VARCHAR(32) NOT NULL, + + -- Foreign key to user + user_token VARCHAR(32) NOT NULL, + + -- NEVER IMPERSONATE A USER'S ACCOUNT WITHOUT THEIR CONSENT! + -- + -- For support requests and inquiries, we can have staff log into user accounts + -- to verify and debug possible broken states. The user is always told about + -- this if we are to use this for support cases. + -- + -- Every action performed by impersonated sessions is logged for auditing. + -- + -- If this session is impersonating another user, this is the token of + -- the staff user who initiated the impersonation. + maybe_impersonation_user_token VARCHAR(32) DEFAULT NULL, + + -- Track each session's creation IP. + -- Wide enough for IPv4/6 + ip_address_creation VARCHAR(40) NOT NULL, + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + + -- Session termination time. + -- This must be set by the server code, or the session is invalid + expires_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + + -- deletion = session termination + -- Typically these are destroyed by users, but if in the future we allow mods to + -- delete them, it doesn't really matter who did the deletion: sessions are not + -- designed to be recoverable. + deleted_at TIMESTAMP NULL, + + -- INDICES -- + PRIMARY KEY (id), + UNIQUE KEY (token), + KEY fk_user_token (user_token), + KEY index_ip_address_creation (ip_address_creation) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;