|
1 | | -import {getSelectResults} from "./src/utils"; |
2 | | - |
3 | | -function registerEventIds(limit: number, offset: number): string[] { |
4 | | - // NB: collect all contract execution event IDs with related "register" action attribute. |
5 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
6 | | - // @ts-ignore |
7 | | - const results = getSelectResults(plv8.execute(`SELECT ev.id |
8 | | - FROM app.events ev |
9 | | - JOIN app.event_attributes ea ON ev.id = ea.event_id |
10 | | - WHERE ev.type = 'wasm' |
11 | | - AND ea.key = 'action' |
12 | | - AND ea.value = 'register' |
13 | | - LIMIT ${limit} |
14 | | - OFFSET ${offset}`)); |
15 | | - if (results) { |
16 | | - return results.map(e => e[0]); |
17 | | - } |
18 | | - return null; |
19 | | -} |
20 | | - |
21 | | -function registerEventData(eventIds: string[]): string[][] { |
22 | | - if (eventIds.length == 0) { |
23 | | - return []; |
24 | | - } |
25 | | - |
26 | | - const eventIdValues = eventIds.map(e => `'${e}'`).join(", "); |
27 | | - |
28 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
29 | | - // @ts-ignore |
30 | | - return getSelectResults(plv8.execute(`SELECT ev.id, ev.transaction_id, ev.block_id, ea.key, ea.value |
31 | | - FROM app.events ev |
32 | | - JOIN app.event_attributes ea ON ev.id = ea.event_id |
33 | | - WHERE ev.id in (${eventIdValues})`)); |
34 | | -} |
35 | | - |
36 | | -function insertAgents(agents: Record<string, string>) { |
37 | | - // NB: bulk insert agents |
38 | | - const agentIdValues = Object.values(agents).map(id => `('${id}')`).join(", "); |
39 | | - const insertAgents = `INSERT INTO app.agents (id) |
40 | | - VALUES ${agentIdValues}`; |
41 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
42 | | - // @ts-ignore |
43 | | - plv8.execute(insertAgents); |
44 | | -} |
45 | | - |
46 | | -export function migrationMicroAgentAlmanacRegistrations() { |
47 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
48 | | - // @ts-ignore |
49 | | - plv8.execute("SET SCHEMA 'app'"); |
50 | | - |
51 | | - const batchSize = 500; |
52 | | - for (let i = 0; ; i++) { |
53 | | - const eventIdsBatch = registerEventIds(batchSize, batchSize * i); |
54 | | - if (!eventIdsBatch) { |
55 | | - // NB: no more register events to process. |
56 | | - break; |
57 | | - } |
58 | | - |
59 | | - const registerEvents = registerEventData(eventIdsBatch); |
60 | | - |
61 | | - // NB: organize register event data |
62 | | - const eventIds = {}; |
63 | | - const agents = {}; |
64 | | - const services = {}; |
65 | | - const expiryHeights = {}; |
66 | | - const signatures = {}; |
67 | | - const sequences = {}; |
68 | | - const contracts = {}; |
69 | | - const txIds = {}; |
70 | | - const blockIds = {}; |
71 | | - for (const record of registerEvents) { |
72 | | - if (record.length < 5) { |
73 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
74 | | - // @ts-ignore |
75 | | - plv8.elog(WARNING, `unable to migrate registration event; event ID: ${record[0]}`); |
76 | | - continue; |
77 | | - } |
78 | | - |
79 | | - const [eventId, txId, blockId, key, value] = record; |
80 | | - eventIds[eventId] = null; |
81 | | - |
82 | | - if (!txIds[eventId]) { |
83 | | - txIds[eventId] = txId; |
84 | | - } |
85 | | - |
86 | | - if (!blockIds[eventId]) { |
87 | | - blockIds[eventId] = blockId; |
88 | | - } |
89 | | - |
90 | | - switch (key) { |
91 | | - case "_contract_address": |
92 | | - contracts[eventId] = value; |
93 | | - break; |
94 | | - case "agent_address": |
95 | | - agents[eventId] = value; |
96 | | - break; |
97 | | - case "record": |
98 | | - try { |
99 | | - // NB: replaces all instances of \" with ". |
100 | | - const unescapedValue = value.replace(new RegExp("\\\\\"", "g"), "\""); |
101 | | - const service = JSON.parse(unescapedValue).service; |
102 | | - if (!service) { |
103 | | - throw new Error("expected record to contain service key but none found"); |
104 | | - } |
105 | | - |
106 | | - services[eventId] = JSON.stringify(service); |
107 | | - } catch (error) { |
108 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
109 | | - // @ts-ignore |
110 | | - plv8.elog(WARNING, `unable to parse expected JSON value "${value}": ${error.toString()}`); |
111 | | - continue; |
112 | | - } |
113 | | - break; |
114 | | - case "signature": |
115 | | - signatures[eventId] = value; |
116 | | - break; |
117 | | - case "sequence": |
118 | | - sequences[eventId] = value; |
119 | | - break; |
120 | | - case "expiry_height": |
121 | | - expiryHeights[eventId] = value; |
122 | | - break; |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - insertAgents(agents); |
127 | | - |
128 | | - // NB: bulk insert records |
129 | | - const recordValues = Object.keys(eventIds).map(eventId => { |
130 | | - return "(" + [ |
131 | | - eventId, |
132 | | - services[eventId], |
133 | | - txIds[eventId], |
134 | | - blockIds[eventId], |
135 | | - ].map(e => `'${e}'`).join(", ") + ")"; |
136 | | - }).join(", "); |
137 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
138 | | - // @ts-ignore |
139 | | - plv8.execute(`INSERT INTO app.almanac_records (id, service, transaction_id, block_id) |
140 | | - VALUES ${recordValues}`); |
141 | | - |
142 | | - // NB: bulk insert registrations |
143 | | - const registrationValues = Object.keys(eventIds).map(eventId => { |
144 | | - return "(" + [ |
145 | | - eventId, |
146 | | - expiryHeights[eventId], |
147 | | - signatures[eventId], |
148 | | - sequences[eventId], |
149 | | - agents[eventId], |
150 | | - eventId, |
151 | | - contracts[eventId], |
152 | | - txIds[eventId], |
153 | | - blockIds[eventId], |
154 | | - ].map(e => `'${e}'`).join(", ") + ")"; |
155 | | - }).join(", "); |
156 | | - |
157 | | - const insertRegistrations = `INSERT INTO app.almanac_registrations (id, expiry_height, signature, sequence, agent_id, record_id, |
158 | | - contract_id, transaction_id, block_id) |
159 | | - VALUES ${registrationValues}`; |
160 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
161 | | - // @ts-ignore |
162 | | - plv8.execute(insertRegistrations); |
163 | | - } |
164 | | -} |
| 1 | +// write plv8 based migration here |
| 2 | +// |
| 3 | +// export function migrationSomeChange() { |
| 4 | +// |
| 5 | +// } |
0 commit comments