feat(desktop): implement workspace foundation + desktop-client skeleton
Plan 0 (workspaces): add workspaces + workspace_memberships schema, extend JWT/Actor/claims with primary_workspace_id, seed default workspace per tenant, thread workspace_id through tenant monitoring quota. Plan A (desktop skeleton): new Electron app (apps/desktop-client) with main/ preload/renderer, shared Vue component package (packages/ui-shared), and server surface — desktop client registration + token rotation + heartbeat, SSE task event stream, desktop accounts/tasks/content handlers, publish job endpoint, and supporting repositories, services, sqlc queries, and migrations. Hard cutover per plan: remove browser-extension monitoring callback endpoints, stub legacy media API in admin-web, and delete monitoring_callback_handler.go.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS workspace_memberships;
|
||||
DROP TABLE IF EXISTS workspaces;
|
||||
@@ -0,0 +1,43 @@
|
||||
CREATE TABLE workspaces (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
name TEXT NOT NULL,
|
||||
slug TEXT NOT NULL,
|
||||
is_default BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (tenant_id, slug)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_workspaces_tenant_default
|
||||
ON workspaces (tenant_id)
|
||||
WHERE is_default = TRUE;
|
||||
|
||||
CREATE TABLE workspace_memberships (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
workspace_id BIGINT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
role TEXT NOT NULL DEFAULT 'member',
|
||||
is_primary BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (workspace_id, user_id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_memberships_user_primary
|
||||
ON workspace_memberships (user_id)
|
||||
WHERE is_primary = TRUE;
|
||||
|
||||
INSERT INTO workspaces (tenant_id, name, slug, is_default)
|
||||
SELECT id, 'Default', 'default', TRUE
|
||||
FROM tenants
|
||||
ON CONFLICT (tenant_id, slug) DO NOTHING;
|
||||
|
||||
INSERT INTO workspace_memberships (workspace_id, user_id, tenant_id, role, is_primary)
|
||||
SELECT w.id, tm.user_id, tm.tenant_id, 'member', TRUE
|
||||
FROM tenant_memberships tm
|
||||
JOIN workspaces w
|
||||
ON w.tenant_id = tm.tenant_id
|
||||
AND w.is_default = TRUE
|
||||
WHERE tm.deleted_at IS NULL
|
||||
ON CONFLICT (workspace_id, user_id) DO NOTHING;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE platform_accounts DROP COLUMN IF EXISTS workspace_id;
|
||||
ALTER TABLE tenant_monitoring_quotas DROP COLUMN IF EXISTS workspace_id;
|
||||
@@ -0,0 +1,31 @@
|
||||
ALTER TABLE tenant_monitoring_quotas
|
||||
ADD COLUMN IF NOT EXISTS workspace_id BIGINT REFERENCES workspaces(id);
|
||||
|
||||
UPDATE tenant_monitoring_quotas q
|
||||
SET workspace_id = w.id
|
||||
FROM workspaces w
|
||||
WHERE w.tenant_id = q.tenant_id
|
||||
AND w.is_default = TRUE
|
||||
AND q.workspace_id IS NULL;
|
||||
|
||||
ALTER TABLE tenant_monitoring_quotas
|
||||
ALTER COLUMN workspace_id SET NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_monitoring_quotas_workspace
|
||||
ON tenant_monitoring_quotas (workspace_id);
|
||||
|
||||
ALTER TABLE platform_accounts
|
||||
ADD COLUMN IF NOT EXISTS workspace_id BIGINT REFERENCES workspaces(id);
|
||||
|
||||
UPDATE platform_accounts a
|
||||
SET workspace_id = w.id
|
||||
FROM workspaces w
|
||||
WHERE w.tenant_id = a.tenant_id
|
||||
AND w.is_default = TRUE
|
||||
AND a.workspace_id IS NULL;
|
||||
|
||||
ALTER TABLE platform_accounts
|
||||
ALTER COLUMN workspace_id SET NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_platform_accounts_workspace
|
||||
ON platform_accounts (workspace_id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS desktop_clients;
|
||||
@@ -0,0 +1,24 @@
|
||||
CREATE TABLE desktop_clients (
|
||||
id UUID PRIMARY KEY,
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
workspace_id BIGINT NOT NULL REFERENCES workspaces(id),
|
||||
user_id BIGINT NOT NULL REFERENCES users(id),
|
||||
token_hash BYTEA NOT NULL UNIQUE,
|
||||
device_name TEXT,
|
||||
os TEXT,
|
||||
cpu_arch TEXT,
|
||||
client_version TEXT,
|
||||
channel TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_seen_at TIMESTAMPTZ,
|
||||
last_rotated_at TIMESTAMPTZ,
|
||||
revoked_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_desktop_clients_workspace
|
||||
ON desktop_clients (workspace_id)
|
||||
WHERE revoked_at IS NULL;
|
||||
|
||||
CREATE INDEX idx_desktop_clients_user
|
||||
ON desktop_clients (user_id)
|
||||
WHERE revoked_at IS NULL;
|
||||
@@ -0,0 +1,18 @@
|
||||
DROP INDEX IF EXISTS idx_platform_accounts_client_id;
|
||||
DROP INDEX IF EXISTS uniq_platform_accounts_workspace_identity_active;
|
||||
DROP INDEX IF EXISTS uniq_platform_accounts_desktop_id;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uk_platform_accounts_identity_active
|
||||
ON platform_accounts(tenant_id, platform_id, platform_uid)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
ALTER TABLE platform_accounts
|
||||
DROP COLUMN IF EXISTS delete_requested_at,
|
||||
DROP COLUMN IF EXISTS sync_version,
|
||||
DROP COLUMN IF EXISTS tags,
|
||||
DROP COLUMN IF EXISTS verified_at,
|
||||
DROP COLUMN IF EXISTS health,
|
||||
DROP COLUMN IF EXISTS display_name,
|
||||
DROP COLUMN IF EXISTS account_fingerprint,
|
||||
DROP COLUMN IF EXISTS client_id,
|
||||
DROP COLUMN IF EXISTS desktop_id;
|
||||
@@ -0,0 +1,61 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
ALTER TABLE platform_accounts
|
||||
ADD COLUMN IF NOT EXISTS desktop_id UUID,
|
||||
ADD COLUMN IF NOT EXISTS client_id UUID REFERENCES desktop_clients(id),
|
||||
ADD COLUMN IF NOT EXISTS account_fingerprint TEXT,
|
||||
ADD COLUMN IF NOT EXISTS display_name TEXT,
|
||||
ADD COLUMN IF NOT EXISTS health TEXT,
|
||||
ADD COLUMN IF NOT EXISTS verified_at TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS tags JSONB,
|
||||
ADD COLUMN IF NOT EXISTS sync_version BIGINT DEFAULT 1,
|
||||
ADD COLUMN IF NOT EXISTS delete_requested_at TIMESTAMPTZ;
|
||||
|
||||
UPDATE platform_accounts
|
||||
SET desktop_id = COALESCE(desktop_id, gen_random_uuid()),
|
||||
display_name = COALESCE(display_name, nickname),
|
||||
health = COALESCE(
|
||||
health,
|
||||
CASE
|
||||
WHEN status = 'active' THEN 'live'
|
||||
WHEN status = 'expired' THEN 'expired'
|
||||
WHEN status = 'risk' THEN 'risk'
|
||||
WHEN status = 'captcha' THEN 'captcha'
|
||||
ELSE 'expired'
|
||||
END
|
||||
),
|
||||
verified_at = COALESCE(verified_at, last_check_at),
|
||||
tags = COALESCE(
|
||||
tags,
|
||||
CASE
|
||||
WHEN metadata_json IS NOT NULL AND jsonb_typeof(metadata_json -> 'tags') = 'array'
|
||||
THEN metadata_json -> 'tags'
|
||||
ELSE NULL
|
||||
END
|
||||
),
|
||||
sync_version = COALESCE(sync_version, 1)
|
||||
WHERE desktop_id IS NULL
|
||||
OR display_name IS NULL
|
||||
OR health IS NULL
|
||||
OR sync_version IS NULL
|
||||
OR verified_at IS NULL
|
||||
OR tags IS NULL;
|
||||
|
||||
ALTER TABLE platform_accounts
|
||||
ALTER COLUMN desktop_id SET NOT NULL,
|
||||
ALTER COLUMN display_name SET NOT NULL,
|
||||
ALTER COLUMN health SET NOT NULL,
|
||||
ALTER COLUMN sync_version SET NOT NULL;
|
||||
|
||||
DROP INDEX IF EXISTS uk_platform_accounts_identity_active;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uniq_platform_accounts_desktop_id
|
||||
ON platform_accounts (desktop_id);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uniq_platform_accounts_workspace_identity_active
|
||||
ON platform_accounts (workspace_id, platform_id, platform_uid)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_platform_accounts_client_id
|
||||
ON platform_accounts (client_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
@@ -0,0 +1,4 @@
|
||||
DROP TABLE IF EXISTS desktop_task_traces;
|
||||
DROP TABLE IF EXISTS desktop_task_attempts;
|
||||
DROP TABLE IF EXISTS desktop_tasks;
|
||||
DROP TABLE IF EXISTS desktop_publish_jobs;
|
||||
@@ -0,0 +1,82 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS desktop_publish_jobs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
desktop_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
workspace_id BIGINT NOT NULL REFERENCES workspaces(id),
|
||||
created_by_user_id BIGINT NOT NULL REFERENCES users(id),
|
||||
title TEXT NOT NULL,
|
||||
content_ref JSONB NOT NULL,
|
||||
scheduled_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (desktop_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_publish_jobs_workspace_created
|
||||
ON desktop_publish_jobs (workspace_id, created_at DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS desktop_tasks (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
desktop_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
job_id UUID NOT NULL,
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
workspace_id BIGINT NOT NULL REFERENCES workspaces(id),
|
||||
target_account_id UUID NOT NULL REFERENCES platform_accounts(desktop_id),
|
||||
target_client_id UUID NOT NULL REFERENCES desktop_clients(id),
|
||||
platform_id VARCHAR(64) NOT NULL REFERENCES media_platforms(platform_id),
|
||||
kind TEXT NOT NULL,
|
||||
payload JSONB NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
dedup_key TEXT,
|
||||
active_attempt_id UUID,
|
||||
lease_token_hash BYTEA,
|
||||
lease_expires_at TIMESTAMPTZ,
|
||||
attempts INT NOT NULL DEFAULT 0,
|
||||
parked_reason TEXT,
|
||||
result JSONB,
|
||||
error JSONB,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (desktop_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_tasks_job
|
||||
ON desktop_tasks (job_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_tasks_target_client_status
|
||||
ON desktop_tasks (target_client_id, status)
|
||||
WHERE status IN ('queued', 'in_progress', 'waiting_user');
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_tasks_workspace_status_created
|
||||
ON desktop_tasks (workspace_id, status, created_at DESC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_tasks_target_account
|
||||
ON desktop_tasks (target_account_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS desktop_task_attempts (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
desktop_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
task_id UUID NOT NULL REFERENCES desktop_tasks(desktop_id) ON DELETE CASCADE,
|
||||
client_id UUID NOT NULL REFERENCES desktop_clients(id),
|
||||
lease_token_hash BYTEA,
|
||||
started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
ended_at TIMESTAMPTZ,
|
||||
final_status TEXT,
|
||||
error JSONB,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (desktop_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_desktop_task_attempts_task_started
|
||||
ON desktop_task_attempts (task_id, started_at DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS desktop_task_traces (
|
||||
task_id UUID NOT NULL REFERENCES desktop_tasks(desktop_id) ON DELETE CASCADE,
|
||||
attempt_id UUID,
|
||||
size BIGINT,
|
||||
uploaded_at TIMESTAMPTZ,
|
||||
object_key TEXT,
|
||||
PRIMARY KEY (task_id, attempt_id)
|
||||
);
|
||||
Reference in New Issue
Block a user