CREATE TABLE IF NOT EXISTS connector_definitions_v025 (id varchar(36) primary key,code varchar(80) unique not null,name varchar(160) not null,category varchar(50) not null,protocol varchar(50) not null,capabilities jsonb not null default '[]',config_schema jsonb not null default '{}',auth_methods jsonb not null default '[]',driver_package varchar(180) not null default '',enabled boolean not null default true,marketplace_visible boolean not null default true,created_at timestamptz not null default now());
CREATE TABLE IF NOT EXISTS tenant_connector_instances_v025 (id varchar(36) primary key,tenant_id varchar(36) not null references tenants_v020(id) on delete cascade,connector_code varchar(80) not null,name varchar(180) not null,status varchar(30) not null default 'draft',encrypted_config text not null default '',public_config jsonb not null default '{}',last_test_at timestamptz,last_test_status varchar(30) not null default 'never',last_test_message text not null default '',created_by varchar(36) not null references users(id),created_at timestamptz not null default now(),updated_at timestamptz not null default now(),unique(tenant_id,name));
CREATE TABLE IF NOT EXISTS connector_discovered_objects_v025 (id varchar(36) primary key,tenant_id varchar(36) not null references tenants_v020(id) on delete cascade,connector_instance_id varchar(36) not null references tenant_connector_instances_v025(id) on delete cascade,object_type varchar(30) not null,namespace varchar(180) not null default '',object_name varchar(255) not null,qualified_name varchar(500) not null,columns_json jsonb not null default '[]',estimated_rows bigint,selected boolean not null default false,discovered_at timestamptz not null default now(),unique(tenant_id,connector_instance_id,qualified_name));
CREATE TABLE IF NOT EXISTS connector_sync_profiles_v025 (id varchar(36) primary key,tenant_id varchar(36) not null references tenants_v020(id) on delete cascade,connector_instance_id varchar(36) not null references tenant_connector_instances_v025(id) on delete cascade,name varchar(180) not null,selected_objects jsonb not null default '[]',mode varchar(30) not null default 'incremental',watermark_column varchar(180) not null default '',cdc_strategy varchar(50) not null default 'watermark',schedule_cron varchar(120),timezone varchar(60) not null default 'America/Sao_Paulo',destination_zone varchar(20) not null default 'bronze',enabled boolean not null default true,last_run_at timestamptz,next_run_at timestamptz,created_by varchar(36) not null references users(id),created_at timestamptz not null default now());
CREATE TABLE IF NOT EXISTS connector_test_runs_v025 (id varchar(36) primary key,tenant_id varchar(36) not null references tenants_v020(id) on delete cascade,connector_instance_id varchar(36) not null references tenant_connector_instances_v025(id) on delete cascade,status varchar(30) not null default 'running',latency_ms integer,details jsonb not null default '{}',error text not null default '',created_at timestamptz not null default now());
ALTER TABLE tenant_connector_instances_v025 ENABLE ROW LEVEL SECURITY; ALTER TABLE tenant_connector_instances_v025 FORCE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS tenant_connector_instances_policy_v025 ON tenant_connector_instances_v025;
CREATE POLICY tenant_connector_instances_policy_v025 ON tenant_connector_instances_v025 USING (tenant_id::text=current_setting('app.current_tenant_id',true)) WITH CHECK (tenant_id::text=current_setting('app.current_tenant_id',true));
