Skip to content

Commit 5f1efc1

Browse files
authored
Merge pull request #17981 from MinaProtocol/lyh/fix-non-nullable-zkapp-state-migrate-v2
Fix Archive Migration Script for Berkeley -> Mesa v2
2 parents 86e41e4 + 31835b3 commit 5f1efc1

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/app/archive/downgrade_to_berkeley.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SET archive.current_protocol_version = '4.0.0';
1818
-- Post-HF protocol version. This one corresponds to Mesa, specifically
1919
SET archive.target_protocol_version = '3.0.0';
2020
-- The version of this script. If you modify the script, please bump the version
21-
SET archive.migration_version = '0.0.3';
21+
SET archive.migration_version = '0.0.4';
2222

2323
-- TODO: put below in a common script
2424

src/app/archive/upgrade_to_mesa.sql

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SET archive.current_protocol_version = '3.0.0';
1818
-- Post-HF protocol version. This one corresponds to Mesa, specifically
1919
SET archive.target_protocol_version = '4.0.0';
2020
-- The version of this script. If you modify the script, please bump the version
21-
SET archive.migration_version = '0.0.3';
21+
SET archive.migration_version = '0.0.4';
2222

2323
-- TODO: put below in a common script
2424

@@ -162,17 +162,39 @@ SELECT add_zkapp_states_nullable_element(30);
162162
SELECT add_zkapp_states_nullable_element(31);
163163

164164
-- 3. `zkapp_states`: Add columns element8..element31
165+
166+
CREATE OR REPLACE FUNCTION get_zero_field_id() RETURNS int AS $$
167+
DECLARE
168+
result int;
169+
zero text := '0';
170+
BEGIN
171+
-- Try to fetch existing id
172+
SELECT id INTO result FROM zkapp_field WHERE field = zero;
173+
174+
-- If not found, insert and get the new id
175+
IF result IS NULL THEN
176+
INSERT INTO zkapp_field(field)
177+
VALUES (zero)
178+
RETURNING id INTO result;
179+
END IF;
180+
181+
RETURN result;
182+
END
183+
$$ LANGUAGE plpgsql;
184+
165185
CREATE OR REPLACE FUNCTION add_zkapp_states_element(p_element_num INT)
166186
RETURNS VOID AS $$
167187
DECLARE
168188
col_name TEXT := 'element' || p_element_num;
189+
default_id int := get_zero_field_id();
169190
BEGIN
170191

171192
RAISE DEBUG 'Adding column % for zkapp_states', col_name;
172193

173194
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
195+
'ALTER TABLE zkapp_states ADD COLUMN IF NOT EXISTS %I INT DEFAULT %s NOT NULL REFERENCES zkapp_field(id)',
196+
col_name,
197+
default_id
176198
);
177199

178200
RAISE DEBUG 'Added column % for zkapp_states', col_name;
@@ -184,6 +206,14 @@ EXCEPTION
184206
END
185207
$$ LANGUAGE plpgsql;
186208

209+
DO $$
210+
DECLARE
211+
default_id int := get_zero_field_id();
212+
BEGIN
213+
RAISE NOTICE 'Zero field in table zkapp_field is of id = %', default_id;
214+
END
215+
$$;
216+
187217
SELECT add_zkapp_states_element(8);
188218
SELECT add_zkapp_states_element(9);
189219
SELECT add_zkapp_states_element(10);

0 commit comments

Comments
 (0)