mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-09-17 16:39:04 +02:00
feat: migrate one-time and signup tokens to an actor (#1611)
Co-authored-by: Elias Schneider <login@eliasschneider.com>
This commit is contained in:
co-authored by
Elias Schneider
parent
531bb5f0cf
commit
a1b4e1d2b2
@@ -0,0 +1,62 @@
|
||||
PRAGMA foreign_keys=OFF;
|
||||
BEGIN;
|
||||
|
||||
-- Recreate the one_time_access_tokens table with the schema it had before it was dropped.
|
||||
CREATE TABLE one_time_access_tokens
|
||||
(
|
||||
id TEXT PRIMARY KEY,
|
||||
created_at DATETIME NOT NULL,
|
||||
token TEXT NOT NULL UNIQUE,
|
||||
expires_at DATETIME NOT NULL,
|
||||
user_id TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
|
||||
device_token TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_one_time_access_tokens_expires_at ON one_time_access_tokens (expires_at);
|
||||
|
||||
-- Recreate the signup token tables with the schema they had before they were frozen.
|
||||
CREATE TABLE signup_tokens (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
created_at DATETIME NOT NULL,
|
||||
token TEXT NOT NULL UNIQUE,
|
||||
expires_at DATETIME NOT NULL,
|
||||
usage_limit INTEGER NOT NULL DEFAULT 1,
|
||||
usage_count INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE INDEX idx_signup_tokens_token ON signup_tokens(token);
|
||||
CREATE INDEX idx_signup_tokens_expires_at ON signup_tokens(expires_at);
|
||||
|
||||
CREATE TABLE signup_tokens_user_groups
|
||||
(
|
||||
signup_token_id TEXT NOT NULL,
|
||||
user_group_id TEXT NOT NULL,
|
||||
PRIMARY KEY (signup_token_id, user_group_id),
|
||||
FOREIGN KEY (signup_token_id) REFERENCES signup_tokens (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_group_id) REFERENCES user_groups (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Restore the signup tokens from the frozen JSON document stored in the "kv" table.
|
||||
-- json_each expands the JSON array into one row per token object.
|
||||
INSERT INTO signup_tokens (id, created_at, token, expires_at, usage_limit, usage_count)
|
||||
SELECT
|
||||
json_extract(e.value, '$.id'),
|
||||
json_extract(e.value, '$.createdAt'),
|
||||
json_extract(e.value, '$.token'),
|
||||
json_extract(e.value, '$.expiresAt'),
|
||||
json_extract(e.value, '$.usageLimit'),
|
||||
json_extract(e.value, '$.usageCount')
|
||||
FROM kv, json_each(kv."value") AS e
|
||||
WHERE kv."key" = 'signup_tokens_migrated';
|
||||
|
||||
-- Restore the token/user-group associations, expanding each token's nested userGroupIds array.
|
||||
INSERT INTO signup_tokens_user_groups (signup_token_id, user_group_id)
|
||||
SELECT
|
||||
json_extract(e.value, '$.id'),
|
||||
g.value
|
||||
FROM kv, json_each(kv."value") AS e, json_each(json_extract(e.value, '$.userGroupIds')) AS g
|
||||
WHERE kv."key" = 'signup_tokens_migrated';
|
||||
|
||||
-- Remove the frozen signup tokens from the "kv" table.
|
||||
DELETE FROM kv WHERE "key" = 'signup_tokens_migrated';
|
||||
|
||||
COMMIT;
|
||||
PRAGMA foreign_keys=ON;
|
||||
@@ -0,0 +1,35 @@
|
||||
PRAGMA foreign_keys=OFF;
|
||||
BEGIN;
|
||||
|
||||
-- One-time access tokens are now stored in the actor state store, so the table is no longer needed.
|
||||
DROP TABLE IF EXISTS one_time_access_tokens;
|
||||
|
||||
-- Freeze the signup tokens.
|
||||
-- Encode every signup token (with its user group IDs) as a single JSON array and store it in the "kv" table under the "signup_tokens_migrated" key, so the singleton signup token actor can seed its state from it on first startup.
|
||||
-- The "HAVING count(*) > 0" clause ensures nothing is written to the "kv" table when there are no signup tokens.
|
||||
-- Timestamps are stored as Unix seconds, matching how DateTime values are persisted on SQLite.
|
||||
INSERT INTO kv ("key", "value")
|
||||
SELECT 'signup_tokens_migrated', json_group_array(
|
||||
json_object(
|
||||
'id', st.id,
|
||||
'token', st.token,
|
||||
'expiresAt', st.expires_at,
|
||||
'usageLimit', st.usage_limit,
|
||||
'usageCount', st.usage_count,
|
||||
'createdAt', st.created_at,
|
||||
'userGroupIds', json((
|
||||
SELECT COALESCE(json_group_array(stug.user_group_id), json_array())
|
||||
FROM signup_tokens_user_groups stug
|
||||
WHERE stug.signup_token_id = st.id
|
||||
))
|
||||
)
|
||||
)
|
||||
FROM signup_tokens st
|
||||
HAVING count(*) > 0;
|
||||
|
||||
-- Drop the now-frozen signup token tables.
|
||||
DROP TABLE signup_tokens_user_groups;
|
||||
DROP TABLE signup_tokens;
|
||||
|
||||
COMMIT;
|
||||
PRAGMA foreign_keys=ON;
|
||||
Reference in New Issue
Block a user