|
| 1 | +/*######################### cosmos00.sql ##########################*/ |
| 2 | + |
| 3 | +/* change vacuum rules */ |
| 4 | +ALTER TABLE block |
| 5 | +SET ( |
| 6 | +autovacuum_vacuum_scale_factor = 0, |
| 7 | +autovacuum_analyze_scale_factor = 0, |
| 8 | +autovacuum_vacuum_threshold = 10000, |
| 9 | +autovacuum_analyze_threshold = 10000 |
| 10 | +); |
| 11 | + |
| 12 | +/* transaction */ |
| 13 | +-- Add new column to the parent transaction table |
| 14 | +ALTER TABLE transaction |
| 15 | +ADD COLUMN events JSONB NOT NULL DEFAULT '[]'::JSONB; |
| 16 | + |
| 17 | +-- Change messages column type to JSON in the parent transaction table |
| 18 | +ALTER TABLE transaction |
| 19 | +ALTER COLUMN messages DROP DEFAULT, |
| 20 | +ALTER COLUMN messages TYPE JSON USING messages::JSON, |
| 21 | +ALTER COLUMN messages SET DEFAULT '[]'::JSON; |
| 22 | + |
| 23 | +-- Add new column to the child transaction tables |
| 24 | +SELECT format( |
| 25 | + 'ALTER TABLE %I ADD COLUMN events JSONB NOT NULL DEFAULT ''[]''::JSONB;', |
| 26 | + relname |
| 27 | +) |
| 28 | +FROM pg_class |
| 29 | +WHERE relname LIKE 'transaction_%' AND relkind = 'r'; |
| 30 | + |
| 31 | +-- Change messages column type to JSON in the child transaction tables |
| 32 | +SELECT format( |
| 33 | + $$ |
| 34 | + ALTER TABLE %I |
| 35 | + ALTER COLUMN messages DROP DEFAULT, |
| 36 | + ALTER COLUMN messages TYPE JSON USING messages::JSON, |
| 37 | + ALTER COLUMN messages SET DEFAULT '[]'::JSON; |
| 38 | + $$, |
| 39 | + relname |
| 40 | +) |
| 41 | +FROM pg_class |
| 42 | +WHERE relname LIKE 'transaction_%' AND relkind = 'r'; |
| 43 | + |
| 44 | +/* message */ |
| 45 | +-- Change value column from JSONB to JSON |
| 46 | +ALTER TABLE message |
| 47 | +ALTER COLUMN value DROP DEFAULT, |
| 48 | +ALTER COLUMN value TYPE JSON USING value::JSON; |
| 49 | + |
| 50 | +-- Add foreign key to message_type(type) |
| 51 | +ALTER TABLE message |
| 52 | +ADD CONSTRAINT message_type_fk |
| 53 | +FOREIGN KEY (type) REFERENCES message_type(type); |
| 54 | + |
| 55 | +-- Alter child message tables |
| 56 | +DO $$ |
| 57 | +DECLARE |
| 58 | + r RECORD; |
| 59 | +BEGIN |
| 60 | + FOR r IN |
| 61 | + SELECT inhrelid::regclass AS partition_name |
| 62 | + FROM pg_inherits |
| 63 | + WHERE inhparent = 'message'::regclass |
| 64 | + LOOP |
| 65 | + RAISE NOTICE 'Altering partition: %', r.partition_name; |
| 66 | + |
| 67 | + -- Add FK constraint on type column |
| 68 | + EXECUTE format( |
| 69 | + 'ALTER TABLE %I |
| 70 | + ADD CONSTRAINT %I |
| 71 | + FOREIGN KEY (type) REFERENCES message_type(type);', |
| 72 | + r.partition_name, |
| 73 | + r.partition_name || '_type_fk' |
| 74 | + ); |
| 75 | + END LOOP; |
| 76 | +END |
| 77 | +$$; |
| 78 | + |
| 79 | + |
| 80 | +/* new message_by_address function */ |
| 81 | +DROP FUNCTION IF EXISTS messages_by_address( |
| 82 | + TEXT[], |
| 83 | + TEXT[], |
| 84 | + BIGINT, |
| 85 | + BIGINT |
| 86 | +); |
| 87 | + |
| 88 | +CREATE FUNCTION messages_by_address( |
| 89 | + addresses TEXT[], |
| 90 | + types TEXT[], |
| 91 | + "limit" BIGINT = 100, |
| 92 | + "offset" BIGINT = 0) |
| 93 | + RETURNS SETOF message AS |
| 94 | +$$ |
| 95 | +SELECT * FROM message |
| 96 | +WHERE (cardinality(types) = 0 OR type = ANY (types)) |
| 97 | + AND addresses && involved_accounts_addresses |
| 98 | +ORDER BY height DESC LIMIT "limit" OFFSET "offset" |
| 99 | +$$ LANGUAGE sql STABLE; |
| 100 | + |
| 101 | +/* Create message_by_address function */ |
| 102 | +CREATE FUNCTION messages_by_type( |
| 103 | + types text [], |
| 104 | + "limit" bigint DEFAULT 100, |
| 105 | + "offset" bigint DEFAULT 0) |
| 106 | + RETURNS SETOF message AS |
| 107 | +$$ |
| 108 | +SELECT * FROM message |
| 109 | +WHERE (cardinality(types) = 0 OR type = ANY (types)) |
| 110 | +ORDER BY height DESC LIMIT "limit" OFFSET "offset" |
| 111 | +$$ LANGUAGE sql STABLE; |
| 112 | + |
| 113 | + |
| 114 | +/*######################### staking.sql ##########################*/ |
| 115 | +/* Add column to table vesting_account */ |
| 116 | +ALTER TABLE staking_pool |
| 117 | +ADD COLUMN unbonding_tokens TEXT NOT NULL DEFAULT '', |
| 118 | +ADD COLUMN staked_not_bonded_tokens TEXT NOT NULL DEFAULT ''; |
| 119 | + |
| 120 | +/* Drop column from table validator_status */ |
| 121 | +ALTER TABLE validator_status |
| 122 | +DROP COLUMN tombstoned; |
| 123 | + |
| 124 | +/*######################### gov.sql ##########################*/ |
| 125 | +/* Add column to table gov_params */ |
| 126 | +ALTER TABLE gov_params |
| 127 | +ADD COLUMN params JSONB NOT NULL DEFAULT '[]'::JSONB; |
| 128 | + |
| 129 | +/* Drop columns from table gov_params */ |
| 130 | +ALTER TABLE gov_params |
| 131 | +DROP COLUMN deposit_params, |
| 132 | +DROP COLUMN voting_params, |
| 133 | +DROP COLUMN tally_params; |
| 134 | + |
| 135 | +/* Drop columns from table proposal */ |
| 136 | +ALTER TABLE proposal |
| 137 | +DROP COLUMN proposal_route; |
| 138 | + |
| 139 | +/* Add column to table proposal */ |
| 140 | +ALTER TABLE proposal |
| 141 | +ADD COLUMN metadata TEXT NOT NULL DEFAULT ''; |
| 142 | + |
| 143 | +/* Set default for content column */ |
| 144 | +ALTER TABLE proposal |
| 145 | +ALTER COLUMN content SET DEFAULT '[]'::JSONB; |
| 146 | + |
| 147 | +/* Drop constraint unique_deposit from table proposal_deposit */ |
| 148 | +ALTER TABLE proposal_deposit |
| 149 | +DROP CONSTRAINT unique_deposit; |
| 150 | + |
| 151 | +/* Drop constraint proposal_deposit_height_fkey from table proposal_deposit */ |
| 152 | +ALTER TABLE proposal_deposit |
| 153 | +DROP CONSTRAINT IF EXISTS proposal_deposit_height_fkey; |
| 154 | + |
| 155 | +/* Add columns to table proposal_deposit */ |
| 156 | +ALTER TABLE proposal_deposit |
| 157 | +ADD COLUMN timestamp TIMESTAMP, |
| 158 | +ADD COLUMN transaction_hash TEXT NOT NULL DEFAULT ''; |
| 159 | + |
| 160 | +/* Add constraint to table proposal_deposit */ |
| 161 | +ALTER TABLE proposal_deposit |
| 162 | +ADD CONSTRAINT unique_deposit UNIQUE (proposal_id, depositor_address, transaction_hash); |
| 163 | + |
| 164 | +/* Drop constraint unique_deposit from table proposal_vote */ |
| 165 | +ALTER TABLE proposal_vote |
| 166 | +DROP CONSTRAINT unique_vote; |
| 167 | + |
| 168 | +/* Drop constraint proposal_deposit_height_fkey from table proposal_deposit */ |
| 169 | +ALTER TABLE proposal_vote |
| 170 | +DROP CONSTRAINT IF EXISTS proposal_vote_height_fkey; |
| 171 | + |
| 172 | +/* Add columns to table proposal_deposit */ |
| 173 | +ALTER TABLE proposal_vote |
| 174 | +ADD COLUMN weight TEXT NOT NULL DEFAULT '', |
| 175 | +ADD COLUMN timestamp TIMESTAMP; |
| 176 | + |
| 177 | +/* Add constraint to table proposal_deposit */ |
| 178 | +ALTER TABLE proposal_vote |
| 179 | +ADD CONSTRAINT unique_vote UNIQUE (proposal_id, voter_address, option); |
| 180 | + |
| 181 | +/*######################### upgrade.sql ##########################*/ |
| 182 | +CREATE TABLE software_upgrade_plan |
| 183 | +( |
| 184 | + proposal_id INTEGER REFERENCES proposal (id) UNIQUE, |
| 185 | + plan_name TEXT NOT NULL, |
| 186 | + upgrade_height BIGINT NOT NULL, |
| 187 | + info TEXT NOT NULL, |
| 188 | + height BIGINT NOT NULL |
| 189 | +); |
| 190 | +CREATE INDEX software_upgrade_plan_proposal_id_index ON software_upgrade_plan (proposal_id); |
| 191 | +CREATE INDEX software_upgrade_plan_height_index ON software_upgrade_plan (height); |
0 commit comments