Skip to content

Commit a7988ad

Browse files
committed
Archive > Mesa Migration: migrate non-nullable zkapp state as well
1 parent eec1737 commit a7988ad

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

src/app/archive/downgrade_to_berkeley.sql

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- ============================================================================
22
-- Mina rollback: from mesa to berkeley
3+
-- + remove zkapp_states.element31..element8 (int)
34
-- + remove zkapp_states_nullable.element31..element8 (int)
45
-- + record status in migration_history
56
-- ============================================================================
@@ -17,7 +18,7 @@ SET archive.current_protocol_version = '4.0.0';
1718
-- Post-HF protocol version. This one corresponds to Mesa, specifically
1819
SET archive.target_protocol_version = '3.0.0';
1920
-- The version of this script. If you modify the script, please bump the version
20-
SET archive.migration_version = '0.0.2';
21+
SET archive.migration_version = '0.0.3';
2122

2223
-- TODO: put below in a common script
2324

@@ -127,7 +128,55 @@ EXCEPTION
127128
END
128129
$$ LANGUAGE plpgsql;
129130

130-
-- 2. `zkapp_states_nullable`: Remove nullable columns element31..element8
131+
-- 2. `zkapp_states`: Remove columns element31..element8
132+
133+
CREATE OR REPLACE FUNCTION try_remove_zkapp_states_element(p_element_num INT)
134+
RETURNS VOID AS $$
135+
DECLARE
136+
col_name TEXT := 'element' || p_element_num;
137+
BEGIN
138+
139+
RAISE DEBUG 'Trying to removing column % for zkapp_states', col_name;
140+
141+
EXECUTE format(
142+
'ALTER TABLE zkapp_states DROP COLUMN IF EXISTS %I',
143+
col_name
144+
);
145+
146+
RAISE DEBUG 'Ensured column % for zkapp_states not existent', col_name;
147+
EXCEPTION
148+
WHEN OTHERS THEN
149+
PERFORM set_migration_status('failed'::migration_status);
150+
RAISE EXCEPTION 'An error occurred: %', SQLERRM;
151+
END
152+
$$ LANGUAGE plpgsql;
153+
154+
SELECT try_remove_zkapp_states_element(31);
155+
SELECT try_remove_zkapp_states_element(30);
156+
SELECT try_remove_zkapp_states_element(29);
157+
SELECT try_remove_zkapp_states_element(28);
158+
SELECT try_remove_zkapp_states_element(27);
159+
SELECT try_remove_zkapp_states_element(26);
160+
SELECT try_remove_zkapp_states_element(25);
161+
SELECT try_remove_zkapp_states_element(24);
162+
SELECT try_remove_zkapp_states_element(23);
163+
SELECT try_remove_zkapp_states_element(22);
164+
SELECT try_remove_zkapp_states_element(21);
165+
SELECT try_remove_zkapp_states_element(20);
166+
SELECT try_remove_zkapp_states_element(19);
167+
SELECT try_remove_zkapp_states_element(18);
168+
SELECT try_remove_zkapp_states_element(17);
169+
SELECT try_remove_zkapp_states_element(16);
170+
SELECT try_remove_zkapp_states_element(15);
171+
SELECT try_remove_zkapp_states_element(14);
172+
SELECT try_remove_zkapp_states_element(13);
173+
SELECT try_remove_zkapp_states_element(12);
174+
SELECT try_remove_zkapp_states_element(11);
175+
SELECT try_remove_zkapp_states_element(10);
176+
SELECT try_remove_zkapp_states_element(9);
177+
SELECT try_remove_zkapp_states_element(8);
178+
179+
-- 3. `zkapp_states_nullable`: Remove nullable columns element31..element8
131180

132181
SELECT try_remove_zkapp_states_nullable_element(31);
133182
SELECT try_remove_zkapp_states_nullable_element(30);
@@ -154,7 +203,7 @@ SELECT try_remove_zkapp_states_nullable_element(10);
154203
SELECT try_remove_zkapp_states_nullable_element(9);
155204
SELECT try_remove_zkapp_states_nullable_element(8);
156205

