Skip to content

Commit

Permalink
db: Drop constraint on lovelace database type
Browse files Browse the repository at this point in the history
This is a workaround fix for:

   #351

This fix will not require the database to be re-synced, but will result
in some bad values being display as negative value.
  • Loading branch information
erikd committed Oct 20, 2020
1 parent 1c61b97 commit 70b7993
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions schema/migration-1-0004-20201020.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Hand written migration to create the custom types with 'DOMAIN' statements.

CREATE FUNCTION migrate() RETURNS void AS $$

DECLARE
next_version int;

BEGIN
SELECT stage_one + 1 INTO next_version FROM "schema_version";
IF next_version = 1 THEN
EXECUTE 'ALTER DOMAIN lovelace DROP CONSTRAINT lovelace_check; ';
EXECUTE 'CREATE DOMAIN txindex AS smallint CHECK (VALUE >= 0 AND VALUE < 1024);';
EXECUTE 'CREATE DOMAIN uinteger AS integer CHECK (VALUE >= 0);';

-- Blocks, transactions and merkel roots use a 32 byte hash.
EXECUTE 'CREATE DOMAIN hash32type AS bytea CHECK (octet_length (VALUE) = 32);';

-- Addresses use a 28 byte hash (as do StakeholdIds).
EXECUTE 'CREATE DOMAIN hash28type AS bytea CHECK (octet_length (VALUE) = 28);';

-- Stake addresses are a 28 byte hash prepended with a byte describing the address.
EXECUTE 'CREATE DOMAIN addr29type AS bytea CHECK (octet_length (VALUE) = 29);';

-- 'maxBound :: Word128' as a decimal has 39 digits, so we only need to check that it
-- is positive.
EXECUTE 'CREATE DOMAIN word128type AS numeric (38, 0) CHECK (VALUE >= 0);';

-- 'maxBound :: Word64' as a decimal has 20 digits but not all 20 digit values are less than
-- 'maxBound :: Word64'.
EXECUTE 'CREATE DOMAIN word64type AS numeric (20, 0) CHECK (VALUE >= 0 AND VALUE <= 18446744073709551615);';

UPDATE "schema_version" SET stage_one = 1;
RAISE NOTICE 'DB has been migrated to stage_one version %', next_version;
END IF;
END;

$$ LANGUAGE plpgsql;

SELECT migrate();

DROP FUNCTION migrate();

0 comments on commit 70b7993

Please sign in to comment.