Files
vHost/backend/internal/migrations/022_integration_api.sql
T

34 lines
1.6 KiB
SQL

CREATE TABLE integration_keys (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
administrator_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(120) NOT NULL,
token_hash CHAR(64) NOT NULL UNIQUE,
token_prefix VARCHAR(24) NOT NULL,
scopes_json JSON NOT NULL,
expires_at DATETIME NOT NULL,
revoked_at DATETIME NULL,
last_used_at DATETIME NULL,
webhook_url VARCHAR(2048) NOT NULL DEFAULT '',
webhook_secret_cipher LONGBLOB NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_integration_admin FOREIGN KEY(administrator_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE admin_batches ADD COLUMN integration_key_id BIGINT UNSIGNED NULL,
ADD CONSTRAINT fk_batch_integration_key FOREIGN KEY(integration_key_id) REFERENCES integration_keys(id);
CREATE TABLE integration_webhooks (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
event_id CHAR(32) NOT NULL UNIQUE,
key_id BIGINT UNSIGNED NOT NULL,
batch_id BIGINT UNSIGNED NOT NULL UNIQUE,
state VARCHAR(24) NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
next_attempt_at DATETIME NOT NULL,
last_status INT NOT NULL DEFAULT 0,
last_error VARCHAR(120) NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
delivered_at DATETIME NULL,
INDEX idx_webhook_due(state,next_attempt_at),
CONSTRAINT fk_webhook_key FOREIGN KEY(key_id) REFERENCES integration_keys(id),
CONSTRAINT fk_webhook_batch FOREIGN KEY(batch_id) REFERENCES admin_batches(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;