157-
-- 3. Update schema_history
206+
-- 4. Update schema_history
158207

159208
DO $$
160209
BEGIN

src/app/archive/upgrade_to_mesa.sql

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- =============================================================================
22
-- Mina migration: from berkeley to mesa
33
-- + extend zkapp_states_nullable with element8..element31 (int)
4+
-- + extend zkapp_states with element8..element31 (int)
45
-- + record status in migration_history
56
-- =============================================================================
67

@@ -17,7 +18,7 @@ SET archive.current_protocol_version = '3.0.0';
1718
-- Post-HF protocol version. This one corresponds to Mesa, specifically
1819
SET archive.target_protocol_version = '4.0.0';
1920
-- The version of this script. If you modify the script, please bump the version
20-
SET archive.migration_version = '0.0.2';
21+
SET archive.migration_version = '0.0.3';
2122

2223
-- TODO: put below in a common script
2324

@@ -89,7 +90,7 @@ BEGIN
8990
) VALUES (
9091
target_protocol_version,
9192
target_migration_version,
92-
'Upgrade from Berkeley to Mesa. Add zkapp_states_nullable.element8..element31 (int)',
93+
'Upgrade from Berkeley to Mesa. Add {zkapp_states,zkapp_states_nullable}.element8..element31 (int)',
9394
'starting'::migration_status
9495
);
9596
ELSIF
@@ -160,7 +161,55 @@ SELECT add_zkapp_states_nullable_element(29);
160161
SELECT add_zkapp_states_nullable_element(30);
161162
SELECT add_zkapp_states_nullable_element(31);
162163

163-
-- 3. Update schema_history
164+
-- 3. `zkapp_states`: Add columns element8..element31
165+
CREATE OR REPLACE FUNCTION add_zkapp_states_element(p_element_num INT)
166+
RETURNS VOID AS $$
167+
DECLARE
168+
col_name TEXT := 'element' || p_element_num;
169+
BEGIN
170+
171+
RAISE DEBUG 'Adding column % for zkapp_states', col_name;
172+
173+
EXECUTE format(
174+
'ALTER TABLE zkapp_states ADD COLUMN IF NOT EXISTS %I INT DEFAULT 0 NOT NULL REFERENCES zkapp_field(id)',
175+
col_name
176+
);
177+
178+
RAISE DEBUG 'Added column % for zkapp_states', col_name;
179+
180+
EXCEPTION
181+
WHEN OTHERS THEN
182+
PERFORM set_migration_status('failed'::migration_status);
183+
RAISE EXCEPTION 'An error occurred while adding column % to zkapp_states: %', col_name, SQLERRM;
184+
END
185+
$$ LANGUAGE plpgsql;
186+
187+
SELECT add_zkapp_states_element(8);
188+
SELECT add_zkapp_states_element(9);
189+
SELECT add_zkapp_states_element(10);
190+
SELECT add_zkapp_states_element(11);
191+
SELECT add_zkapp_states_element(12);
192+
SELECT add_zkapp_states_element(13);
193+
SELECT add_zkapp_states_element(14);
194+
SELECT add_zkapp_states_element(15);
195+
SELECT add_zkapp_states_element(16);
196+
SELECT add_zkapp_states_element(17);
197+
SELECT add_zkapp_states_element(18);
198+
SELECT add_zkapp_states_element(19);
199+
SELECT add_zkapp_states_element(20);
200+
SELECT add_zkapp_states_element(21);
201+
SELECT add_zkapp_states_element(22);
202+
SELECT add_zkapp_states_element(23);
203+
SELECT add_zkapp_states_element(24);
204+
SELECT add_zkapp_states_element(25);
205+
SELECT add_zkapp_states_element(26);
206+
SELECT add_zkapp_states_element(27);
207+
SELECT add_zkapp_states_element(28);
208+
SELECT add_zkapp_states_element(29);
209+
SELECT add_zkapp_states_element(30);
210+
SELECT add_zkapp_states_element(31);
211+
212+
-- 4. Update schema_history
164213

165214
DO $$
166215
BEGIN

0 commit comments

Comments
 (0)