Skip to content

Commit

Permalink
feat: merge all migrations to one (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored and flemzord committed May 12, 2023
1 parent c9075c2 commit d0a4462
Show file tree
Hide file tree
Showing 30 changed files with 83 additions and 1,734 deletions.
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/formancehq/ledger/cmd/internal"
"github.com/formancehq/ledger/pkg/storage/sqlstorage"
_ "github.com/formancehq/ledger/pkg/storage/sqlstorage/ledger/migrates/9-add-pre-post-volumes"
"github.com/formancehq/stack/libs/go-libs/otlp/otlpmetrics"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/publish"
Expand Down
145 changes: 83 additions & 62 deletions pkg/storage/sqlstorage/ledger/migrates/0-init-schema/postgres.sql
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 pkg/storage/sqlstorage/ledger/migrates/0-init-schema/sqlite.sql

This file was deleted.

Loading

0 comments on commit d0a4462

Please sign in to comment.