From e87f10fd4bc966ac5c696c756bb5e4edc6b2dd69 Mon Sep 17 00:00:00 2001 From: Naian <126972030+nain-F49FF806@users.noreply.github.com> Date: Thu, 12 Oct 2023 21:47:14 +0200 Subject: [PATCH] fix: add missing migrations (for mysql database) Signed-off-by: Naian <126972030+nain-F49FF806@users.noreply.github.com> --- .github/workflows/mediator.pr.yml | 2 +- ...-data-(accounts,_recipients,_messages).sql | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 mediator/migrations/20230810132416_3-part-data-(accounts,_recipients,_messages).sql diff --git a/.github/workflows/mediator.pr.yml b/.github/workflows/mediator.pr.yml index 7b6b0c32b2..c9b459c151 100644 --- a/.github/workflows/mediator.pr.yml +++ b/.github/workflows/mediator.pr.yml @@ -51,7 +51,7 @@ jobs: - name: Install prerequisites (sqlx) run: cargo install sqlx-cli - name: Setup database - run: DATABASE_URL=${MYSQL_URL} sqlx migrate run + run: DATABASE_URL=${MYSQL_URL} sqlx migrate run --source mediator/migrations - name: "Run mediator integration tests" run: | RUST_TEST_THREADS=1 cargo test --package mediator; \ No newline at end of file diff --git a/mediator/migrations/20230810132416_3-part-data-(accounts,_recipients,_messages).sql b/mediator/migrations/20230810132416_3-part-data-(accounts,_recipients,_messages).sql new file mode 100644 index 0000000000..e78b006397 --- /dev/null +++ b/mediator/migrations/20230810132416_3-part-data-(accounts,_recipients,_messages).sql @@ -0,0 +1,54 @@ +-- Add migration script here + +CREATE TABLE IF NOT EXISTS accounts ( + -- metadata (total accounts registered until this account's creation) + seq_num SERIAL, + -- sequential time ordered uuid + -- https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support/ + account_id BINARY(16) DEFAULT (UUID_TO_BIN(UUID(),true)) NOT NULL UNIQUE, + -- for display purpose + account_name CHAR(36) GENERATED ALWAYS AS (BIN_TO_UUID(account_id)) VIRTUAL NOT NULL, + -- mediator facing pubkey + -- UNIQUE constraint automatically creates index for fast unique checking; is reused for scanning + auth_pubkey VARCHAR(64) NOT NULL UNIQUE, + -- key used to sign messages to aries peer + our_signing_key VARCHAR(64) NOT NULL, + -- their complete did_doc, including routing keys etc (useful for wrapping messages in encryption envelope) + did_doc JSON NOT NULL, + PRIMARY KEY(account_id) +); + + +CREATE TABLE IF NOT EXISTS recipients ( + seq_num SERIAL, + account_id BINARY(16) NOT NULL, + recipient_key VARCHAR(64) NOT NULL UNIQUE, + -- UNIQUE constraint creates index to implement fast unique checking + -- so explicitly creating index is not needed (and will lead to duplicate index) + -- If removing UNIQUE above then activate the commented index below + -- INDEX(recipient_key) + PRIMARY KEY (account_id, recipient_key), + FOREIGN KEY (account_id) REFERENCES accounts(account_id) + ON DELETE CASCADE +); + + +CREATE TABLE IF NOT EXISTS messages ( + seq_num SERIAL, + account_id BINARY(16) NOT NULL, + -- Allow nullable so that recipient_key remove key-updates are possible + -- without also removing associated messages + recipient_key VARCHAR(64) NULL, + INDEX(account_id, recipient_key), + message_idb BINARY(16) DEFAULT (UUID_TO_BIN(UUID(),true)) NOT NULL UNIQUE, + -- Readable message id (for identifying message over wire) + message_id CHAR(36) GENERATED ALWAYS AS (BIN_TO_UUID(message_idb)) VIRTUAL, + -- 16MiB limit (medium blob) + message_data MEDIUMBLOB NOT NULL, + PRIMARY KEY (account_id, message_idb), + FOREIGN KEY (account_id) REFERENCES accounts(account_id) + ON DELETE CASCADE, + FOREIGN KEY (recipient_key) REFERENCES recipients(recipient_key) + ON DELETE SET NULL +); +