-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement script-based migrations (#139)
This PR implements the second and last step of the transition to script based migrations (#101). After this PR, new CW-D database migrations can be implemented by creating new scripts in the `haskell-src/db-schema/migrations` folder. The scripts in that folder must be named as `1.2.3.4_somename.sql`. This file names correspond to the version components `[1,2,3,4]` along with the step name `somename.sql`. The version components can contain an arbitrary number of elements, so `1.2.3_othername.sql` is also valid. The migration logic implemented by this PR aims to be fairly conservative in that it expects the existing migrations to be a perfect prefix of the incoming migrations with the correct order. The order of the migrations are defined by the version components. The condition that the existing migrations need to be a prefix all the desired migrations means that once a set of migrations are run, we can only append new migrations and those migrations have to have version components that are bigger than the existing migrations. The reason why we're being conservative like this is to avoid very subtle issues that occasionally arise due to migrations running in different orders in different deployments. It's also worth noting that the `--migrations-folder` introduced by this PR is optional and when that argument is not provided, CW-D uses the set of migrations that get embedded into the binary from the repository during compilation. The purpose is to avoid increasing the operational complexity of running CW-D from a compiled binary. The set of migrations associated with a CW-D release are tightly coupled with the Haskell code that comes with it anyway. Another point worth noting is that this migrations workflow also allows CW-D operators to interleave their own migrations with the official migrations that come with CW-D. If the operator of a particular CW-D node wants to include additional migrations, they can do so by maintaining a `migrations` folder of their own and including the official CW-D migrations side by side with their own migrations. In this setup, they need to name their own migration scripts to have version numbers that are compatible with this migration workflow. Resolves #101 * Implement script-based migrations * Return the migration steps in matchRecursive * Preorder steps and detect duplicates * Fix warnings * Base64 encode the migration checksum * Simplify matchSteps
- Loading branch information
Showing
11 changed files
with
406 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
CREATE TABLE blocks ( | ||
chainid bigint NOT NULL, | ||
creationtime timestamp with time zone NOT NULL, | ||
epoch timestamp with time zone NOT NULL, | ||
flags numeric(20,0) NOT NULL, | ||
hash character varying NOT NULL, | ||
height bigint NOT NULL, | ||
miner character varying NOT NULL, | ||
nonce numeric(20,0) NOT NULL, | ||
parent character varying NOT NULL, | ||
payload character varying NOT NULL, | ||
powhash character varying NOT NULL, | ||
predicate character varying NOT NULL, | ||
target numeric(80,0) NOT NULL, | ||
weight numeric(80,0) NOT NULL | ||
); | ||
|
||
ALTER TABLE ONLY blocks | ||
ADD CONSTRAINT blocks_pkey PRIMARY KEY (hash); | ||
|
||
CREATE TABLE events ( | ||
block character varying NOT NULL, | ||
chainid bigint NOT NULL, | ||
height bigint NOT NULL, | ||
idx bigint NOT NULL, | ||
module character varying NOT NULL, | ||
modulehash character varying NOT NULL, | ||
name character varying NOT NULL, | ||
params jsonb NOT NULL, | ||
paramtext character varying NOT NULL, | ||
qualname character varying NOT NULL, | ||
requestkey character varying NOT NULL | ||
); | ||
|
||
ALTER TABLE ONLY events | ||
ADD CONSTRAINT events_pkey PRIMARY KEY (block, idx, requestkey); | ||
|
||
ALTER TABLE ONLY events | ||
ADD CONSTRAINT events_block_fkey FOREIGN KEY (block) REFERENCES blocks(hash); | ||
|
||
CREATE INDEX events_height_chainid_idx | ||
ON events | ||
USING btree (height DESC, chainid, idx); | ||
|
||
CREATE INDEX events_height_name_expr_expr1_idx | ||
ON events | ||
USING btree (height DESC, name, ((params ->> 0)), ((params ->> 1))) WHERE ((name)::text = 'TRANSFER'::text); | ||
|
||
CREATE INDEX events_requestkey_idx | ||
ON events | ||
USING btree (requestkey); | ||
|
||
CREATE TABLE minerkeys ( | ||
block character varying NOT NULL, | ||
key character varying NOT NULL | ||
); | ||
|
||
ALTER TABLE ONLY minerkeys | ||
ADD CONSTRAINT minerkeys_pkey PRIMARY KEY (block, key); | ||
|
||
ALTER TABLE ONLY minerkeys | ||
ADD CONSTRAINT minerkeys_block_fkey FOREIGN KEY (block) REFERENCES blocks(hash); | ||
|
||
|
||
CREATE TABLE signers ( | ||
addr character varying, | ||
caps jsonb NOT NULL, | ||
idx integer NOT NULL, | ||
pubkey character varying NOT NULL, | ||
requestkey character varying NOT NULL, | ||
scheme character varying, | ||
sig character varying NOT NULL | ||
); | ||
|
||
ALTER TABLE ONLY signers | ||
ADD CONSTRAINT signers_pkey PRIMARY KEY (idx, requestkey); | ||
|
||
|
||
CREATE TABLE transactions ( | ||
badresult jsonb, | ||
block character varying NOT NULL, | ||
chainid bigint NOT NULL, | ||
code character varying, | ||
continuation jsonb, | ||
creationtime timestamp with time zone NOT NULL, | ||
data jsonb, | ||
gas bigint NOT NULL, | ||
gaslimit bigint NOT NULL, | ||
gasprice double precision NOT NULL, | ||
goodresult jsonb, | ||
height bigint NOT NULL, | ||
logs character varying, | ||
metadata jsonb, | ||
nonce character varying NOT NULL, | ||
num_events bigint, | ||
pactid character varying, | ||
proof character varying, | ||
requestkey character varying NOT NULL, | ||
rollback boolean, | ||
sender character varying NOT NULL, | ||
step bigint, | ||
ttl bigint NOT NULL, | ||
txid bigint | ||
); | ||
|
||
ALTER TABLE ONLY transactions | ||
ADD CONSTRAINT transactions_pkey PRIMARY KEY (block, requestkey); | ||
|
||
ALTER TABLE ONLY transactions | ||
ADD CONSTRAINT transactions_block_fkey FOREIGN KEY (block) REFERENCES blocks(hash); | ||
|
||
CREATE INDEX transactions_height_idx | ||
ON transactions | ||
USING btree (height); | ||
|
||
CREATE INDEX transactions_requestkey_idx | ||
ON transactions | ||
USING btree (requestkey); | ||
|
||
|
||
CREATE TABLE transfers ( | ||
amount numeric NOT NULL, | ||
block character varying NOT NULL, | ||
chainid bigint NOT NULL, | ||
from_acct character varying NOT NULL, | ||
height bigint NOT NULL, | ||
idx bigint NOT NULL, | ||
modulehash character varying NOT NULL, | ||
modulename character varying NOT NULL, | ||
requestkey character varying NOT NULL, | ||
to_acct character varying NOT NULL | ||
); | ||
|
||
ALTER TABLE ONLY transfers | ||
ADD CONSTRAINT transfers_pkey PRIMARY KEY (block, chainid, idx, modulehash, requestkey); | ||
|
||
CREATE INDEX transfers_from_acct_height_idx | ||
ON transfers | ||
USING btree (from_acct, height DESC, idx); | ||
|
||
|
||
CREATE INDEX transfers_to_acct_height_idx_idx | ||
ON transfers | ||
USING btree (to_acct, height DESC, idx); | ||
|
||
ALTER TABLE ONLY transfers | ||
ADD CONSTRAINT transfers_block_fkey FOREIGN KEY (block) REFERENCES blocks(hash); | ||
|
||
|
||
CREATE TABLE schema_migrations ( | ||
filename character varying(512) NOT NULL, | ||
checksum character varying(32) NOT NULL, | ||
executed_at timestamp without time zone DEFAULT now() NOT NULL | ||
); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.