forked from getarcaneapp/arcane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path046_nullable_api_key_user_id.up.sql
More file actions
60 lines (54 loc) · 1.41 KB
/
046_nullable_api_key_user_id.up.sql
File metadata and controls
60 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Environment bootstrap keys are owned by the system, not a user.
-- Allow user_id to be NULL so agent-side key creation doesn't violate the FK constraint.
PRAGMA foreign_keys=OFF;
DROP TABLE IF EXISTS api_keys_new;
CREATE TABLE api_keys_new (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
key_hash TEXT NOT NULL,
key_prefix TEXT NOT NULL,
managed_by TEXT,
user_id TEXT,
environment_id TEXT,
expires_at DATETIME,
last_used_at DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE
);
INSERT INTO api_keys_new (
id,
name,
description,
key_hash,
key_prefix,
managed_by,
user_id,
environment_id,
expires_at,
last_used_at,
created_at,
updated_at
)
SELECT
id,
name,
description,
key_hash,
key_prefix,
managed_by,
user_id,
environment_id,
expires_at,
last_used_at,
created_at,
updated_at
FROM api_keys;
DROP TABLE api_keys;
ALTER TABLE api_keys_new RENAME TO api_keys;
CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON api_keys(key_hash);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
PRAGMA foreign_keys=ON;