From 1c2a8ff74b01a4bc7cb430e87363f6b5de594c26 Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Thu, 21 Mar 2024 21:01:14 +0400 Subject: [PATCH 1/6] Add everything Signed-off-by: Shota Jolbordi --- .../V10__atala_object_tx_submission.sql | 15 - .../db/migration/V11__revoked_credentials.sql | 16 - .../V12__drop_atalaobjects_blockhash.sql | 11 - ...13__add_ledger_data_to_protocol_events.sql | 102 ------ ...V14__atala_object_tx_submissions_index.sql | 3 - .../V15__delete_legacy_credentials_table.sql | 3 - .../db/migration/V16__drop_blocks_table.sql | 4 - .../migration/V17__add_operation_statuses.sql | 16 - .../migration/V18__alter_public_key_table.sql | 5 - .../db/migration/V1__create_tables.sql | 312 +++++++++++++++++- .../migration/V20__alter_public_key_table.sql | 13 - ...1__alter_atala_objects_add_received_at.sql | 12 - .../migration/V22__alter_type_key_usage.sql | 1 - .../V23__add_atala_object_status.sql | 10 - .../V24__add_atala_object_id_indexes.sql | 4 - .../V25__add_protocol_versions_table.sql | 16 - .../migration/V26__add_trusted_proposers.sql | 10 - ...27__atala_operation_status_description.sql | 1 - ...ala_object_status_add_scheduled_status.sql | 1 - .../migration/V29__drop_trusted_proposers.sql | 4 - .../db/migration/V2__atala_object.sql | 24 -- .../V30__add_metrics_counters_db.sql | 4 - .../migration/V31__add_did_request_nonces.sql | 9 - ...d_services_and_service_endpoints_table.sql | 41 --- .../migration/V33__alter_type_key_usage.sql | 4 - ...rvice_endpoints_and_add_it_to_services.sql | 10 - .../db/migration/V35__add_contexts_table.sql | 20 -- .../resources/db/migration/V3__did_tables.sql | 39 --- .../db/migration/V4__credentials_table.sql | 27 -- .../db/migration/V5__key_values_table.sql | 5 - .../V6__atala_objects_add_tx_info.sql | 19 -- ...la_objects_drop_sequence_number_unique.sql | 7 - .../db/migration/V8__atala_object_tx.sql | 31 -- .../V9__credential_batches_table.sql | 31 -- node/src/main/scala/db/migration/.gitkeep | 0 .../scala/db/migration/V19__public_keys.scala | 56 ---- .../iohk/atala/prism/node/migrations/.gitkeep | 0 .../node/migrations/V19MigrationSpec.scala | 82 ----- 38 files changed, 295 insertions(+), 673 deletions(-) delete mode 100644 node/src/main/resources/db/migration/V10__atala_object_tx_submission.sql delete mode 100644 node/src/main/resources/db/migration/V11__revoked_credentials.sql delete mode 100644 node/src/main/resources/db/migration/V12__drop_atalaobjects_blockhash.sql delete mode 100644 node/src/main/resources/db/migration/V13__add_ledger_data_to_protocol_events.sql delete mode 100644 node/src/main/resources/db/migration/V14__atala_object_tx_submissions_index.sql delete mode 100644 node/src/main/resources/db/migration/V15__delete_legacy_credentials_table.sql delete mode 100644 node/src/main/resources/db/migration/V16__drop_blocks_table.sql delete mode 100644 node/src/main/resources/db/migration/V17__add_operation_statuses.sql delete mode 100644 node/src/main/resources/db/migration/V18__alter_public_key_table.sql delete mode 100644 node/src/main/resources/db/migration/V20__alter_public_key_table.sql delete mode 100644 node/src/main/resources/db/migration/V21__alter_atala_objects_add_received_at.sql delete mode 100644 node/src/main/resources/db/migration/V22__alter_type_key_usage.sql delete mode 100644 node/src/main/resources/db/migration/V23__add_atala_object_status.sql delete mode 100644 node/src/main/resources/db/migration/V24__add_atala_object_id_indexes.sql delete mode 100644 node/src/main/resources/db/migration/V25__add_protocol_versions_table.sql delete mode 100644 node/src/main/resources/db/migration/V26__add_trusted_proposers.sql delete mode 100644 node/src/main/resources/db/migration/V27__atala_operation_status_description.sql delete mode 100644 node/src/main/resources/db/migration/V28__atala_object_status_add_scheduled_status.sql delete mode 100644 node/src/main/resources/db/migration/V29__drop_trusted_proposers.sql delete mode 100644 node/src/main/resources/db/migration/V2__atala_object.sql delete mode 100644 node/src/main/resources/db/migration/V30__add_metrics_counters_db.sql delete mode 100644 node/src/main/resources/db/migration/V31__add_did_request_nonces.sql delete mode 100644 node/src/main/resources/db/migration/V32__add_services_and_service_endpoints_table.sql delete mode 100644 node/src/main/resources/db/migration/V33__alter_type_key_usage.sql delete mode 100644 node/src/main/resources/db/migration/V34__drop_service_endpoints_and_add_it_to_services.sql delete mode 100644 node/src/main/resources/db/migration/V35__add_contexts_table.sql delete mode 100644 node/src/main/resources/db/migration/V3__did_tables.sql delete mode 100644 node/src/main/resources/db/migration/V4__credentials_table.sql delete mode 100644 node/src/main/resources/db/migration/V5__key_values_table.sql delete mode 100644 node/src/main/resources/db/migration/V6__atala_objects_add_tx_info.sql delete mode 100644 node/src/main/resources/db/migration/V7__atala_objects_drop_sequence_number_unique.sql delete mode 100644 node/src/main/resources/db/migration/V8__atala_object_tx.sql delete mode 100644 node/src/main/resources/db/migration/V9__credential_batches_table.sql create mode 100644 node/src/main/scala/db/migration/.gitkeep delete mode 100644 node/src/main/scala/db/migration/V19__public_keys.scala create mode 100644 node/src/test/scala/io/iohk/atala/prism/node/migrations/.gitkeep delete mode 100644 node/src/test/scala/io/iohk/atala/prism/node/migrations/V19MigrationSpec.scala diff --git a/node/src/main/resources/db/migration/V10__atala_object_tx_submission.sql b/node/src/main/resources/db/migration/V10__atala_object_tx_submission.sql deleted file mode 100644 index 0681313281..0000000000 --- a/node/src/main/resources/db/migration/V10__atala_object_tx_submission.sql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TYPE ATALA_OBJECT_TRANSACTION_STATUS AS ENUM('PENDING', 'DELETED', 'IN_LEDGER'); - --- Create table to record all transaction submissions -CREATE TABLE atala_object_tx_submissions ( - atala_object_id ATALA_OBJECT_ID NOT NULL, - ledger VARCHAR(32) NOT NULL, - transaction_id TRANSACTION_ID NOT NULL, - submission_timestamp TIMESTAMPTZ NOT NULL, - status ATALA_OBJECT_TRANSACTION_STATUS NOT NULL, - - CONSTRAINT atala_object_tx_submissions_pk PRIMARY KEY (ledger, transaction_id), - CONSTRAINT atala_object_tx_submissions_atala_object_id_fk - FOREIGN KEY (atala_object_id) - REFERENCES atala_objects (atala_object_id) -); diff --git a/node/src/main/resources/db/migration/V11__revoked_credentials.sql b/node/src/main/resources/db/migration/V11__revoked_credentials.sql deleted file mode 100644 index 8d73cbe3aa..0000000000 --- a/node/src/main/resources/db/migration/V11__revoked_credentials.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE DOMAIN CREDENTIAL_HASH AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE revoked_credentials( - batch_id ID_TYPE NOT NULL, - credential_id CREDENTIAL_HASH NOT NULL, - revoked_on TIMESTAMPTZ NOT NULL, - -- Atala Block Sequence Number (absn) of the operation that revoked the batch - revoked_on_absn INTEGER NOT NULL, - -- Operation Sequence Number (osn) of the operation that revoked the batch - revoked_on_osn INTEGER NOT NULL, - CONSTRAINT revoked_credentials_pk PRIMARY KEY (batch_id, credential_id), - CONSTRAINT revoked_credentials_batch_id_fk FOREIGN KEY (batch_id) REFERENCES credential_batches (batch_id) -); \ No newline at end of file diff --git a/node/src/main/resources/db/migration/V12__drop_atalaobjects_blockhash.sql b/node/src/main/resources/db/migration/V12__drop_atalaobjects_blockhash.sql deleted file mode 100644 index 6d540621c5..0000000000 --- a/node/src/main/resources/db/migration/V12__drop_atalaobjects_blockhash.sql +++ /dev/null @@ -1,11 +0,0 @@ --- Set a default value for objects without content --- (this may happen only in a local dev env, so it's fine to corrupt it) -UPDATE atala_objects - SET object_content = '' - WHERE object_content IS NULL; - -ALTER TABLE atala_objects - -- Delete atala_block_hash as it's no longer used (in favor of object_content) - DROP COLUMN atala_block_hash, - -- Make object_content required - ALTER COLUMN object_content SET NOT NULL; diff --git a/node/src/main/resources/db/migration/V13__add_ledger_data_to_protocol_events.sql b/node/src/main/resources/db/migration/V13__add_ledger_data_to_protocol_events.sql deleted file mode 100644 index 373562e21d..0000000000 --- a/node/src/main/resources/db/migration/V13__add_ledger_data_to_protocol_events.sql +++ /dev/null @@ -1,102 +0,0 @@ --- As part of ATA-4153, we intend to store in the database the information of the --- underlying blockchain transaction that carried each operation. Before this story, --- we were only storing timestamp information associated to the transaction. Now, we --- will also add the transaction id and the ledger where the transaction belongs to. - --- For legacy purpose, we need to add mock data to existing tables --- We will use: --- · the InMemoryLedger, and --- · a proper byte array for transaction id - --- + did_data table -ALTER TABLE did_data - ADD COLUMN transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL, - -- we will additionally add timestamp information to this table - ADD COLUMN published_on TIMESTAMPTZ NULL, - ADD COLUMN published_on_absn INTEGER NULL, - ADD COLUMN published_on_osn INTEGER NULL; - -UPDATE did_data - SET transaction_id = last_operation::TRANSACTION_ID, -- we can take the bytes from this column - ledger = 'InMemory', - published_on = now(), - published_on_absn = 1, - published_on_osn = 1; - -ALTER TABLE did_data - ALTER COLUMN transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL, - ALTER COLUMN published_on SET NOT NULL, - ALTER COLUMN published_on_absn SET NOT NULL, - ALTER COLUMN published_on_osn SET NOT NULL; - - --- + credentials table -ALTER TABLE credentials - ADD COLUMN issued_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN revoked_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL; -- we assume the same ledger for issuance and revocation - -UPDATE credentials - SET issued_on_transaction_id = last_operation::TRANSACTION_ID, - ledger = 'InMemory'; - -ALTER TABLE credentials - ALTER COLUMN issued_on_transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL; - --- + credential_batches table -ALTER TABLE credential_batches - ADD COLUMN issued_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN revoked_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL; - -UPDATE credential_batches - SET issued_on_transaction_id = last_operation::TRANSACTION_ID, - ledger = 'InMemory'; - -ALTER TABLE credential_batches - ALTER COLUMN issued_on_transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL; - --- + revoked_credentials table -ALTER TABLE revoked_credentials - ADD COLUMN transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL; - -UPDATE revoked_credentials - SET transaction_id = credential_id::TRANSACTION_ID, - ledger = 'InMemory'; - -ALTER TABLE revoked_credentials - ALTER COLUMN transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL; - --- + public_keys table --- in this table, there is no BYTEA value we could use for default transaction_id, --- we neither have a hex encoded BYTEA we could use, we will use the function found --- here: https://dba.stackexchange.com/questions/22512/how-can-i-generate-a-random-bytea -create function random_bytea(p_length in integer) returns bytea language plpgsql as $$ -declare - o bytea := ''; -begin - for i in 1..p_length loop - o := o||decode(lpad(to_hex(width_bucket(random(), 0, 1, 256)-1),2,'0'), 'hex'); - end loop; - return o; -end;$$; --- it is not the most efficient but it will be enough for our case - -ALTER TABLE public_keys - ADD COLUMN added_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN revoked_on_transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL; -- we assume the same ledger for key addition and revocation - -UPDATE public_keys - SET added_on_transaction_id = random_bytea(32), - ledger = 'InMemory'; - -ALTER TABLE public_keys - ALTER COLUMN added_on_transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL; diff --git a/node/src/main/resources/db/migration/V14__atala_object_tx_submissions_index.sql b/node/src/main/resources/db/migration/V14__atala_object_tx_submissions_index.sql deleted file mode 100644 index 1250c6d626..0000000000 --- a/node/src/main/resources/db/migration/V14__atala_object_tx_submissions_index.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Speed up queries on atala_object_tx_submissions -CREATE INDEX atala_object_tx_submissions_latest_index ON atala_object_tx_submissions USING BTREE (atala_object_id, submission_timestamp); -CREATE INDEX atala_object_tx_submissions_filter_index ON atala_object_tx_submissions USING BTREE (submission_timestamp, status, ledger); diff --git a/node/src/main/resources/db/migration/V15__delete_legacy_credentials_table.sql b/node/src/main/resources/db/migration/V15__delete_legacy_credentials_table.sql deleted file mode 100644 index dd09dd7bed..0000000000 --- a/node/src/main/resources/db/migration/V15__delete_legacy_credentials_table.sql +++ /dev/null @@ -1,3 +0,0 @@ --- We can now delete the old credentials table - -DROP TABLE credentials; diff --git a/node/src/main/resources/db/migration/V16__drop_blocks_table.sql b/node/src/main/resources/db/migration/V16__drop_blocks_table.sql deleted file mode 100644 index 7f75e1f5eb..0000000000 --- a/node/src/main/resources/db/migration/V16__drop_blocks_table.sql +++ /dev/null @@ -1,4 +0,0 @@ --- ATA-4656: We deleted the code related to the bitcoin network. --- As a consequence, we do not need the "blocks" table anymore - -DROP TABLE blocks; \ No newline at end of file diff --git a/node/src/main/resources/db/migration/V17__add_operation_statuses.sql b/node/src/main/resources/db/migration/V17__add_operation_statuses.sql deleted file mode 100644 index 1206ec1171..0000000000 --- a/node/src/main/resources/db/migration/V17__add_operation_statuses.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TYPE ATALA_OPERATION_STATUS AS ENUM('UNKNOWN', 'RECEIVED', 'APPLIED', 'REJECTED'); -CREATE DOMAIN ATALA_OPERATION_ID AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE atala_operations( - signed_atala_operation_id ATALA_OPERATION_ID NOT NULL, - atala_object_id ATALA_OBJECT_ID NOT NULL, - atala_operation_status ATALA_OPERATION_STATUS NOT NULL, - -- constraints - CONSTRAINT signed_atala_operation_id_pk PRIMARY KEY (signed_atala_operation_id), - CONSTRAINT atala_object_id_fk - FOREIGN KEY (atala_object_id) - REFERENCES atala_objects (atala_object_id) -); diff --git a/node/src/main/resources/db/migration/V18__alter_public_key_table.sql b/node/src/main/resources/db/migration/V18__alter_public_key_table.sql deleted file mode 100644 index 92a6f4b034..0000000000 --- a/node/src/main/resources/db/migration/V18__alter_public_key_table.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE public_keys -ADD COLUMN xCompressed bytea NULL; - -ALTER TABLE public_keys -ADD CONSTRAINT x_compressed_length CHECK (LENGTH(xCompressed) = 33); diff --git a/node/src/main/resources/db/migration/V1__create_tables.sql b/node/src/main/resources/db/migration/V1__create_tables.sql index 6d1df791d0..7a860f058b 100644 --- a/node/src/main/resources/db/migration/V1__create_tables.sql +++ b/node/src/main/resources/db/migration/V1__create_tables.sql @@ -1,24 +1,302 @@ +CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION postgres; -CREATE DOMAIN BLOCKHASH_TYPE AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 +COMMENT ON SCHEMA public IS 'standard public schema'; + + +CREATE DOMAIN public.atala_object_id AS bytea + CONSTRAINT atala_object_id_check CHECK (length(VALUE) = 32); +-- DROP TYPE public.atala_object_status; + +CREATE TYPE public.atala_object_status AS ENUM ( + 'SCHEDULED', + 'PENDING', + 'MERGED', + 'PROCESSED'); + + +CREATE TYPE public.atala_object_transaction_status AS ENUM ( + 'PENDING', + 'DELETED', + 'IN_LEDGER'); + + +CREATE DOMAIN public.atala_operation_id AS bytea + CONSTRAINT atala_operation_id_check CHECK (length(VALUE) = 32); + +CREATE TYPE public.atala_operation_status AS ENUM ( + 'UNKNOWN', + 'RECEIVED', + 'APPLIED', + 'REJECTED'); + + +CREATE DOMAIN public.block_hash_type AS bytea + CONSTRAINT block_hash_type_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.block_no AS integer + CONSTRAINT block_no_check CHECK (VALUE >= 0); + +CREATE DOMAIN public.blockhash_type AS bytea + CONSTRAINT blockhash_type_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.content_hash AS bytea + CONSTRAINT content_hash_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.credential_hash AS bytea + CONSTRAINT credential_hash_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.did AS text + COLLATE "default" + CONSTRAINT did_check CHECK (VALUE ~ '^did:[a-z0-9]+:[a-zA-Z0-9._-]*(:[a-zA-Z0-9._-]*)*$'::text); + +CREATE DOMAIN public.id_type AS text + COLLATE "default" + CONSTRAINT id_type_check CHECK (VALUE ~ '^[0-9a-f]{64}$'::text); + +CREATE TYPE public.key_usage AS ENUM ( + 'MASTER_KEY', + 'ISSUING_KEY', + 'KEY_AGREEMENT_KEY', + 'AUTHENTICATION_KEY', + 'REVOCATION_KEY', + 'CAPABILITY_INVOCATION_KEY', + 'CAPABILITY_DELEGATION_KEY'); + + +CREATE DOMAIN public.merkle_root AS bytea + CONSTRAINT merkle_root_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.non_negative_int_type AS integer + CONSTRAINT non_negative_int_type_check CHECK (VALUE >= 0); + +CREATE DOMAIN public.operation_hash AS bytea + CONSTRAINT operation_hash_check CHECK (length(VALUE) = 32); + +CREATE DOMAIN public.transaction_id AS bytea + CONSTRAINT transaction_id_check CHECK (length(VALUE) = 32); + +CREATE TABLE public.atala_objects +( + atala_object_id public.atala_object_id NOT NULL, + object_content bytea NOT NULL, + received_at timestamptz NOT NULL, + atala_object_status public.atala_object_status DEFAULT 'PENDING'::atala_object_status NULL, + CONSTRAINT atala_objects_pk PRIMARY KEY (atala_object_id) ); +CREATE INDEX atala_objects_atala_object_status_index ON public.atala_objects USING btree (atala_object_status); +CREATE INDEX atala_objects_received_at ON public.atala_objects USING btree (received_at); + -CREATE DOMAIN NON_NEGATIVE_INT_TYPE AS INT -CHECK ( - VALUE >= 0 +CREATE TABLE public.contexts +( + context_id public.id_type NOT NULL, + did_suffix public.id_type NOT NULL, + context text NOT NULL, + added_on_transaction_id public.transaction_id NOT NULL, + added_on timestamptz NOT NULL, + added_on_absn int4 NOT NULL, + added_on_osn int4 NOT NULL, + revoked_on_transaction_id public.transaction_id NULL, + revoked_on timestamptz NULL, + revoked_on_absn int4 NULL, + revoked_on_osn int4 NULL, + CONSTRAINT contexts_pkey PRIMARY KEY (context_id) ); +CREATE UNIQUE INDEX unique_did_suffix_and_context_string_on_non_revoked ON public.contexts USING btree (did_suffix, context) WHERE (revoked_on IS NULL); -CREATE TABLE blocks( - blockhash BLOCKHASH_TYPE NOT NULL, - previous_blockhash BLOCKHASH_TYPE NULL, - height NON_NEGATIVE_INT_TYPE NOT NULL, - time BIGINT NOT NULL, - -- constraints - CONSTRAINT blocks_blockhash_pk PRIMARY KEY (blockhash), - CONSTRAINT blocks_height_unique UNIQUE (height), - CONSTRAINT blocks_previous_blockhash_fk FOREIGN KEY (previous_blockhash) REFERENCES blocks (blockhash) + +CREATE TABLE public.did_data +( + did_suffix public.id_type NOT NULL, + last_operation public.operation_hash NOT NULL, + transaction_id public.transaction_id NOT NULL, + ledger varchar(32) NOT NULL, + published_on timestamptz NOT NULL, + published_on_absn int4 NOT NULL, + published_on_osn int4 NOT NULL, + CONSTRAINT did_data_pk PRIMARY KEY (did_suffix) ); -CREATE INDEX blocks_time_index ON blocks USING BTREE (time); -CREATE INDEX blocks_previous_blockhash_index ON blocks USING BTREE (previous_blockhash); + + +CREATE TABLE public.did_request_nonces +( + request_nonce bytea NOT NULL, + did public.did NOT NULL, + CONSTRAINT did_request_nonces_pk PRIMARY KEY (request_nonce, did) +); + + +CREATE TABLE public.key_values +( + "key" varchar(64) NOT NULL, + value text NULL, + CONSTRAINT key_values_pkey PRIMARY KEY (key) +); + + + +CREATE TABLE public.metrics_counters +( + counter_name varchar(256) NOT NULL, + counter_value public.non_negative_int_type DEFAULT 0 NOT NULL, + CONSTRAINT metrics_counters_pkey PRIMARY KEY (counter_name) +); + + +CREATE TABLE public.protocol_versions +( + major_version public.non_negative_int_type NOT NULL, + minor_version public.non_negative_int_type NOT NULL, + version_name varchar(256) NULL, + effective_since public.block_no NOT NULL, + published_in public.transaction_id NOT NULL, + is_effective bool NOT NULL, + proposer_did public.id_type NOT NULL, + CONSTRAINT protocol_version_pk PRIMARY KEY (major_version, minor_version) +); + + + +CREATE TABLE public.public_keys +( + did_suffix public.id_type NOT NULL, + key_id text NOT NULL, + key_usage public.key_usage NOT NULL, + curve text NOT NULL, + added_on timestamptz NOT NULL, + added_on_absn int4 NOT NULL, + added_on_osn int4 NOT NULL, + revoked_on timestamptz NULL, + revoked_on_absn int4 NULL, + revoked_on_osn int4 NULL, + added_on_transaction_id public.transaction_id NOT NULL, + revoked_on_transaction_id public.transaction_id NULL, + ledger varchar(32) NOT NULL, + compressed bytea NOT NULL, + CONSTRAINT public_keys_pk PRIMARY KEY (did_suffix, key_id), + CONSTRAINT x_compressed_length CHECK ((length(compressed) = 33)) +); + + + +CREATE TABLE public.atala_object_tx_submissions +( + atala_object_id public.atala_object_id NOT NULL, + ledger varchar(32) NOT NULL, + transaction_id public.transaction_id NOT NULL, + submission_timestamp timestamptz NOT NULL, + status public.atala_object_transaction_status NOT NULL, + CONSTRAINT atala_object_tx_submissions_pk PRIMARY KEY (ledger, transaction_id), + CONSTRAINT atala_object_tx_submissions_atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) +); +CREATE INDEX atala_object_tx_submissions_atala_object_id_index ON public.atala_object_tx_submissions USING hash (atala_object_id); +CREATE INDEX atala_object_tx_submissions_filter_index ON public.atala_object_tx_submissions USING btree (submission_timestamp, status, ledger); +CREATE INDEX atala_object_tx_submissions_latest_index ON public.atala_object_tx_submissions USING btree (atala_object_id, submission_timestamp); + + + +CREATE TABLE public.atala_object_txs +( + atala_object_id public.atala_object_id NOT NULL, + ledger varchar(32) NOT NULL, + block_number int4 NOT NULL, + block_timestamp timestamptz NOT NULL, + block_index int4 NOT NULL, + transaction_id public.transaction_id NOT NULL, + CONSTRAINT atala_object_txs_pk PRIMARY KEY (atala_object_id), + CONSTRAINT atala_object_txs_atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) +); +CREATE INDEX atala_object_txs_atala_object_id_index ON public.atala_object_txs USING hash (atala_object_id); + + +CREATE TABLE public.atala_operations +( + signed_atala_operation_id public.atala_operation_id NOT NULL, + atala_object_id public.atala_object_id NOT NULL, + atala_operation_status public.atala_operation_status NOT NULL, + status_details varchar(256) DEFAULT ''::character varying NULL, + CONSTRAINT signed_atala_operation_id_pk PRIMARY KEY (signed_atala_operation_id), + CONSTRAINT atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) +); + + + +CREATE TABLE public.credential_batches +( + batch_id public.id_type NOT NULL, + last_operation public.operation_hash NOT NULL, + issuer_did_suffix public.id_type NOT NULL, + merkle_root public.merkle_root NOT NULL, + issued_on timestamptz NOT NULL, + issued_on_absn int4 NOT NULL, + issued_on_osn int4 NOT NULL, + revoked_on timestamptz NULL, + revoked_on_absn int4 NULL, + revoked_on_osn int4 NULL, + issued_on_transaction_id public.transaction_id NOT NULL, + revoked_on_transaction_id public.transaction_id NULL, + ledger varchar(32) NOT NULL, + CONSTRAINT credential_batches_pk PRIMARY KEY (batch_id), + CONSTRAINT revoke_on_check CHECK (( + ((revoked_on IS NULL) AND (revoked_on_absn IS NULL) AND (revoked_on_osn IS NULL)) OR + ((revoked_on IS NOT NULL) AND (revoked_on_absn IS NOT NULL) AND (revoked_on_osn IS NOT NULL)))), + CONSTRAINT credential_batches_issuer_did_suffix_fk FOREIGN KEY (issuer_did_suffix) REFERENCES public.did_data (did_suffix) +); +CREATE INDEX credential_batches_issuer_did_suffix_index ON public.credential_batches USING btree (issuer_did_suffix); + + + +CREATE TABLE public.revoked_credentials +( + batch_id public.id_type NOT NULL, + credential_id public.credential_hash NOT NULL, + revoked_on timestamptz NOT NULL, + revoked_on_absn int4 NOT NULL, + revoked_on_osn int4 NOT NULL, + transaction_id public.transaction_id NOT NULL, + ledger varchar(32) NOT NULL, + CONSTRAINT revoked_credentials_pk PRIMARY KEY (batch_id, credential_id), + CONSTRAINT revoked_credentials_batch_id_fk FOREIGN KEY (batch_id) REFERENCES public.credential_batches (batch_id) +); + + + +CREATE TABLE public.services +( + service_id public.id_type NOT NULL, + id text NOT NULL, + did_suffix public.id_type NOT NULL, + "type" text NOT NULL, + added_on_transaction_id public.transaction_id NOT NULL, + added_on timestamptz NOT NULL, + added_on_absn int4 NOT NULL, + added_on_osn int4 NOT NULL, + revoked_on_transaction_id public.transaction_id NULL, + revoked_on timestamptz NULL, + revoked_on_absn int4 NULL, + revoked_on_osn int4 NULL, + ledger varchar(32) NOT NULL, + service_endpoints text NOT NULL, + CONSTRAINT services_pkey PRIMARY KEY (service_id), + CONSTRAINT services_did_suffix_fk FOREIGN KEY (did_suffix) REFERENCES public.did_data (did_suffix) +); +CREATE UNIQUE INDEX unique_did_suffix_and_id_on_non_revoked ON public.services USING btree (did_suffix, id) WHERE (revoked_on IS NULL); + + +CREATE OR REPLACE FUNCTION public.random_bytea(p_length integer) + RETURNS bytea + LANGUAGE plpgsql +AS +$function$ +declare + o bytea := ''; +begin + for i in 1..p_length + loop + o := o || decode(lpad(to_hex(width_bucket(random(), 0, 1, 256) - 1), 2, '0'), 'hex'); + end loop; + return o; +end; +$function$ +; diff --git a/node/src/main/resources/db/migration/V20__alter_public_key_table.sql b/node/src/main/resources/db/migration/V20__alter_public_key_table.sql deleted file mode 100644 index 719a71d534..0000000000 --- a/node/src/main/resources/db/migration/V20__alter_public_key_table.sql +++ /dev/null @@ -1,13 +0,0 @@ -ALTER TABLE public_keys -DROP COLUMN x; - -ALTER TABLE public_keys -DROP COLUMN y; - -ALTER TABLE public_keys -RENAME COLUMN xCompressed -TO compressed; - -ALTER TABLE public_keys -ALTER COLUMN compressed -SET NOT NULL; \ No newline at end of file diff --git a/node/src/main/resources/db/migration/V21__alter_atala_objects_add_received_at.sql b/node/src/main/resources/db/migration/V21__alter_atala_objects_add_received_at.sql deleted file mode 100644 index 0bc59f5225..0000000000 --- a/node/src/main/resources/db/migration/V21__alter_atala_objects_add_received_at.sql +++ /dev/null @@ -1,12 +0,0 @@ -ALTER TABLE atala_objects - ADD COLUMN received_at TIMESTAMPTZ NULL; - --- Set a default value for objects without timestamp -UPDATE atala_objects - SET received_at = NOW(); - -ALTER TABLE atala_objects - -- Set received_at column mandatory - ALTER COLUMN received_at SET NOT NULL; - -CREATE INDEX atala_objects_received_at on atala_objects USING BTREE(received_at); diff --git a/node/src/main/resources/db/migration/V22__alter_type_key_usage.sql b/node/src/main/resources/db/migration/V22__alter_type_key_usage.sql deleted file mode 100644 index cc6b3f4c92..0000000000 --- a/node/src/main/resources/db/migration/V22__alter_type_key_usage.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TYPE KEY_USAGE ADD VALUE 'REVOCATION_KEY'; diff --git a/node/src/main/resources/db/migration/V23__add_atala_object_status.sql b/node/src/main/resources/db/migration/V23__add_atala_object_status.sql deleted file mode 100644 index 086434e52b..0000000000 --- a/node/src/main/resources/db/migration/V23__add_atala_object_status.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TYPE ATALA_OBJECT_STATUS AS ENUM('PENDING', 'MERGED', 'PROCESSED'); - -ALTER TABLE atala_objects - ADD COLUMN atala_object_status ATALA_OBJECT_STATUS DEFAULT 'PENDING'; - -UPDATE atala_objects - SET atala_object_status = 'PROCESSED' - WHERE processed = true; - -ALTER TABLE atala_objects DROP COLUMN processed; diff --git a/node/src/main/resources/db/migration/V24__add_atala_object_id_indexes.sql b/node/src/main/resources/db/migration/V24__add_atala_object_id_indexes.sql deleted file mode 100644 index 5f4e1b0b27..0000000000 --- a/node/src/main/resources/db/migration/V24__add_atala_object_id_indexes.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE INDEX atala_objects_atala_object_status_index ON atala_objects USING BTREE (atala_object_status); - -CREATE INDEX atala_object_tx_submissions_atala_object_id_index ON atala_object_tx_submissions USING HASH (atala_object_id); -CREATE INDEX atala_object_txs_atala_object_id_index ON atala_object_txs USING HASH (atala_object_id); diff --git a/node/src/main/resources/db/migration/V25__add_protocol_versions_table.sql b/node/src/main/resources/db/migration/V25__add_protocol_versions_table.sql deleted file mode 100644 index e62a200cf6..0000000000 --- a/node/src/main/resources/db/migration/V25__add_protocol_versions_table.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE DOMAIN BLOCK_NO AS INT -CHECK ( - VALUE >= 0 -); - -create table protocol_versions( - major_version non_negative_int_type not null, - minor_version non_negative_int_type not null, - version_name varchar(256) null, - effective_since BLOCK_NO not null, - published_in transaction_id not null, - is_effective bool not null, - proposer_did id_type not null, - - CONSTRAINT protocol_version_pk PRIMARY KEY (major_version, minor_version) -); diff --git a/node/src/main/resources/db/migration/V26__add_trusted_proposers.sql b/node/src/main/resources/db/migration/V26__add_trusted_proposers.sql deleted file mode 100644 index 509df7b891..0000000000 --- a/node/src/main/resources/db/migration/V26__add_trusted_proposers.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE trusted_proposers( - did_suffix id_type, - - CONSTRAINT trusted_proposers_pk PRIMARY KEY (did_suffix) -); - -ALTER TABLE protocol_versions - ADD CONSTRAINT proposer_fk - FOREIGN KEY (proposer_did) - REFERENCES trusted_proposers (did_suffix); diff --git a/node/src/main/resources/db/migration/V27__atala_operation_status_description.sql b/node/src/main/resources/db/migration/V27__atala_operation_status_description.sql deleted file mode 100644 index 8ab2ff5aea..0000000000 --- a/node/src/main/resources/db/migration/V27__atala_operation_status_description.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE atala_operations ADD COLUMN status_details VARCHAR(256) DEFAULT ''; diff --git a/node/src/main/resources/db/migration/V28__atala_object_status_add_scheduled_status.sql b/node/src/main/resources/db/migration/V28__atala_object_status_add_scheduled_status.sql deleted file mode 100644 index 29dc2d3cbf..0000000000 --- a/node/src/main/resources/db/migration/V28__atala_object_status_add_scheduled_status.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TYPE ATALA_OBJECT_STATUS ADD VALUE 'SCHEDULED' BEFORE 'PENDING'; \ No newline at end of file diff --git a/node/src/main/resources/db/migration/V29__drop_trusted_proposers.sql b/node/src/main/resources/db/migration/V29__drop_trusted_proposers.sql deleted file mode 100644 index d59a0ef77a..0000000000 --- a/node/src/main/resources/db/migration/V29__drop_trusted_proposers.sql +++ /dev/null @@ -1,4 +0,0 @@ -ALTER TABLE protocol_versions - DROP CONSTRAINT proposer_fk; - -DROP TABLE trusted_proposers; diff --git a/node/src/main/resources/db/migration/V2__atala_object.sql b/node/src/main/resources/db/migration/V2__atala_object.sql deleted file mode 100644 index e7d642b620..0000000000 --- a/node/src/main/resources/db/migration/V2__atala_object.sql +++ /dev/null @@ -1,24 +0,0 @@ -CREATE DOMAIN ATALA_OBJECT_ID AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE DOMAIN BLOCK_HASH_TYPE AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE atala_objects( - atala_object_id ATALA_OBJECT_ID NOT NULL, - sequence_number INTEGER NOT NULL, - object_timestamp TIMESTAMPTZ NOT NULL, - atala_block_hash BLOCK_HASH_TYPE NULL DEFAULT NULL, - object_content BYTEA NULL DEFAULT NULL, - processed BOOLEAN NOT NULL DEFAULT FALSE, - -- constraints - CONSTRAINT atala_objects_pk PRIMARY KEY (atala_object_id), - CONSTRAINT atala_objects_sequence_number_unique UNIQUE (sequence_number), - CONSTRAINT atala_objects_sequence_number_positive CHECK (sequence_number > 0) -); - -CREATE INDEX atala_objects_sequence_number_index on atala_objects USING BTREE(sequence_number); diff --git a/node/src/main/resources/db/migration/V30__add_metrics_counters_db.sql b/node/src/main/resources/db/migration/V30__add_metrics_counters_db.sql deleted file mode 100644 index 8219f3eef2..0000000000 --- a/node/src/main/resources/db/migration/V30__add_metrics_counters_db.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE metrics_counters( - counter_name VARCHAR(256) PRIMARY KEY NOT NULL, - counter_value NON_NEGATIVE_INT_TYPE NOT NULL DEFAULT 0 -); diff --git a/node/src/main/resources/db/migration/V31__add_did_request_nonces.sql b/node/src/main/resources/db/migration/V31__add_did_request_nonces.sql deleted file mode 100644 index 8101dc5417..0000000000 --- a/node/src/main/resources/db/migration/V31__add_did_request_nonces.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE DOMAIN DID AS TEXT CHECK( - VALUE ~ '^did:[a-z0-9]+:[a-zA-Z0-9._-]*(:[a-zA-Z0-9._-]*)*$' -); - -CREATE TABLE did_request_nonces ( - request_nonce BYTEA NOT NULL, - did DID NOT NULL, - CONSTRAINT did_request_nonces_pk PRIMARY KEY (request_nonce, did) -); diff --git a/node/src/main/resources/db/migration/V32__add_services_and_service_endpoints_table.sql b/node/src/main/resources/db/migration/V32__add_services_and_service_endpoints_table.sql deleted file mode 100644 index ac45e91396..0000000000 --- a/node/src/main/resources/db/migration/V32__add_services_and_service_endpoints_table.sql +++ /dev/null @@ -1,41 +0,0 @@ -CREATE TABLE services -( - service_id ID_TYPE PRIMARY KEY NOT NULL, - id TEXT NOT NULl, - did_suffix ID_TYPE NOT NULL, - type TEXT NOT NULL, - - added_on_transaction_id TRANSACTION_ID NOT NULL, - added_on TIMESTAMP WITH TIME ZONE NOT NULL, - added_on_absn INTEGER NOT NULL, - --^ Atala Block Sequence Number (absn) of the operation that added the service - added_on_osn INTEGER NOT NULL, - --^ Operation Sequence Number (osn) of the operation that added the service - - revoked_on_transaction_id TRANSACTION_ID NULL, - revoked_on TIMESTAMP WITH TIME ZONE NULL, - revoked_on_absn INTEGER NULL, - revoked_on_osn INTEGER NULL, - - ledger VARCHAR(32) NOT NULL, - - CONSTRAINT services_did_suffix_fk - FOREIGN KEY (did_suffix) REFERENCES did_data (did_suffix) - -); - -CREATE UNIQUE INDEX unique_did_suffix_and_id_on_non_revoked - ON services (did_suffix, id) WHERE (revoked_on is NULL); - - -CREATE TABLE service_endpoints -( - service_endpoint_id ID_TYPE PRIMARY KEY NOT NULL, - url_index INTEGER NOT NULL, - service_id ID_TYPE NOT NULL, - url TEXT NOT NULL, - - CONSTRAINT service_endpoints_service_id_fk - FOREIGN KEY (service_id) REFERENCES services (service_id) - -); diff --git a/node/src/main/resources/db/migration/V33__alter_type_key_usage.sql b/node/src/main/resources/db/migration/V33__alter_type_key_usage.sql deleted file mode 100644 index 899e79005c..0000000000 --- a/node/src/main/resources/db/migration/V33__alter_type_key_usage.sql +++ /dev/null @@ -1,4 +0,0 @@ -ALTER TYPE key_usage RENAME VALUE 'COMMUNICATION_KEY' to 'KEY_AGREEMENT_KEY'; - -ALTER TYPE key_usage ADD VALUE 'CAPABILITY_INVOCATION_KEY'; -ALTER TYPE key_usage ADD VALUE 'CAPABILITY_DELEGATION_KEY'; diff --git a/node/src/main/resources/db/migration/V34__drop_service_endpoints_and_add_it_to_services.sql b/node/src/main/resources/db/migration/V34__drop_service_endpoints_and_add_it_to_services.sql deleted file mode 100644 index 1f35108396..0000000000 --- a/node/src/main/resources/db/migration/V34__drop_service_endpoints_and_add_it_to_services.sql +++ /dev/null @@ -1,10 +0,0 @@ - --- This migration assumes that tables services and services_endpoints are empty -DROP TABLE service_endpoints; - -ALTER TABLE services - ADD COLUMN service_endpoints TEXT NOT NULL default ''; - -ALTER TABLE services - ALTER COLUMN service_endpoints DROP DEFAULT; - diff --git a/node/src/main/resources/db/migration/V35__add_contexts_table.sql b/node/src/main/resources/db/migration/V35__add_contexts_table.sql deleted file mode 100644 index cdaca0681e..0000000000 --- a/node/src/main/resources/db/migration/V35__add_contexts_table.sql +++ /dev/null @@ -1,20 +0,0 @@ -CREATE TABLE contexts -( - context_id ID_TYPE PRIMARY KEY NOT NULL, - did_suffix ID_TYPE NOT NULL, - context TEXT NOT NULL, - added_on_transaction_id TRANSACTION_ID NOT NULL, - added_on TIMESTAMP WITH TIME ZONE NOT NULL, - added_on_absn INTEGER NOT NULL, - --^ Atala Block Sequence Number (absn) of the operation that added the context string - added_on_osn INTEGER NOT NULL, - --^ Operation Sequence Number (osn) of the operation that added the context string - - revoked_on_transaction_id TRANSACTION_ID NULL, - revoked_on TIMESTAMP WITH TIME ZONE NULL, - revoked_on_absn INTEGER NULL, - revoked_on_osn INTEGER NULL -); - -CREATE UNIQUE INDEX unique_did_suffix_and_context_string_on_non_revoked - ON contexts (did_suffix, context) WHERE (revoked_on is NULL); \ No newline at end of file diff --git a/node/src/main/resources/db/migration/V3__did_tables.sql b/node/src/main/resources/db/migration/V3__did_tables.sql deleted file mode 100644 index 9309f97126..0000000000 --- a/node/src/main/resources/db/migration/V3__did_tables.sql +++ /dev/null @@ -1,39 +0,0 @@ -CREATE TYPE KEY_USAGE AS ENUM('MASTER_KEY', 'ISSUING_KEY' ,'COMMUNICATION_KEY', 'AUTHENTICATION_KEY'); - -CREATE DOMAIN ID_TYPE AS TEXT CHECK( - VALUE ~ '^[0-9a-f]{64}$' -); - -CREATE DOMAIN OPERATION_HASH AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE did_data( - did_suffix ID_TYPE NOT NULL, - last_operation OPERATION_HASH NOT NULL, - CONSTRAINT did_data_pk PRIMARY KEY (did_suffix) -); - -CREATE TABLE public_keys( - did_suffix ID_TYPE NOT NULL, - key_id TEXT NOT NULL, - key_usage KEY_USAGE NOT NULL, - curve TEXT NOT NULL, - x BYTEA NOT NULL, - y BYTEA NOT NULL, - added_on TIMESTAMPTZ NOT NULL, - added_on_absn INTEGER NOT NULL, - --^ Atala Block Sequence Number (absn) of the operation that added the key - added_on_osn INTEGER NOT NULL, - --^ Operation Sequence Number (osn) of the operation that added the key - - revoked_on TIMESTAMPTZ NULL DEFAULT NULL, - revoked_on_absn INTEGER NULL DEFAULT NULL, - --^ Atala Block Sequence Number (absn) of the operation that revoked the key - revoked_on_osn INTEGER NULL DEFAULT NULL, - --^ Operation Sequence Number (osn) of the operation that revoked the key - - CONSTRAINT public_keys_pk PRIMARY KEY (did_suffix, key_id) - -- add constraints about congruent addition of sequence_numbers -); diff --git a/node/src/main/resources/db/migration/V4__credentials_table.sql b/node/src/main/resources/db/migration/V4__credentials_table.sql deleted file mode 100644 index f877b59908..0000000000 --- a/node/src/main/resources/db/migration/V4__credentials_table.sql +++ /dev/null @@ -1,27 +0,0 @@ -CREATE DOMAIN CONTENT_HASH AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE credentials( - credential_id ID_TYPE NOT NULL, - last_operation OPERATION_HASH NOT NULL, - issuer ID_TYPE NOT NULL, - content_hash CONTENT_HASH NOT NULL, - issued_on TIMESTAMPTZ NOT NULL, - issued_on_absn INTEGER NOT NULL, - --^ Atala Block Sequence Number (absn) of the operation that issued the credential - issued_on_osn INTEGER NOT NULL, - --^ Operation Sequence Number (osn) of the operation that issued the credential - revoked_on TIMESTAMPTZ NULL DEFAULT NULL, - revoked_on_absn INTEGER NULL DEFAULT NULL, - --^ Atala Block Sequence Number (absn) of the operation that revoked the credential - revoked_on_osn INTEGER NULL DEFAULT NULL, - --^ Operation Sequence Number (osn) of the operation that revoked the credential - - CONSTRAINT credentials_pk PRIMARY KEY (credential_id), - CONSTRAINT credentials_issuer_fk FOREIGN KEY (issuer) REFERENCES did_data (did_suffix) - -- Add constraint so that revoked_on and revoke_on_sequence_number match in terms on not being null -); - -CREATE INDEX credentials_issuer_index ON credentials USING BTREE (issuer); diff --git a/node/src/main/resources/db/migration/V5__key_values_table.sql b/node/src/main/resources/db/migration/V5__key_values_table.sql deleted file mode 100644 index 24d725f06c..0000000000 --- a/node/src/main/resources/db/migration/V5__key_values_table.sql +++ /dev/null @@ -1,5 +0,0 @@ --- General purpose key-value pairs -CREATE TABLE key_values( - key VARCHAR(64) PRIMARY KEY, - value TEXT -); diff --git a/node/src/main/resources/db/migration/V6__atala_objects_add_tx_info.sql b/node/src/main/resources/db/migration/V6__atala_objects_add_tx_info.sql deleted file mode 100644 index 87ca33d29e..0000000000 --- a/node/src/main/resources/db/migration/V6__atala_objects_add_tx_info.sql +++ /dev/null @@ -1,19 +0,0 @@ -CREATE DOMAIN TRANSACTION_ID AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - --- Add optional columns to the table -ALTER TABLE atala_objects - ADD COLUMN transaction_id TRANSACTION_ID NULL, - ADD COLUMN ledger VARCHAR(32) NULL; - --- Back-fill new columns for the existing rows -UPDATE atala_objects - SET transaction_id = atala_object_id, - ledger = 'InMemory'; - --- Make new columns no longer optional -ALTER TABLE atala_objects - ALTER COLUMN transaction_id SET NOT NULL, - ALTER COLUMN ledger SET NOT NULL; diff --git a/node/src/main/resources/db/migration/V7__atala_objects_drop_sequence_number_unique.sql b/node/src/main/resources/db/migration/V7__atala_objects_drop_sequence_number_unique.sql deleted file mode 100644 index 72cb638fea..0000000000 --- a/node/src/main/resources/db/migration/V7__atala_objects_drop_sequence_number_unique.sql +++ /dev/null @@ -1,7 +0,0 @@ --- atala_objects.sequence_number is not constrained anymore -ALTER TABLE atala_objects - DROP CONSTRAINT atala_objects_sequence_number_unique, - DROP CONSTRAINT atala_objects_sequence_number_positive; - --- It also does not need to be queried directly anymore -DROP INDEX atala_objects_sequence_number_index; diff --git a/node/src/main/resources/db/migration/V8__atala_object_tx.sql b/node/src/main/resources/db/migration/V8__atala_object_tx.sql deleted file mode 100644 index 1a08031c8f..0000000000 --- a/node/src/main/resources/db/migration/V8__atala_object_tx.sql +++ /dev/null @@ -1,31 +0,0 @@ --- Create table to hold atala_objects' transaction info -CREATE TABLE atala_object_txs ( - atala_object_id ATALA_OBJECT_ID NOT NULL, - ledger VARCHAR(32) NOT NULL, - block_number INT NOT NULL, - block_timestamp TIMESTAMPTZ NOT NULL, - block_index INTEGER NOT NULL, - transaction_id TRANSACTION_ID NOT NULL, - - CONSTRAINT atala_object_txs_pk PRIMARY KEY (atala_object_id), - CONSTRAINT atala_object_txs_atala_object_id_fk - FOREIGN KEY (atala_object_id) - REFERENCES atala_objects (atala_object_id) -); - --- Migrate existing data (set a hard-coded block_number of 1) -INSERT INTO atala_object_txs - SELECT atala_object_id, - ledger, - 1 as block_number, - object_timestamp AS block_timestamp, - sequence_number AS block_index, - transaction_id - FROM atala_objects; - --- Drop old columns -ALTER TABLE atala_objects - DROP COLUMN sequence_number, - DROP COLUMN object_timestamp, - DROP COLUMN transaction_id, - DROP COLUMN ledger; diff --git a/node/src/main/resources/db/migration/V9__credential_batches_table.sql b/node/src/main/resources/db/migration/V9__credential_batches_table.sql deleted file mode 100644 index b24fb905ea..0000000000 --- a/node/src/main/resources/db/migration/V9__credential_batches_table.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE DOMAIN MERKLE_ROOT AS BYTEA -CHECK ( - LENGTH(VALUE) = 32 -); - -CREATE TABLE credential_batches( - batch_id ID_TYPE NOT NULL, - last_operation OPERATION_HASH NOT NULL, - issuer_did_suffix ID_TYPE NOT NULL, - merkle_root MERKLE_ROOT NOT NULL, - issued_on TIMESTAMPTZ NOT NULL, - -- Atala Block Sequence Number (absn) of the operation that issued the batch - issued_on_absn INTEGER NOT NULL, - -- Operation Sequence Number (osn) of the operation that issued the batch - issued_on_osn INTEGER NOT NULL, - revoked_on TIMESTAMPTZ NULL DEFAULT NULL, - -- Atala Block Sequence Number (absn) of the operation that revoked the batch - revoked_on_absn INTEGER NULL DEFAULT NULL, - -- Operation Sequence Number (osn) of the operation that revoked the batch - revoked_on_osn INTEGER NULL DEFAULT NULL, - - CONSTRAINT credential_batches_pk PRIMARY KEY (batch_id), - CONSTRAINT credential_batches_issuer_did_suffix_fk FOREIGN KEY (issuer_did_suffix) REFERENCES did_data (did_suffix), - CONSTRAINT revoke_on_check CHECK ( - (revoked_on IS NULL AND revoked_on_absn IS NULL AND revoked_on_osn IS NULL) - OR - (revoked_on IS NOT NULL AND revoked_on_absn IS NOT NULL AND revoked_on_osn IS NOT NULL) - ) -); - -CREATE INDEX credential_batches_issuer_did_suffix_index ON credential_batches USING BTREE (issuer_did_suffix); diff --git a/node/src/main/scala/db/migration/.gitkeep b/node/src/main/scala/db/migration/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/node/src/main/scala/db/migration/V19__public_keys.scala b/node/src/main/scala/db/migration/V19__public_keys.scala deleted file mode 100644 index a05652ef11..0000000000 --- a/node/src/main/scala/db/migration/V19__public_keys.scala +++ /dev/null @@ -1,56 +0,0 @@ -package db.migration - -import io.iohk.atala.prism.crypto.EC.{INSTANCE => EC} -import org.flywaydb.core.api.migration.{BaseJavaMigration, Context} - -import java.sql.ResultSet -import scala.util.{Failure, Success, Try, Using} - -class V19__public_keys extends BaseJavaMigration { - - override def migrate(context: Context): Unit = { - Try { - val rows = context.getConnection.createStatement - .executeQuery( - "SELECT did_suffix, key_id, x, y FROM public_keys WHERE xCompressed is NULL" - ) - if (rows.next()) - loop(rows, context) - } match { - case Failure(exception) => - exception.printStackTrace() - throw new Exception("V19__public_keys migration failed") - case Success(_) => println("V19__public_keys migration succeed") - } - } - - def loop(row: ResultSet, context: Context): Unit = { - - val did_suffix = row.getString("did_suffix") - val key_id = row.getString("key_id") - - val x = row.getBytes("x") - val y = row.getBytes("y") - - val compressedX: Array[Byte] = - EC.toPublicKeyFromByteCoordinates(x, y).getEncodedCompressed - - Using( - context.getConnection - .prepareStatement( - "UPDATE public_keys SET xCompressed = ? WHERE did_suffix = ? AND key_id = ?" - ) - ) { update => - update.setBytes(1, compressedX) - update.setString(2, did_suffix) - update.setObject(3, key_id) - update.execute() - } - - if (row.next()) - loop(row, context) - else () - - } - -} diff --git a/node/src/test/scala/io/iohk/atala/prism/node/migrations/.gitkeep b/node/src/test/scala/io/iohk/atala/prism/node/migrations/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/node/src/test/scala/io/iohk/atala/prism/node/migrations/V19MigrationSpec.scala b/node/src/test/scala/io/iohk/atala/prism/node/migrations/V19MigrationSpec.scala deleted file mode 100644 index 14d2180e0f..0000000000 --- a/node/src/test/scala/io/iohk/atala/prism/node/migrations/V19MigrationSpec.scala +++ /dev/null @@ -1,82 +0,0 @@ -package io.iohk.atala.prism.node.migrations - -import doobie.implicits._ -import io.iohk.atala.prism.crypto.EC.{INSTANCE => EC} -import io.iohk.atala.prism.crypto.ECConfig.{INSTANCE => ECConfig} -import io.iohk.atala.prism.daos.BaseDAO -import io.iohk.atala.prism.models.{DidSuffix, Ledger, TransactionId} -import io.iohk.atala.prism.node.models.nodeState.LedgerData -import io.iohk.atala.prism.node.models.{DIDPublicKey, KeyUsage} -import io.iohk.atala.prism.repositories.PostgresMigrationSpec -import io.iohk.atala.prism.node.repositories.daos._ -import io.iohk.atala.prism.repositories.ops.SqlTestOps.Implicits -import doobie.implicits.legacy.instant._ -import io.iohk.atala.prism.crypto.Sha256 -import io.iohk.atala.prism.protos.models.TimestampInfo - -import java.time.Instant - -class V19MigrationSpec extends PostgresMigrationSpec("db.migration.V19") with BaseDAO { - - private val dummyTimestampInfo = - new TimestampInfo(Instant.ofEpochMilli(0).toEpochMilli, 1, 0) - private val dummyLedgerData = LedgerData( - TransactionId - .from(Array.fill[Byte](TransactionId.config.size.toBytes.toInt)(0)) - .get, - Ledger.InMemory, - dummyTimestampInfo - ) - val didDigest = Sha256.compute("test".getBytes()) - val didSuffix = DidSuffix(didDigest.getHexValue) - val didPublicKey: DIDPublicKey = - DIDPublicKey( - didSuffix, - "master", - KeyUsage.MasterKey, - EC.generateKeyPair().getPublicKey - ) - - private def insertPublicKey(key: DIDPublicKey, ledgerData: LedgerData) = { - val curveName = ECConfig.getCURVE_NAME - val point = key.key.getCurvePoint - - val xBytes = point.getX.bytes() - val yBytes = point.getY.bytes() - - val addedOn = ledgerData.timestampInfo - sql""" - |INSERT INTO public_keys (did_suffix, key_id, key_usage, curve, x, y, - | added_on, added_on_absn, added_on_osn, - | added_on_transaction_id, ledger) - |VALUES (${key.didSuffix}, ${key.keyId}, ${key.keyUsage}, $curveName, $xBytes, $yBytes, - | ${Instant - .ofEpochMilli( - addedOn.getAtalaBlockTimestamp - )}, ${addedOn.getAtalaBlockSequenceNumber}, ${addedOn.getOperationSequenceNumber}, - | ${ledgerData.transactionId}, ${ledgerData.ledger}) - """.stripMargin.runUpdate() - } - - private def selectPublicKeyCompressed(key: DIDPublicKey) = { - sql"SELECT xCompressed FROM public_keys WHERE did_suffix = ${key.didSuffix} AND key_id = ${key.keyId}" - .runUnique[Array[Byte]]() - } - - test( - beforeApply = { - insertPublicKey(didPublicKey, dummyLedgerData) - }, - afterApplied = { - val inDB = selectPublicKeyCompressed(didPublicKey) - val expected = EC - .toPublicKeyFromByteCoordinates( - didPublicKey.key.getCurvePoint.getX.bytes(), - didPublicKey.key.getCurvePoint.getY.bytes() - ) - .getEncodedCompressed - inDB mustBe expected - inDB.length mustBe 33 - } - ) -} From d87cdd5d5a53d08e28383bc5f7ad56f0f425539b Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Fri, 22 Mar 2024 04:28:29 +0400 Subject: [PATCH 2/6] Remove migration tests Signed-off-by: Shota Jolbordi --- .gitignore | 4 +- .../migrations/V6MigrationSpec.scala | 45 -------------- .../migrations/V8MigrationSpec.scala | 62 ------------------- 3 files changed, 2 insertions(+), 109 deletions(-) delete mode 100644 node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V6MigrationSpec.scala delete mode 100644 node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V8MigrationSpec.scala diff --git a/.gitignore b/.gitignore index 12b25d1797..8ae454e2e8 100644 --- a/.gitignore +++ b/.gitignore @@ -20,8 +20,7 @@ out/ **/.bloop/ *.worksheet.sc **/metals.sbt -prism-interactive-demo-web/src/protos/intdemo -*.aux +*.aux *.toc *.pdf .idea_modules @@ -35,3 +34,4 @@ mill client-storage.txt .bsp *.sc +megalinter-reports diff --git a/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V6MigrationSpec.scala b/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V6MigrationSpec.scala deleted file mode 100644 index 5559dfc11c..0000000000 --- a/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V6MigrationSpec.scala +++ /dev/null @@ -1,45 +0,0 @@ -package io.iohk.atala.prism.node.repositories.migrations - -import java.time.Instant -import doobie.implicits._ -import doobie.implicits.legacy.instant._ -import io.iohk.atala.prism.crypto.Sha256 -import io.iohk.atala.prism.repositories.PostgresMigrationSpec -import io.iohk.atala.prism.repositories.ops.SqlTestOps.Implicits - -class V6MigrationSpec extends PostgresMigrationSpec("V6") { - private val objectId = Sha256.compute("objectId".getBytes).getValue - private val sequenceNumber = 1337 - private val objectTimestamp = Instant.now() - - private case class TestAtalaObject( - atalaObjectId: Array[Byte], - sequenceNumber: Int, - objectTimestamp: Instant, - transactionId: Array[Byte], - ledger: String - ) - - test( - beforeApply = { - sql""" - |INSERT INTO atala_objects (atala_object_id, sequence_number, object_timestamp) - |VALUES($objectId, $sequenceNumber, $objectTimestamp)""".stripMargin - .runUpdate() - }, - afterApplied = { - val data = sql""" - |SELECT atala_object_id, sequence_number, object_timestamp, transaction_id, ledger - |FROM atala_objects""".stripMargin - .runUnique[TestAtalaObject]() - - // Verify old data is the same - data.atalaObjectId mustBe objectId - data.sequenceNumber mustBe sequenceNumber - data.objectTimestamp mustBe objectTimestamp - // Verify new data was properly set - data.transactionId mustBe objectId - data.ledger mustBe "InMemory" - } - ) -} diff --git a/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V8MigrationSpec.scala b/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V8MigrationSpec.scala deleted file mode 100644 index 02abdb10b2..0000000000 --- a/node/src/test/scala/io/iohk/atala/prism/node/repositories/migrations/V8MigrationSpec.scala +++ /dev/null @@ -1,62 +0,0 @@ -package io.iohk.atala.prism.node.repositories.migrations - -import java.time.Instant -import doobie.implicits._ -import doobie.implicits.legacy.instant._ -import io.iohk.atala.prism.crypto.Sha256 -import io.iohk.atala.prism.repositories.PostgresMigrationSpec -import io.iohk.atala.prism.repositories.ops.SqlTestOps.Implicits - -class V8MigrationSpec extends PostgresMigrationSpec("V8") { - private val objectId = Sha256.compute("objectId".getBytes).getValue - private val objectContent = "objectContent".getBytes - private val sequenceNumber = 1337 - private val objectTimestamp = Instant.now() - private val transactionId = Sha256.compute("transactionId".getBytes).getValue - private val ledger = "SomeLedger" - - private case class TestAtalaObject( - atalaObjectId: Array[Byte], - objectContent: Array[Byte] - ) - - private case class TestAtalaObjectTx( - atalaObjectId: Array[Byte], - ledger: String, - blockNumber: Int, - blockTimestamp: Instant, - blockIndex: Int, - transactionId: Array[Byte] - ) - - test( - beforeApply = { - sql""" - |INSERT INTO atala_objects - | (atala_object_id, object_content, sequence_number, object_timestamp, transaction_id, ledger) - |VALUES ($objectId, $objectContent, $sequenceNumber, $objectTimestamp, $transactionId, $ledger) - """.stripMargin.runUpdate() - }, - afterApplied = { - val atalaObject = sql""" - |SELECT atala_object_id, object_content - |FROM atala_objects - """.stripMargin.runUnique[TestAtalaObject]() - val atalaObjectTx = sql""" - |SELECT atala_object_id, ledger, block_number, block_timestamp, block_index, transaction_id - |FROM atala_object_txs - """.stripMargin.runUnique[TestAtalaObjectTx]() - - // Verify old data is the same - atalaObject.atalaObjectId mustBe objectId - atalaObject.objectContent mustBe objectContent - // Verify new data was properly set - atalaObjectTx.atalaObjectId mustBe objectId - atalaObjectTx.ledger mustBe ledger - atalaObjectTx.blockNumber mustBe 1 - atalaObjectTx.blockTimestamp mustBe objectTimestamp - atalaObjectTx.blockIndex mustBe sequenceNumber - atalaObjectTx.transactionId mustBe transactionId - } - ) -} From cd26e69550c404b058a3da644188803eaad44be3 Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Fri, 22 Mar 2024 04:46:30 +0400 Subject: [PATCH 3/6] add ingnore invalid create option for postres mega linter Signed-off-by: Shota Jolbordi --- .mega-linter.yml | 40 ++-- .../db/migration/V1__create_tables.sql | 176 +++++++++--------- 2 files changed, 108 insertions(+), 108 deletions(-) diff --git a/.mega-linter.yml b/.mega-linter.yml index febb121a33..8584e0caaf 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -4,25 +4,25 @@ APPLY_FIXES: all DEFAULT_BRANCH: main DISABLE_LINTERS: [ - REPOSITORY_DEVSKIM, - REPOSITORY_GITLEAKS, - DOCKERFILE_HADOLINT, - REPOSITORY_TRIVY, - REPOSITORY_CHECKOV, - REPOSITORY_SECRETLINT, - SCALA_SCALAFIX, - SQL_TSQLLINT, - C_CPPLINT, # For pollux/lib/anoncreds/src/main/c - CPP_CPPLINT, # For pollux/lib/anoncreds/src/main/c - JAVA_CHECKSTYLE, # For pollux/lib/anoncreds/src/main/java -] -DISABLE_ERRORS_LINTERS: [ - KOTLIN_KTLINT, - PROTOBUF_PROTOLINT, - OPENAPI_SPECTRAL, - MARKDOWN_MARKDOWN_LINK_CHECK, -] - + REPOSITORY_DEVSKIM, + REPOSITORY_GITLEAKS, + DOCKERFILE_HADOLINT, + REPOSITORY_TRIVY, + REPOSITORY_CHECKOV, + REPOSITORY_SECRETLINT, + SCALA_SCALAFIX, + SQL_TSQLLINT, + C_CPPLINT, # For pollux/lib/anoncreds/src/main/c + CPP_CPPLINT, # For pollux/lib/anoncreds/src/main/c + JAVA_CHECKSTYLE, # For pollux/lib/anoncreds/src/main/java + ] +DISABLE_ERRORS_LINTERS: + [ + KOTLIN_KTLINT, + PROTOBUF_PROTOLINT, + OPENAPI_SPECTRAL, + MARKDOWN_MARKDOWN_LINK_CHECK, + ] DISABLE: [COPYPASTE, SPELL, CREDENTIALS] SHOW_ELAPSED_TIME: false FILEIO_REPORTER: false @@ -43,7 +43,7 @@ PRE_COMMANDS: # Linter customisation MARKDOWN_MARKDOWN_LINK_CHECK_FILTER_REGEX_EXCLUDE: "CHANGELOG.md" MARKDOWN_MARKDOWNLINT_FILTER_REGEX_EXCLUDE: "CHANGELOG.md" -SQL_SQL_LINT_ARGUMENTS: -d postgres --ignore-errors=postgres-invalid-alter-option +SQL_SQL_LINT_ARGUMENTS: -d postgres --ignore-errors=postgres-invalid-alter-option,postgres-invalid-create-option YAML_YAMLLINT_FILTER_REGEX_EXCLUDE: "infrastructure/charts/node/*" YAML_PRETTIER_FILTER_REGEX_EXCLUDE: "infrastructure/charts/node/*" YAML_V8R_FILTER_REGEX_EXCLUDE: "infrastructure/charts/node/*" diff --git a/node/src/main/resources/db/migration/V1__create_tables.sql b/node/src/main/resources/db/migration/V1__create_tables.sql index 7a860f058b..ed6c7eb99b 100644 --- a/node/src/main/resources/db/migration/V1__create_tables.sql +++ b/node/src/main/resources/db/migration/V1__create_tables.sql @@ -1,89 +1,70 @@ -CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION postgres; +CREATE SCHEMA IF NOT EXISTS PUBLIC + AUTHORIZATION postgres; -COMMENT ON SCHEMA public IS 'standard public schema'; +CREATE DOMAIN public.atala_object_id AS BYTEA CONSTRAINT atala_object_id_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.atala_object_id AS bytea - CONSTRAINT atala_object_id_check CHECK (length(VALUE) = 32); --- DROP TYPE public.atala_object_status; +CREATE TYPE public.atala_object_status AS ENUM ('SCHEDULED', 'PENDING', 'MERGED', 'PROCESSED'); -CREATE TYPE public.atala_object_status AS ENUM ( - 'SCHEDULED', - 'PENDING', - 'MERGED', - 'PROCESSED'); +CREATE TYPE public.atala_object_transaction_status AS ENUM ('PENDING', 'DELETED', 'IN_LEDGER'); -CREATE TYPE public.atala_object_transaction_status AS ENUM ( - 'PENDING', - 'DELETED', - 'IN_LEDGER'); +CREATE DOMAIN public.atala_operation_id AS BYTEA CONSTRAINT atala_operation_id_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.atala_operation_id AS bytea - CONSTRAINT atala_operation_id_check CHECK (length(VALUE) = 32); -CREATE TYPE public.atala_operation_status AS ENUM ( - 'UNKNOWN', - 'RECEIVED', - 'APPLIED', - 'REJECTED'); +CREATE TYPE public.atala_operation_status AS ENUM ('UNKNOWN', 'RECEIVED', 'APPLIED', 'REJECTED'); -CREATE DOMAIN public.block_hash_type AS bytea - CONSTRAINT block_hash_type_check CHECK (length(VALUE) = 32); +CREATE DOMAIN public.block_hash_type AS BYTEA CONSTRAINT block_hash_type_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.block_no AS integer - CONSTRAINT block_no_check CHECK (VALUE >= 0); -CREATE DOMAIN public.blockhash_type AS bytea - CONSTRAINT blockhash_type_check CHECK (length(VALUE) = 32); +CREATE DOMAIN public.block_no AS integer CONSTRAINT block_no_check CHECK (VALUE >= 0); -CREATE DOMAIN public.content_hash AS bytea - CONSTRAINT content_hash_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.credential_hash AS bytea - CONSTRAINT credential_hash_check CHECK (length(VALUE) = 32); +CREATE DOMAIN public.blockhash_type AS BYTEA CONSTRAINT blockhash_type_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.did AS text - COLLATE "default" - CONSTRAINT did_check CHECK (VALUE ~ '^did:[a-z0-9]+:[a-zA-Z0-9._-]*(:[a-zA-Z0-9._-]*)*$'::text); -CREATE DOMAIN public.id_type AS text - COLLATE "default" - CONSTRAINT id_type_check CHECK (VALUE ~ '^[0-9a-f]{64}$'::text); +CREATE DOMAIN public.content_hash AS BYTEA CONSTRAINT content_hash_check CHECK (length(VALUE) = 32); -CREATE TYPE public.key_usage AS ENUM ( - 'MASTER_KEY', - 'ISSUING_KEY', - 'KEY_AGREEMENT_KEY', - 'AUTHENTICATION_KEY', - 'REVOCATION_KEY', - 'CAPABILITY_INVOCATION_KEY', - 'CAPABILITY_DELEGATION_KEY'); +CREATE DOMAIN public.credential_hash AS BYTEA CONSTRAINT credential_hash_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.merkle_root AS bytea - CONSTRAINT merkle_root_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.non_negative_int_type AS integer - CONSTRAINT non_negative_int_type_check CHECK (VALUE >= 0); +CREATE DOMAIN public.did AS text COLLATE "default" CONSTRAINT did_check CHECK (VALUE ~ '^did:[a-z0-9]+:[a-zA-Z0-9._-]*(:[a-zA-Z0-9._-]*)*$'::text); -CREATE DOMAIN public.operation_hash AS bytea - CONSTRAINT operation_hash_check CHECK (length(VALUE) = 32); -CREATE DOMAIN public.transaction_id AS bytea - CONSTRAINT transaction_id_check CHECK (length(VALUE) = 32); +CREATE DOMAIN public.id_type AS text COLLATE "default" CONSTRAINT id_type_check CHECK (VALUE ~ '^[0-9a-f]{64}$'::text); + + +CREATE TYPE public.key_usage AS ENUM ('MASTER_KEY', 'ISSUING_KEY', 'KEY_AGREEMENT_KEY', 'AUTHENTICATION_KEY', 'REVOCATION_KEY', 'CAPABILITY_INVOCATION_KEY', 'CAPABILITY_DELEGATION_KEY'); + + +CREATE DOMAIN public.merkle_root AS BYTEA CONSTRAINT merkle_root_check CHECK (length(VALUE) = 32); + + +CREATE DOMAIN public.non_negative_int_type AS integer CONSTRAINT non_negative_int_type_check CHECK (VALUE >= 0); + + +CREATE DOMAIN public.operation_hash AS BYTEA CONSTRAINT operation_hash_check CHECK (length(VALUE) = 32); + + +CREATE DOMAIN public.transaction_id AS BYTEA CONSTRAINT transaction_id_check CHECK (length(VALUE) = 32); + CREATE TABLE public.atala_objects ( atala_object_id public.atala_object_id NOT NULL, - object_content bytea NOT NULL, + object_content BYTEA NOT NULL, received_at timestamptz NOT NULL, atala_object_status public.atala_object_status DEFAULT 'PENDING'::atala_object_status NULL, CONSTRAINT atala_objects_pk PRIMARY KEY (atala_object_id) ); + + CREATE INDEX atala_objects_atala_object_status_index ON public.atala_objects USING btree (atala_object_status); + + CREATE INDEX atala_objects_received_at ON public.atala_objects USING btree (received_at); @@ -102,7 +83,10 @@ CREATE TABLE public.contexts revoked_on_osn int4 NULL, CONSTRAINT contexts_pkey PRIMARY KEY (context_id) ); -CREATE UNIQUE INDEX unique_did_suffix_and_context_string_on_non_revoked ON public.contexts USING btree (did_suffix, context) WHERE (revoked_on IS NULL); + + +CREATE UNIQUE INDEX unique_did_suffix_and_context_string_on_non_revoked ON public.contexts USING btree (did_suffix, context) + WHERE (revoked_on IS NULL); CREATE TABLE public.did_data @@ -118,12 +102,12 @@ CREATE TABLE public.did_data ); - CREATE TABLE public.did_request_nonces ( - request_nonce bytea NOT NULL, + request_nonce BYTEA NOT NULL, did public.did NOT NULL, - CONSTRAINT did_request_nonces_pk PRIMARY KEY (request_nonce, did) + CONSTRAINT did_request_nonces_pk PRIMARY KEY (request_nonce, + did) ); @@ -131,11 +115,10 @@ CREATE TABLE public.key_values ( "key" varchar(64) NOT NULL, value text NULL, - CONSTRAINT key_values_pkey PRIMARY KEY (key) + CONSTRAINT key_values_pkey PRIMARY KEY (KEY) ); - CREATE TABLE public.metrics_counters ( counter_name varchar(256) NOT NULL, @@ -153,11 +136,11 @@ CREATE TABLE public.protocol_versions published_in public.transaction_id NOT NULL, is_effective bool NOT NULL, proposer_did public.id_type NOT NULL, - CONSTRAINT protocol_version_pk PRIMARY KEY (major_version, minor_version) + CONSTRAINT protocol_version_pk PRIMARY KEY (major_version, + minor_version) ); - CREATE TABLE public.public_keys ( did_suffix public.id_type NOT NULL, @@ -173,13 +156,13 @@ CREATE TABLE public.public_keys added_on_transaction_id public.transaction_id NOT NULL, revoked_on_transaction_id public.transaction_id NULL, ledger varchar(32) NOT NULL, - compressed bytea NOT NULL, - CONSTRAINT public_keys_pk PRIMARY KEY (did_suffix, key_id), + compressed BYTEA NOT NULL, + CONSTRAINT public_keys_pk PRIMARY KEY (did_suffix, + key_id), CONSTRAINT x_compressed_length CHECK ((length(compressed) = 33)) ); - CREATE TABLE public.atala_object_tx_submissions ( atala_object_id public.atala_object_id NOT NULL, @@ -187,14 +170,21 @@ CREATE TABLE public.atala_object_tx_submissions transaction_id public.transaction_id NOT NULL, submission_timestamp timestamptz NOT NULL, status public.atala_object_transaction_status NOT NULL, - CONSTRAINT atala_object_tx_submissions_pk PRIMARY KEY (ledger, transaction_id), - CONSTRAINT atala_object_tx_submissions_atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) + CONSTRAINT atala_object_tx_submissions_pk PRIMARY KEY (ledger, + transaction_id), + CONSTRAINT atala_object_tx_submissions_atala_object_id_fk + FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) ); + + CREATE INDEX atala_object_tx_submissions_atala_object_id_index ON public.atala_object_tx_submissions USING hash (atala_object_id); + + CREATE INDEX atala_object_tx_submissions_filter_index ON public.atala_object_tx_submissions USING btree (submission_timestamp, status, ledger); -CREATE INDEX atala_object_tx_submissions_latest_index ON public.atala_object_tx_submissions USING btree (atala_object_id, submission_timestamp); +CREATE INDEX atala_object_tx_submissions_latest_index ON public.atala_object_tx_submissions USING btree (atala_object_id, submission_timestamp); + CREATE TABLE public.atala_object_txs ( @@ -205,8 +195,11 @@ CREATE TABLE public.atala_object_txs block_index int4 NOT NULL, transaction_id public.transaction_id NOT NULL, CONSTRAINT atala_object_txs_pk PRIMARY KEY (atala_object_id), - CONSTRAINT atala_object_txs_atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) + CONSTRAINT atala_object_txs_atala_object_id_fk + FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) ); + + CREATE INDEX atala_object_txs_atala_object_id_index ON public.atala_object_txs USING hash (atala_object_id); @@ -215,13 +208,13 @@ CREATE TABLE public.atala_operations signed_atala_operation_id public.atala_operation_id NOT NULL, atala_object_id public.atala_object_id NOT NULL, atala_operation_status public.atala_operation_status NOT NULL, - status_details varchar(256) DEFAULT ''::character varying NULL, + status_details varchar(256) DEFAULT ''::CHARACTER varying NULL, CONSTRAINT signed_atala_operation_id_pk PRIMARY KEY (signed_atala_operation_id), - CONSTRAINT atala_object_id_fk FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) + CONSTRAINT atala_object_id_fk + FOREIGN KEY (atala_object_id) REFERENCES public.atala_objects (atala_object_id) ); - CREATE TABLE public.credential_batches ( batch_id public.id_type NOT NULL, @@ -238,14 +231,19 @@ CREATE TABLE public.credential_batches revoked_on_transaction_id public.transaction_id NULL, ledger varchar(32) NOT NULL, CONSTRAINT credential_batches_pk PRIMARY KEY (batch_id), - CONSTRAINT revoke_on_check CHECK (( - ((revoked_on IS NULL) AND (revoked_on_absn IS NULL) AND (revoked_on_osn IS NULL)) OR - ((revoked_on IS NOT NULL) AND (revoked_on_absn IS NOT NULL) AND (revoked_on_osn IS NOT NULL)))), - CONSTRAINT credential_batches_issuer_did_suffix_fk FOREIGN KEY (issuer_did_suffix) REFERENCES public.did_data (did_suffix) + CONSTRAINT revoke_on_check CHECK ((((revoked_on IS NULL) + AND (revoked_on_absn IS NULL) + AND (revoked_on_osn IS NULL)) + OR ((revoked_on IS NOT NULL) + AND (revoked_on_absn IS NOT NULL) + AND (revoked_on_osn IS NOT NULL)))), + CONSTRAINT credential_batches_issuer_did_suffix_fk + FOREIGN KEY (issuer_did_suffix) REFERENCES public.did_data (did_suffix) ); -CREATE INDEX credential_batches_issuer_did_suffix_index ON public.credential_batches USING btree (issuer_did_suffix); +CREATE INDEX credential_batches_issuer_did_suffix_index ON public.credential_batches USING btree (issuer_did_suffix); + CREATE TABLE public.revoked_credentials ( @@ -256,12 +254,13 @@ CREATE TABLE public.revoked_credentials revoked_on_osn int4 NOT NULL, transaction_id public.transaction_id NOT NULL, ledger varchar(32) NOT NULL, - CONSTRAINT revoked_credentials_pk PRIMARY KEY (batch_id, credential_id), - CONSTRAINT revoked_credentials_batch_id_fk FOREIGN KEY (batch_id) REFERENCES public.credential_batches (batch_id) + CONSTRAINT revoked_credentials_pk PRIMARY KEY (batch_id, + credential_id), + CONSTRAINT revoked_credentials_batch_id_fk + FOREIGN KEY (batch_id) REFERENCES public.credential_batches (batch_id) ); - CREATE TABLE public.services ( service_id public.id_type NOT NULL, @@ -279,15 +278,17 @@ CREATE TABLE public.services ledger varchar(32) NOT NULL, service_endpoints text NOT NULL, CONSTRAINT services_pkey PRIMARY KEY (service_id), - CONSTRAINT services_did_suffix_fk FOREIGN KEY (did_suffix) REFERENCES public.did_data (did_suffix) + CONSTRAINT services_did_suffix_fk + FOREIGN KEY (did_suffix) REFERENCES public.did_data (did_suffix) ); -CREATE UNIQUE INDEX unique_did_suffix_and_id_on_non_revoked ON public.services USING btree (did_suffix, id) WHERE (revoked_on IS NULL); -CREATE OR REPLACE FUNCTION public.random_bytea(p_length integer) - RETURNS bytea - LANGUAGE plpgsql -AS +CREATE UNIQUE INDEX unique_did_suffix_and_id_on_non_revoked ON public.services USING btree (did_suffix, id) + WHERE (revoked_on IS NULL); + + +CREATE OR REPLACE FUNCTION public.random_bytea(p_length integer) RETURNS BYTEA + LANGUAGE PLPGSQL AS $function$ declare o bytea := ''; @@ -298,5 +299,4 @@ begin end loop; return o; end; -$function$ -; +$function$; From 81d94bdf2186715ab265e81a1899a03ea02c143e Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Mon, 25 Mar 2024 18:39:35 +0400 Subject: [PATCH 4/6] Try mega linter 7 Signed-off-by: Shota Jolbordi --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index de3600b533..638994216a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -25,7 +25,7 @@ jobs: fetch-depth: 0 - name: Mega-Linter id: ml - uses: megalinter/megalinter@v6 + uses: megalinter/megalinter@v7 - name: Archive production artifacts if: success() || failure() uses: actions/upload-artifact@v3 From 2dbad189a92631adc2883bf3c656fd92195dbdd7 Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Mon, 25 Mar 2024 18:57:01 +0400 Subject: [PATCH 5/6] edit megalinter.yaml Signed-off-by: Shota Jolbordi --- .mega-linter.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.mega-linter.yml b/.mega-linter.yml index 8584e0caaf..ec8a366c5c 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -15,14 +15,14 @@ DISABLE_LINTERS: [ C_CPPLINT, # For pollux/lib/anoncreds/src/main/c CPP_CPPLINT, # For pollux/lib/anoncreds/src/main/c JAVA_CHECKSTYLE, # For pollux/lib/anoncreds/src/main/java - ] -DISABLE_ERRORS_LINTERS: - [ +] +DISABLE_ERRORS_LINTERS: [ KOTLIN_KTLINT, PROTOBUF_PROTOLINT, OPENAPI_SPECTRAL, MARKDOWN_MARKDOWN_LINK_CHECK, - ] +] + DISABLE: [COPYPASTE, SPELL, CREDENTIALS] SHOW_ELAPSED_TIME: false FILEIO_REPORTER: false @@ -37,8 +37,8 @@ BASH_SHELLCHECK_DISABLE_ERRORS: true # that could lead to new files in workspace created # with CLRF instead of CL due docker/python/git chain PRE_COMMANDS: - - command: git config --global core.autocrlf input - cwd: "workspace" + - command: git config --global core.autocrlf input + cwd: "workspace" # Linter customisation MARKDOWN_MARKDOWN_LINK_CHECK_FILTER_REGEX_EXCLUDE: "CHANGELOG.md" From 3177bb510116c8c383d233c2f57471749ff7d5fa Mon Sep 17 00:00:00 2001 From: Shota Jolbordi Date: Mon, 25 Mar 2024 19:05:51 +0400 Subject: [PATCH 6/6] Disable some linters Signed-off-by: Shota Jolbordi --- .mega-linter.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.mega-linter.yml b/.mega-linter.yml index ec8a366c5c..7bf516a131 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -21,6 +21,8 @@ DISABLE_ERRORS_LINTERS: [ PROTOBUF_PROTOLINT, OPENAPI_SPECTRAL, MARKDOWN_MARKDOWN_LINK_CHECK, + REPOSITORY_TRUFFLEHOG, + REPOSITORY_KICS, ] DISABLE: [COPYPASTE, SPELL, CREDENTIALS]