22
33DROP TABLE IF EXISTS apm_requests_new;
44
5- -- 1. Create new table with correct schema
6- CREATE TABLE apm_requests_new (
7- id INTEGER PRIMARY KEY AUTOINCREMENT,
8- request_token TEXT NOT NULL ,
9- request_dt TEXT NOT NULL ,
10- request_method TEXT ,
11- request_url TEXT ,
12- total_time REAL ,
13- peak_memory INTEGER ,
14- response_code INTEGER ,
15- response_size INTEGER ,
16- response_build_time REAL ,
17- is_bot INTEGER DEFAULT 0 ,
18- ip TEXT ,
19- user_agent TEXT ,
20- host TEXT ,
21- session_id TEXT
22- );
5+ -- IMPORTANT: The old request_id was TEXT (UUID/token). After migration, all child tables must reference the new INTEGER id from apm_requests.
6+ -- To do this, create a mapping table during migration, then update all child tables to use the new id.
237
24- -- 1.5 Do performance enhancements to insert quickly
25- PRAGMA synchronous = OFF;
26- PRAGMA journal_mode = OFF;
8+ -- 0. Create a mapping table to store old request_id (TEXT) and new id (INTEGER)
9+ CREATE TABLE apm_requests_id_map (
10+ old_request_id TEXT PRIMARY KEY ,
11+ new_id INTEGER
12+ );
2713
14+ -- 1. Create new apm_requests table and insert data
15+ CREATE TABLE apm_requests_new (
16+ id INTEGER PRIMARY KEY AUTOINCREMENT,
17+ request_token TEXT NOT NULL ,
18+ request_dt TEXT NOT NULL ,
19+ request_method TEXT ,
20+ request_url TEXT ,
21+ total_time REAL ,
22+ peak_memory INTEGER ,
23+ response_code INTEGER ,
24+ response_size INTEGER ,
25+ response_build_time REAL ,
26+ is_bot INTEGER DEFAULT 0 ,
27+ ip TEXT ,
28+ user_agent TEXT ,
29+ host TEXT ,
30+ session_id TEXT
31+ );
2832
29- -- 2. Copy data from old table to new table
3033INSERT INTO apm_requests_new (
31- request_token,
32- request_dt,
33- request_method,
34- request_url,
35- total_time,
36- peak_memory,
37- response_code,
38- response_size,
39- response_build_time,
40- is_bot,
41- ip,
42- user_agent,
43- host,
44- session_id
34+ request_token,
35+ request_dt,
36+ request_method,
37+ request_url,
38+ total_time,
39+ peak_memory,
40+ response_code,
41+ response_size,
42+ response_build_time,
43+ is_bot,
44+ ip,
45+ user_agent,
46+ host,
47+ session_id
4548)
4649SELECT
47- request_id,
48- timestamp ,
49- request_method,
50- request_url,
51- total_time,
52- peak_memory,
53- response_code,
54- response_size,
55- response_build_time,
56- is_bot,
57- ip,
58- user_agent,
59- host,
60- session_id
50+ request_id,
51+ timestamp ,
52+ request_method,
53+ request_url,
54+ total_time,
55+ peak_memory,
56+ response_code,
57+ response_size,
58+ response_build_time,
59+ is_bot,
60+ ip,
61+ user_agent,
62+ host,
63+ session_id
6164FROM apm_requests;
6265
63- -- 3. Drop old table
64- DROP TABLE apm_requests;
66+ -- 2. Populate mapping table
67+ INSERT INTO apm_requests_id_map (old_request_id, new_id)
68+ SELECT request_token, id FROM apm_requests_new;
6569
66- -- 4. Rename new table to original name
70+ -- 3. Drop old table and rename new table
71+ DROP TABLE apm_requests;
6772ALTER TABLE apm_requests_new RENAME TO apm_requests;
6873
69- -- 5. Create index on request_id (now token)
70- CREATE INDEX idx_apm_requests_request_id ON apm_requests(request_token);
71- CREATE INDEX IF NOT EXISTS idx_apm_requests_request_dt ON apm_requests(request_dt);
72- CREATE INDEX IF NOT EXISTS idx_apm_requests_url ON apm_requests(request_url);
73- CREATE INDEX IF NOT EXISTS idx_apm_requests_response_code ON apm_requests(response_code);
74- CREATE INDEX IF NOT EXISTS idx_apm_requests_composite ON apm_requests(request_dt, response_code, request_method);
75- CREATE INDEX IF NOT EXISTS idx_apm_requests_ip ON apm_requests(ip);
76- CREATE INDEX IF NOT EXISTS idx_apm_requests_host ON apm_requests(host);
77- CREATE INDEX IF NOT EXISTS idx_apm_requests_session_id ON apm_requests(session_id);
78- -- Index on user_agent can help with bot filtering
79- CREATE INDEX IF NOT EXISTS idx_apm_requests_user_agent ON apm_requests(user_agent);
74+ -- 4. Update all child tables: Convert request_id from old TEXT token to new INTEGER id using the mapping table
75+ -- This must be done before migrating child tables to INTEGER request_id
76+
77+ -- apm_custom_events
78+ UPDATE apm_custom_events SET request_id = (
79+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_custom_events .request_id
80+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
81+
82+ -- apm_routes
83+ UPDATE apm_routes SET request_id = (
84+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_routes .request_id
85+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
86+
87+ -- apm_middleware
88+ UPDATE apm_middleware SET request_id = (
89+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_middleware .request_id
90+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
91+
92+ -- apm_views
93+ UPDATE apm_views SET request_id = (
94+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_views .request_id
95+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
96+
97+ -- apm_db_connections
98+ UPDATE apm_db_connections SET request_id = (
99+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_db_connections .request_id
100+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
80101
102+ -- apm_db_queries
103+ UPDATE apm_db_queries SET request_id = (
104+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_db_queries .request_id
105+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
81106
107+ -- apm_errors
108+ UPDATE apm_errors SET request_id = (
109+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_errors .request_id
110+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
111+
112+ -- apm_cache
113+ UPDATE apm_cache SET request_id = (
114+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_cache .request_id
115+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
116+
117+ -- apm_raw_metrics
118+ UPDATE apm_raw_metrics SET request_id = (
119+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_raw_metrics .request_id
120+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
121+
122+ -- apm_custom_event_data
123+ UPDATE apm_custom_event_data SET request_id = (
124+ SELECT new_id FROM apm_requests_id_map WHERE old_request_id = apm_custom_event_data .request_id
125+ ) WHERE request_id IN (SELECT old_request_id FROM apm_requests_id_map);
126+
127+ -- Now proceed with child table migrations
82128
83129-- And now we need to fix custom requests
84130CREATE TABLE IF NOT EXISTS apm_custom_events (
@@ -272,7 +318,8 @@ CREATE INDEX IF NOT EXISTS idx_apm_cache_hit ON apm_cache(hit);
272318-- Raw metrics table migration
273319--
274320CREATE TABLE apm_raw_metrics_new (
275- request_id INTEGER PRIMARY KEY ,
321+ id INTEGER PRIMARY KEY AUTOINCREMENT,
322+ request_id INTEGER NOT NULL ,
276323 metrics_json TEXT NOT NULL ,
277324 FOREIGN KEY (request_id) REFERENCES apm_requests(id) ON DELETE CASCADE
278325);
@@ -282,6 +329,7 @@ INSERT INTO apm_raw_metrics_new (
282329SELECT request_id, metrics_json FROM apm_raw_metrics;
283330DROP TABLE apm_raw_metrics;
284331ALTER TABLE apm_raw_metrics_new RENAME TO apm_raw_metrics;
332+ CREATE INDEX IF NOT EXISTS idx_apm_raw_metrics_request_id ON apm_raw_metrics(request_id);
285333
286334-- Migration for apm_custom_event_data table to use INTEGER request_id
287335CREATE TABLE apm_custom_event_data_new (
@@ -303,5 +351,6 @@ CREATE INDEX IF NOT EXISTS idx_apm_custom_event_data_event_id ON apm_custom_even
303351CREATE INDEX IF NOT EXISTS idx_apm_custom_event_data_request_id ON apm_custom_event_data(request_id);
304352CREATE INDEX IF NOT EXISTS idx_apm_custom_event_data_key ON apm_custom_event_data(json_key);
305353
354+ DROP TABLE apm_requests_id_map;
306355
307356VACUUM;
0 commit comments