@@ -17,6 +17,7 @@ export async function runClickhouseMigrations() {
1717 await client . exec ( { query : USERS_TABLE_BASE_SQL } ) ;
1818 await client . exec ( { query : USERS_VIEW_SQL } ) ;
1919 await client . exec ( { query : TOKEN_REFRESH_EVENT_ROW_FORMAT_MUTATION_SQL } ) ;
20+ await client . exec ( { query : SIGN_UP_RULE_TRIGGER_EVENT_ROW_FORMAT_MUTATION_SQL } ) ;
2021 const queries = [
2122 "REVOKE ALL PRIVILEGES ON *.* FROM limited_user;" ,
2223 "REVOKE ALL FROM limited_user;" ,
@@ -93,6 +94,27 @@ WHERE event_type = '$token-refresh'
9394 AND JSONHas(toJSONString(data), 'refreshTokenId');
9495` ;
9596
97+ // Normalizes legacy $sign-up-rule-trigger rows (camelCase JSON) to the new format:
98+ // - Row identity stays in columns (project_id/branch_id)
99+ // - data JSON becomes { project_id, branch_id, rule_id, action, email, auth_method, oauth_provider } (snake_case)
100+ const SIGN_UP_RULE_TRIGGER_EVENT_ROW_FORMAT_MUTATION_SQL = `
101+ ALTER TABLE analytics_internal.events
102+ UPDATE
103+ data = CAST(concat(
104+ '{',
105+ '"project_id":', toJSONString(JSONExtractString(toJSONString(data), 'projectId')), ',',
106+ '"branch_id":', toJSONString(JSONExtractString(toJSONString(data), 'branchId')), ',',
107+ '"rule_id":', toJSONString(JSONExtractString(toJSONString(data), 'ruleId')), ',',
108+ '"action":', toJSONString(JSONExtractString(toJSONString(data), 'action')), ',',
109+ '"email":', toJSONString(JSONExtract(toJSONString(data), 'email', 'Nullable(String)')), ',',
110+ '"auth_method":', toJSONString(JSONExtract(toJSONString(data), 'authMethod', 'Nullable(String)')), ',',
111+ '"oauth_provider":', toJSONString(JSONExtract(toJSONString(data), 'oauthProvider', 'Nullable(String)')),
112+ '}'
113+ ) AS JSON)
114+ WHERE event_type = '$sign-up-rule-trigger'
115+ AND JSONHas(toJSONString(data), 'ruleId');
116+ ` ;
117+
96118const USERS_TABLE_BASE_SQL = `
97119CREATE TABLE IF NOT EXISTS analytics_internal.users (
98120 project_id String,
@@ -103,9 +125,9 @@ CREATE TABLE IF NOT EXISTS analytics_internal.users (
103125 primary_email Nullable(String),
104126 primary_email_verified UInt8,
105127 signed_up_at DateTime64(3, 'UTC'),
106- client_metadata JSON ,
107- client_read_only_metadata JSON ,
108- server_metadata JSON ,
128+ client_metadata String ,
129+ client_read_only_metadata String ,
130+ server_metadata String ,
109131 is_anonymous UInt8,
110132 restricted_by_admin UInt8,
111133 restricted_by_admin_reason Nullable(String),
0 commit comments