|
| 1 | +--! Previous: sha1:b16de8bb2e28541b6b0444a5fb2ecc79874e5cec |
| 2 | +--! Hash: sha1:ccf7c5f4ff4288d6857fa79e9235617d08ba5ad5 |
| 3 | + |
| 4 | +CREATE SCHEMA IF NOT EXISTS app; |
| 5 | +SET SCHEMA 'app'; |
| 6 | + |
| 7 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_transaction_id_fkey; |
| 8 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_record_id_fkey; |
| 9 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_contract_id_fkey; |
| 10 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_block_id_fkey; |
| 11 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_agent_id_fkey; |
| 12 | +ALTER TABLE IF EXISTS ONLY app.almanac_records DROP CONSTRAINT IF EXISTS almanac_records_transaction_id_fkey; |
| 13 | +ALTER TABLE IF EXISTS ONLY app.almanac_records DROP CONSTRAINT IF EXISTS almanac_records_block_id_fkey; |
| 14 | +DROP INDEX IF EXISTS app.almanac_registrations_transaction_id; |
| 15 | +DROP INDEX IF EXISTS app.almanac_registrations_record_id; |
| 16 | +DROP INDEX IF EXISTS app.almanac_registrations_expiry_height; |
| 17 | +DROP INDEX IF EXISTS app.almanac_registrations_contract_id; |
| 18 | +DROP INDEX IF EXISTS app.almanac_registrations_block_id; |
| 19 | +DROP INDEX IF EXISTS app.almanac_registrations_agent_id; |
| 20 | +DROP INDEX IF EXISTS app.almanac_records_transaction_id; |
| 21 | +DROP INDEX IF EXISTS app.almanac_records_service; |
| 22 | +DROP INDEX IF EXISTS app.almanac_records_block_id; |
| 23 | +ALTER TABLE IF EXISTS ONLY app.almanac_registrations DROP CONSTRAINT IF EXISTS almanac_registrations_pkey; |
| 24 | +ALTER TABLE IF EXISTS ONLY app.almanac_records DROP CONSTRAINT IF EXISTS almanac_records_pkey; |
| 25 | +ALTER TABLE IF EXISTS ONLY app.agents DROP CONSTRAINT IF EXISTS agents_pkey; |
| 26 | +DROP TABLE IF EXISTS app.almanac_registrations; |
| 27 | +DROP TABLE IF EXISTS app.almanac_records; |
| 28 | +DROP TABLE IF EXISTS app.agents; |
| 29 | +SET default_tablespace = ''; |
| 30 | + |
| 31 | +SET default_table_access_method = heap; |
| 32 | + |
| 33 | +-- |
| 34 | +-- Name: agents; Type: TABLE; Schema: app; Owner: subquery |
| 35 | +-- |
| 36 | + |
| 37 | +CREATE TABLE app.agents ( |
| 38 | + id text NOT NULL |
| 39 | +); |
| 40 | + |
| 41 | + |
| 42 | +ALTER TABLE app.agents OWNER TO subquery; |
| 43 | + |
| 44 | +-- |
| 45 | +-- Name: almanac_records; Type: TABLE; Schema: app; Owner: subquery |
| 46 | +-- |
| 47 | + |
| 48 | +CREATE TABLE app.almanac_records ( |
| 49 | + id text NOT NULL, |
| 50 | + service jsonb, |
| 51 | + transaction_id text NOT NULL, |
| 52 | + block_id text NOT NULL |
| 53 | +); |
| 54 | + |
| 55 | + |
| 56 | +ALTER TABLE app.almanac_records OWNER TO subquery; |
| 57 | + |
| 58 | +-- |
| 59 | +-- Name: almanac_registrations; Type: TABLE; Schema: app; Owner: subquery |
| 60 | +-- |
| 61 | + |
| 62 | +CREATE TABLE app.almanac_registrations ( |
| 63 | + id text NOT NULL, |
| 64 | + expiry_height numeric NOT NULL, |
| 65 | + agent_id text NOT NULL, |
| 66 | + signature text NOT NULL, |
| 67 | + sequence integer NOT NULL, |
| 68 | + contract_id text NOT NULL, |
| 69 | + record_id text NOT NULL, |
| 70 | + transaction_id text NOT NULL, |
| 71 | + block_id text NOT NULL |
| 72 | +); |
| 73 | + |
| 74 | + |
| 75 | +ALTER TABLE app.almanac_registrations OWNER TO subquery; |
| 76 | + |
| 77 | + |
| 78 | +-- |
| 79 | +-- Name: agents agents_pkey; Type: CONSTRAINT; Schema: app; Owner: subquery |
| 80 | +-- |
| 81 | + |
| 82 | +ALTER TABLE ONLY app.agents |
| 83 | + ADD CONSTRAINT agents_pkey PRIMARY KEY (id); |
| 84 | + |
| 85 | + |
| 86 | +-- |
| 87 | +-- Name: almanac_records almanac_records_pkey; Type: CONSTRAINT; Schema: app; Owner: subquery |
| 88 | +-- |
| 89 | + |
| 90 | +ALTER TABLE ONLY app.almanac_records |
| 91 | + ADD CONSTRAINT almanac_records_pkey PRIMARY KEY (id); |
| 92 | + |
| 93 | + |
| 94 | +-- |
| 95 | +-- Name: almanac_registrations almanac_registrations_pkey; Type: CONSTRAINT; Schema: app; Owner: subquery |
| 96 | +-- |
| 97 | + |
| 98 | +ALTER TABLE ONLY app.almanac_registrations |
| 99 | + ADD CONSTRAINT almanac_registrations_pkey PRIMARY KEY (id); |
| 100 | + |
| 101 | + |
| 102 | +-- |
| 103 | +-- Name: almanac_records_block_id; Type: INDEX; Schema: app; Owner: subquery |
| 104 | +-- |
| 105 | + |
| 106 | +CREATE INDEX almanac_records_block_id ON app.almanac_records USING hash (block_id); |
| 107 | + |
| 108 | + |
| 109 | +-- |
| 110 | +-- Name: almanac_records_service; Type: INDEX; Schema: app; Owner: subquery |
| 111 | +-- |
| 112 | + |
| 113 | +CREATE INDEX almanac_records_service ON app.almanac_records USING gin (service); |
| 114 | + |
| 115 | + |
| 116 | +-- |
| 117 | +-- Name: almanac_records_transaction_id; Type: INDEX; Schema: app; Owner: subquery |
| 118 | +-- |
| 119 | + |
| 120 | +CREATE INDEX almanac_records_transaction_id ON app.almanac_records USING hash (transaction_id); |
| 121 | + |
| 122 | + |
| 123 | +-- |
| 124 | +-- Name: almanac_registrations_agent_id; Type: INDEX; Schema: app; Owner: subquery |
| 125 | +-- |
| 126 | + |
| 127 | +CREATE INDEX almanac_registrations_agent_id ON app.almanac_registrations USING hash (agent_id); |
| 128 | + |
| 129 | + |
| 130 | +-- |
| 131 | +-- Name: almanac_registrations_block_id; Type: INDEX; Schema: app; Owner: subquery |
| 132 | +-- |
| 133 | + |
| 134 | +CREATE INDEX almanac_registrations_block_id ON app.almanac_registrations USING hash (block_id); |
| 135 | + |
| 136 | + |
| 137 | +-- |
| 138 | +-- Name: almanac_registrations_contract_id; Type: INDEX; Schema: app; Owner: subquery |
| 139 | +-- |
| 140 | + |
| 141 | +CREATE INDEX almanac_registrations_contract_id ON app.almanac_registrations USING hash (contract_id); |
| 142 | + |
| 143 | + |
| 144 | +-- |
| 145 | +-- Name: almanac_registrations_expiry_height; Type: INDEX; Schema: app; Owner: subquery |
| 146 | +-- |
| 147 | + |
| 148 | +CREATE INDEX almanac_registrations_expiry_height ON app.almanac_registrations USING btree (expiry_height); |
| 149 | + |
| 150 | + |
| 151 | +-- |
| 152 | +-- Name: almanac_registrations_record_id; Type: INDEX; Schema: app; Owner: subquery |
| 153 | +-- |
| 154 | + |
| 155 | +CREATE INDEX almanac_registrations_record_id ON app.almanac_registrations USING hash (record_id); |
| 156 | + |
| 157 | + |
| 158 | +-- |
| 159 | +-- Name: almanac_registrations_transaction_id; Type: INDEX; Schema: app; Owner: subquery |
| 160 | +-- |
| 161 | + |
| 162 | +CREATE INDEX almanac_registrations_transaction_id ON app.almanac_registrations USING hash (transaction_id); |
| 163 | + |
| 164 | + |
| 165 | +-- |
| 166 | +-- Name: almanac_records almanac_records_block_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 167 | +-- |
| 168 | + |
| 169 | +ALTER TABLE ONLY app.almanac_records |
| 170 | + ADD CONSTRAINT almanac_records_block_id_fkey FOREIGN KEY (block_id) REFERENCES app.blocks(id) ON UPDATE CASCADE; |
| 171 | + |
| 172 | + |
| 173 | +-- |
| 174 | +-- Name: almanac_records almanac_records_transaction_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 175 | +-- |
| 176 | + |
| 177 | +ALTER TABLE ONLY app.almanac_records |
| 178 | + ADD CONSTRAINT almanac_records_transaction_id_fkey FOREIGN KEY (transaction_id) REFERENCES app.transactions(id) ON UPDATE CASCADE; |
| 179 | + |
| 180 | + |
| 181 | +-- |
| 182 | +-- Name: almanac_registrations almanac_registrations_agent_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 183 | +-- |
| 184 | + |
| 185 | +ALTER TABLE ONLY app.almanac_registrations |
| 186 | + ADD CONSTRAINT almanac_registrations_agent_id_fkey FOREIGN KEY (agent_id) REFERENCES app.agents(id) ON UPDATE CASCADE ON DELETE CASCADE; |
| 187 | + |
| 188 | + |
| 189 | +-- |
| 190 | +-- Name: CONSTRAINT almanac_registrations_agent_id_fkey ON almanac_registrations; Type: COMMENT; Schema: app; Owner: subquery |
| 191 | +-- |
| 192 | + |
| 193 | +COMMENT ON CONSTRAINT almanac_registrations_agent_id_fkey ON app.almanac_registrations IS '@foreignFieldName almanacRegistrations'; |
| 194 | + |
| 195 | + |
| 196 | +-- |
| 197 | +-- Name: almanac_registrations almanac_registrations_block_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 198 | +-- |
| 199 | + |
| 200 | +ALTER TABLE ONLY app.almanac_registrations |
| 201 | + ADD CONSTRAINT almanac_registrations_block_id_fkey FOREIGN KEY (block_id) REFERENCES app.blocks(id) ON UPDATE CASCADE; |
| 202 | + |
| 203 | + |
| 204 | +-- |
| 205 | +-- Name: almanac_registrations almanac_registrations_contract_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 206 | +-- |
| 207 | + |
| 208 | +ALTER TABLE ONLY app.almanac_registrations |
| 209 | + ADD CONSTRAINT almanac_registrations_contract_id_fkey FOREIGN KEY (contract_id) REFERENCES app.contracts(id) ON UPDATE CASCADE; |
| 210 | + |
| 211 | + |
| 212 | +-- |
| 213 | +-- Name: almanac_registrations almanac_registrations_record_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 214 | +-- |
| 215 | + |
| 216 | +ALTER TABLE ONLY app.almanac_registrations |
| 217 | + ADD CONSTRAINT almanac_registrations_record_id_fkey FOREIGN KEY (record_id) REFERENCES app.almanac_records(id) ON UPDATE CASCADE; |
| 218 | + |
| 219 | + |
| 220 | +-- |
| 221 | +-- Name: almanac_registrations almanac_registrations_transaction_id_fkey; Type: FK CONSTRAINT; Schema: app; Owner: subquery |
| 222 | +-- |
| 223 | + |
| 224 | +ALTER TABLE ONLY app.almanac_registrations |
| 225 | + ADD CONSTRAINT almanac_registrations_transaction_id_fkey FOREIGN KEY (transaction_id) REFERENCES app.transactions(id) ON UPDATE CASCADE; |
| 226 | + |
| 227 | + |
| 228 | +-- |
| 229 | +-- PostgreSQL database dump complete |
| 230 | +-- |
| 231 | + |
| 232 | + |
| 233 | +DROP FUNCTION IF EXISTS plv8ify_migrationMicroAgentAlmanacRegistrations(); |
| 234 | +CREATE OR REPLACE FUNCTION plv8ify_migrationMicroAgentAlmanacRegistrations() RETURNS JSONB AS $plv8ify$ |
| 235 | +var plv8ify = (() => { |
| 236 | + var __defProp = Object.defineProperty; |
| 237 | + var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 238 | + var __getOwnPropNames = Object.getOwnPropertyNames; |
| 239 | + var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 240 | + var __export = (target, all) => { |
| 241 | + for (var name in all) |
| 242 | + __defProp(target, name, { get: all[name], enumerable: true }); |
| 243 | + }; |
| 244 | + var __copyProps = (to, from, except, desc) => { |
| 245 | + if (from && typeof from === "object" || typeof from === "function") { |
| 246 | + for (let key of __getOwnPropNames(from)) |
| 247 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 248 | + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 249 | + } |
| 250 | + return to; |
| 251 | + }; |
| 252 | + var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 253 | + |
| 254 | + // migrations/current.ts |
| 255 | + var current_exports = {}; |
| 256 | + __export(current_exports, { |
| 257 | + migrationMicroAgentAlmanacRegistrations: () => migrationMicroAgentAlmanacRegistrations |
| 258 | + }); |
| 259 | + |
| 260 | + // migrations/src/utils.ts |
| 261 | + function getSelectResults(rows) { |
| 262 | + if (rows.length < 1) { |
| 263 | + return null; |
| 264 | + } |
| 265 | + return rows.map((row) => Object.entries(row).map((e) => e[1])); |
| 266 | + } |
| 267 | + |
| 268 | + // migrations/current.ts |
| 269 | + function registerEventIds(limit, offset) { |
| 270 | + const results = getSelectResults(plv8.execute(`SELECT ev.id |
| 271 | + FROM app.events ev |
| 272 | + JOIN app.event_attributes ea ON ev.id = ea.event_id |
| 273 | + WHERE ev.type = 'wasm' |
| 274 | + AND ea.key = 'action' |
| 275 | + AND ea.value = 'register' |
| 276 | + LIMIT ${limit} |
| 277 | + OFFSET ${offset}`)); |
| 278 | + if (results) { |
| 279 | + return results.map((e) => e[0]); |
| 280 | + } |
| 281 | + return null; |
| 282 | + } |
| 283 | + function registerEventData(eventIds) { |
| 284 | + if (eventIds.length == 0) { |
| 285 | + return []; |
| 286 | + } |
| 287 | + const eventIdValues = eventIds.map((e) => `'${e}'`).join(", "); |
| 288 | + return getSelectResults(plv8.execute(`SELECT ev.id, ev.transaction_id, ev.block_id, ea.key, ea.value |
| 289 | + FROM app.events ev |
| 290 | + JOIN app.event_attributes ea ON ev.id = ea.event_id |
| 291 | + WHERE ev.id in (${eventIdValues})`)); |
| 292 | + } |
| 293 | + function insertAgents(agents) { |
| 294 | + const agentIdValues = Object.values(agents).map((id) => `('${id}')`).join(", "); |
| 295 | + const insertAgents2 = `INSERT INTO app.agents (id) |
| 296 | + VALUES ${agentIdValues}`; |
| 297 | + plv8.execute(insertAgents2); |
| 298 | + } |
| 299 | + function migrationMicroAgentAlmanacRegistrations() { |
| 300 | + plv8.execute("SET SCHEMA 'app'"); |
| 301 | + const batchSize = 500; |
| 302 | + for (let i = 0; ; i++) { |
| 303 | + const eventIdsBatch = registerEventIds(batchSize, batchSize * i); |
| 304 | + if (!eventIdsBatch) { |
| 305 | + break; |
| 306 | + } |
| 307 | + const registerEvents = registerEventData(eventIdsBatch); |
| 308 | + const eventIds = {}; |
| 309 | + const agents = {}; |
| 310 | + const services = {}; |
| 311 | + const expiryHeights = {}; |
| 312 | + const signatures = {}; |
| 313 | + const sequences = {}; |
| 314 | + const contracts = {}; |
| 315 | + const txIds = {}; |
| 316 | + const blockIds = {}; |
| 317 | + for (const record of registerEvents) { |
| 318 | + if (record.length < 5) { |
| 319 | + plv8.elog(WARNING, `unable to migrate registration event; event ID: ${record[0]}`); |
| 320 | + continue; |
| 321 | + } |
| 322 | + const [eventId, txId, blockId, key, value] = record; |
| 323 | + eventIds[eventId] = null; |
| 324 | + if (!txIds[eventId]) { |
| 325 | + txIds[eventId] = txId; |
| 326 | + } |
| 327 | + if (!blockIds[eventId]) { |
| 328 | + blockIds[eventId] = blockId; |
| 329 | + } |
| 330 | + switch (key) { |
| 331 | + case "_contract_address": |
| 332 | + contracts[eventId] = value; |
| 333 | + break; |
| 334 | + case "agent_address": |
| 335 | + agents[eventId] = value; |
| 336 | + break; |
| 337 | + case "record": |
| 338 | + try { |
| 339 | + const unescapedValue = value.replace(new RegExp('\\\\"', "g"), '"'); |
| 340 | + const service = JSON.parse(unescapedValue).service; |
| 341 | + if (!service) { |
| 342 | + throw new Error("expected record to contain service key but none found"); |
| 343 | + } |
| 344 | + services[eventId] = JSON.stringify(service); |
| 345 | + } catch (error) { |
| 346 | + plv8.elog(WARNING, `unable to parse expected JSON value "${value}": ${error.toString()}`); |
| 347 | + continue; |
| 348 | + } |
| 349 | + break; |
| 350 | + case "signature": |
| 351 | + signatures[eventId] = value; |
| 352 | + break; |
| 353 | + case "sequence": |
| 354 | + sequences[eventId] = value; |
| 355 | + break; |
| 356 | + case "expiry_height": |
| 357 | + expiryHeights[eventId] = value; |
| 358 | + break; |
| 359 | + } |
| 360 | + } |
| 361 | + insertAgents(agents); |
| 362 | + const recordValues = Object.keys(eventIds).map((eventId) => { |
| 363 | + return "(" + [ |
| 364 | + eventId, |
| 365 | + services[eventId], |
| 366 | + txIds[eventId], |
| 367 | + blockIds[eventId] |
| 368 | + ].map((e) => `'${e}'`).join(", ") + ")"; |
| 369 | + }).join(", "); |
| 370 | + plv8.execute(`INSERT INTO app.almanac_records (id, service, transaction_id, block_id) |
| 371 | + VALUES ${recordValues}`); |
| 372 | + const registrationValues = Object.keys(eventIds).map((eventId) => { |
| 373 | + return "(" + [ |
| 374 | + eventId, |
| 375 | + expiryHeights[eventId], |
| 376 | + signatures[eventId], |
| 377 | + sequences[eventId], |
| 378 | + agents[eventId], |
| 379 | + eventId, |
| 380 | + contracts[eventId], |
| 381 | + txIds[eventId], |
| 382 | + blockIds[eventId] |
| 383 | + ].map((e) => `'${e}'`).join(", ") + ")"; |
| 384 | + }).join(", "); |
| 385 | + const insertRegistrations = `INSERT INTO app.almanac_registrations (id, expiry_height, signature, sequence, agent_id, record_id, |
| 386 | + contract_id, transaction_id, block_id) |
| 387 | + VALUES ${registrationValues}`; |
| 388 | + plv8.execute(insertRegistrations); |
| 389 | + } |
| 390 | + } |
| 391 | + return __toCommonJS(current_exports); |
| 392 | +})(); |
| 393 | + |
| 394 | +return plv8ify.migrationMicroAgentAlmanacRegistrations() |
| 395 | + |
| 396 | +$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT; |
| 397 | +SELECT * from plv8ify_migrationMicroAgentAlmanacRegistrations(); |
0 commit comments