-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge all migrations to one (#225)
- Loading branch information
1 parent
c9075c2
commit d0a4462
Showing
30 changed files
with
83 additions
and
1,734 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
145 changes: 83 additions & 62 deletions
145
pkg/storage/sqlstorage/ledger/migrates/0-init-schema/postgres.sql
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 |
---|---|---|
@@ -1,89 +1,110 @@ | ||
--statement | ||
CREATE SCHEMA IF NOT EXISTS "VAR_LEDGER_NAME"; | ||
|
||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".migrations ( | ||
"version" varchar, | ||
"date" varchar, | ||
CREATE FUNCTION "VAR_LEDGER_NAME".meta_compare(metadata jsonb, value boolean, VARIADIC path text[]) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ BEGIN return jsonb_extract_path(metadata, variadic path)::bool = value::bool; EXCEPTION WHEN others THEN RAISE INFO 'Error Name: %', SQLERRM; RAISE INFO 'Error State: %', SQLSTATE; RETURN false; END $$; | ||
|
||
UNIQUE("version") | ||
); | ||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".transactions ( | ||
"id" bigint, | ||
"timestamp" varchar, | ||
"reference" varchar, | ||
"hash" varchar, | ||
CREATE FUNCTION "VAR_LEDGER_NAME".meta_compare(metadata jsonb, value numeric, VARIADIC path text[]) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ BEGIN return jsonb_extract_path(metadata, variadic path)::numeric = value::numeric; EXCEPTION WHEN others THEN RAISE INFO 'Error Name: %', SQLERRM; RAISE INFO 'Error State: %', SQLSTATE; RETURN false; END $$; | ||
|
||
UNIQUE("id"), | ||
UNIQUE("reference") | ||
); | ||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".postings ( | ||
"id" smallint, | ||
"txid" bigint, | ||
"source" varchar, | ||
"destination" varchar, | ||
"amount" bigint, | ||
"asset" varchar, | ||
|
||
UNIQUE("id", "txid") | ||
CREATE FUNCTION "VAR_LEDGER_NAME".meta_compare(metadata jsonb, value character varying, VARIADIC path text[]) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ BEGIN return jsonb_extract_path_text(metadata, variadic path)::varchar = value::varchar; EXCEPTION WHEN others THEN RAISE INFO 'Error Name: %', SQLERRM; RAISE INFO 'Error State: %', SQLSTATE; RETURN false; END $$; | ||
|
||
--statement | ||
CREATE FUNCTION "VAR_LEDGER_NAME".use_account_as_destination(postings jsonb, account character varying) RETURNS boolean | ||
LANGUAGE sql | ||
AS $_$ select bool_or(v.value::bool) from ( select jsonb_extract_path_text(jsonb_array_elements(postings), 'destination') ~ ('^' || account || '$') as value) as v; $_$; | ||
|
||
--statement | ||
CREATE FUNCTION "VAR_LEDGER_NAME".use_account_as_source(postings jsonb, account character varying) RETURNS boolean | ||
LANGUAGE sql | ||
AS $_$ select bool_or(v.value::bool) from ( select jsonb_extract_path_text(jsonb_array_elements(postings), 'source') ~ ('^' || account || '$') as value) as v; $_$; | ||
|
||
--statement | ||
CREATE FUNCTION "VAR_LEDGER_NAME".use_account(postings jsonb, account character varying) RETURNS boolean | ||
LANGUAGE sql | ||
AS $$ SELECT bool_or(v.value) from ( SELECT "VAR_LEDGER_NAME".use_account_as_source(postings, account) AS value UNION SELECT "VAR_LEDGER_NAME".use_account_as_destination(postings, account) AS value ) v $$; | ||
|
||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".accounts ( | ||
address character varying NOT NULL, | ||
metadata jsonb DEFAULT '{}'::jsonb, | ||
|
||
unique(address) | ||
); | ||
|
||
--statement | ||
CREATE INDEX IF NOT EXISTS p_c0 ON "VAR_LEDGER_NAME".postings ( | ||
"txid" DESC, | ||
"source", | ||
"destination" | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".logs_ingestion ( | ||
onerow_id boolean DEFAULT true NOT NULL, | ||
log_id bigint, | ||
|
||
primary key (onerow_id) | ||
); | ||
|
||
--statement | ||
CREATE INDEX IF NOT EXISTS posting_txid ON "VAR_LEDGER_NAME".postings ( | ||
"txid" DESC | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".logs_v2 ( | ||
id bigint, | ||
type smallint, | ||
hash character varying(256), | ||
date timestamp with time zone, | ||
data bytea, | ||
reference text, | ||
|
||
unique(id) | ||
); | ||
|
||
--statement | ||
CREATE INDEX IF NOT EXISTS posting_source ON "VAR_LEDGER_NAME".postings ( | ||
"source" | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".migrations ( | ||
version character varying, | ||
date character varying, | ||
|
||
unique(version) | ||
); | ||
|
||
--statement | ||
CREATE INDEX IF NOT EXISTS posting_destination ON "VAR_LEDGER_NAME".postings ( | ||
"destination" | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".postings ( | ||
txid uuid, | ||
posting_index integer, | ||
source jsonb, | ||
destination jsonb | ||
); | ||
|
||
--statement | ||
CREATE INDEX IF NOT EXISTS posting_asset ON "VAR_LEDGER_NAME".postings ( | ||
"asset" | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".transactions ( | ||
id uuid unique, | ||
"timestamp" timestamp with time zone, | ||
reference character varying unique, | ||
hash character varying, | ||
postings jsonb, | ||
metadata jsonb DEFAULT '{}'::jsonb, | ||
pre_commit_volumes jsonb, | ||
post_commit_volumes jsonb | ||
); | ||
|
||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".metadata ( | ||
"meta_id" bigint, | ||
"meta_target_type" varchar NOT NULL CHECK (meta_target_type <> ''), | ||
"meta_target_id" varchar NOT NULL CHECK (meta_target_id <> ''), | ||
"meta_key" varchar NOT NULL CHECK (meta_key <> ''), | ||
"meta_value" varchar, | ||
"timestamp" varchar NOT NULL CHECK (timestamp <> ''), | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".volumes ( | ||
account character varying, | ||
asset character varying, | ||
input numeric, | ||
output numeric, | ||
|
||
UNIQUE("meta_id") | ||
unique(account, asset) | ||
); | ||
|
||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".contract ( | ||
"contract_id" integer, | ||
"contract_account" varchar, | ||
"contract_expr" varchar, | ||
CREATE INDEX IF NOT EXISTS postings_addresses ON "VAR_LEDGER_NAME".transactions USING gin (postings); | ||
|
||
UNIQUE("contract_id") | ||
); | ||
--statement | ||
CREATE TABLE IF NOT EXISTS "VAR_LEDGER_NAME".mapping ( | ||
"mapping_id" varchar, | ||
"mapping" varchar, | ||
CREATE INDEX IF NOT EXISTS postings_dest ON "VAR_LEDGER_NAME".postings USING gin (destination); | ||
|
||
UNIQUE("mapping_id") | ||
); | ||
--statement | ||
CREATE INDEX IF NOT EXISTS m_i0 ON "VAR_LEDGER_NAME".metadata ( | ||
"meta_target_type", | ||
"meta_target_id" | ||
); | ||
CREATE INDEX IF NOT EXISTS postings_src ON "VAR_LEDGER_NAME".postings USING gin (source); | ||
|
||
--statement | ||
CREATE OR REPLACE VIEW "VAR_LEDGER_NAME".addresses AS SELECT "address" FROM ( | ||
SELECT source as address FROM "VAR_LEDGER_NAME".postings GROUP BY source | ||
UNION | ||
SELECT destination as address FROM "VAR_LEDGER_NAME".postings GROUP BY destination | ||
) addr_agg GROUP BY "address"; | ||
CREATE INDEX IF NOT EXISTS postings_txid ON "VAR_LEDGER_NAME".postings USING btree (txid); |
75 changes: 0 additions & 75 deletions
75
pkg/storage/sqlstorage/ledger/migrates/0-init-schema/sqlite.sql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.