Criou tabelas e RLS via SQL

X-Lovable-Edit-ID: edt-6bf5e7af-c611-4e61-b3e7-897b081e8cfb
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot] 2026-04-17 19:37:38 +00:00
commit 9738e10895
2 changed files with 190 additions and 0 deletions

View file

@ -22,6 +22,12 @@ export type Database = {
status: string
telegram_bot_token_vault_id: string | null
telegram_bot_username: string | null
telegram_connected_at: string | null
telegram_first_message_received_at: string | null
telegram_onboarding_completed: boolean
telegram_token_invalid: boolean
telegram_webhook_configured: boolean
telegram_webhook_secret: string | null
updated_at: string
user_id: string
uuid_tenant: string
@ -34,6 +40,12 @@ export type Database = {
status?: string
telegram_bot_token_vault_id?: string | null
telegram_bot_username?: string | null
telegram_connected_at?: string | null
telegram_first_message_received_at?: string | null
telegram_onboarding_completed?: boolean
telegram_token_invalid?: boolean
telegram_webhook_configured?: boolean
telegram_webhook_secret?: string | null
updated_at?: string
user_id: string
uuid_tenant?: string
@ -46,6 +58,12 @@ export type Database = {
status?: string
telegram_bot_token_vault_id?: string | null
telegram_bot_username?: string | null
telegram_connected_at?: string | null
telegram_first_message_received_at?: string | null
telegram_onboarding_completed?: boolean
telegram_token_invalid?: boolean
telegram_webhook_configured?: boolean
telegram_webhook_secret?: string | null
updated_at?: string
user_id?: string
uuid_tenant?: string
@ -501,6 +519,102 @@ export type Database = {
},
]
}
telegram_messages_log: {
Row: {
agent_instance_id: string
created_at: string
direction: string
id: string
is_first_message: boolean
message_text: string | null
message_type: string
raw_payload: Json | null
telegram_chat_id: number
telegram_user_id: number | null
telegram_username: string | null
user_id: string
}
Insert: {
agent_instance_id: string
created_at?: string
direction: string
id?: string
is_first_message?: boolean
message_text?: string | null
message_type?: string
raw_payload?: Json | null
telegram_chat_id: number
telegram_user_id?: number | null
telegram_username?: string | null
user_id: string
}
Update: {
agent_instance_id?: string
created_at?: string
direction?: string
id?: string
is_first_message?: boolean
message_text?: string | null
message_type?: string
raw_payload?: Json | null
telegram_chat_id?: number
telegram_user_id?: number | null
telegram_username?: string | null
user_id?: string
}
Relationships: [
{
foreignKeyName: "telegram_messages_log_agent_instance_id_fkey"
columns: ["agent_instance_id"]
isOneToOne: false
referencedRelation: "agent_instances"
referencedColumns: ["id"]
},
{
foreignKeyName: "telegram_messages_log_user_id_fkey"
columns: ["user_id"]
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
},
{
foreignKeyName: "telegram_messages_log_user_id_fkey"
columns: ["user_id"]
isOneToOne: false
referencedRelation: "user_skill_limits"
referencedColumns: ["user_id"]
},
]
}
telegram_rate_limit_bucket: {
Row: {
agent_instance_id: string
request_count: number
updated_at: string
window_start: string
}
Insert: {
agent_instance_id: string
request_count?: number
updated_at?: string
window_start?: string
}
Update: {
agent_instance_id?: string
request_count?: number
updated_at?: string
window_start?: string
}
Relationships: [
{
foreignKeyName: "telegram_rate_limit_bucket_agent_instance_id_fkey"
columns: ["agent_instance_id"]
isOneToOne: true
referencedRelation: "agent_instances"
referencedColumns: ["id"]
},
]
}
}
Views: {
user_skill_limits: {

View file

@ -0,0 +1,76 @@
-- ============================================================
-- FASE 3 — Telegram onboarding: schema
-- ============================================================
-- 1) Novas colunas em agent_instances --------------------------
ALTER TABLE public.agent_instances
ADD COLUMN IF NOT EXISTS telegram_webhook_configured boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS telegram_webhook_secret text,
ADD COLUMN IF NOT EXISTS telegram_token_invalid boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS telegram_first_message_received_at timestamptz,
ADD COLUMN IF NOT EXISTS telegram_connected_at timestamptz,
ADD COLUMN IF NOT EXISTS telegram_onboarding_completed boolean NOT NULL DEFAULT false;
-- Índice parcial em username (busca rápida e garante 1 bot por conta)
CREATE INDEX IF NOT EXISTS agent_instances_telegram_username_idx
ON public.agent_instances (telegram_bot_username)
WHERE telegram_bot_username IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS agent_instances_telegram_username_unique
ON public.agent_instances (telegram_bot_username)
WHERE telegram_bot_username IS NOT NULL;
-- 2) Tabela telegram_messages_log ------------------------------
CREATE TABLE IF NOT EXISTS public.telegram_messages_log (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
agent_instance_id uuid NOT NULL REFERENCES public.agent_instances(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
telegram_chat_id bigint NOT NULL,
telegram_user_id bigint,
telegram_username text,
direction text NOT NULL CHECK (direction IN ('incoming','outgoing')),
message_text text,
message_type text NOT NULL DEFAULT 'text' CHECK (message_type IN ('text','command','other')),
is_first_message boolean NOT NULL DEFAULT false,
raw_payload jsonb,
created_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE public.telegram_messages_log ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Usuários veem o próprio log de mensagens"
ON public.telegram_messages_log
FOR SELECT
USING (auth.uid() = user_id);
-- Sem policies de INSERT/UPDATE/DELETE: somente service_role (Edge Function)
CREATE INDEX IF NOT EXISTS telegram_messages_log_agent_idx
ON public.telegram_messages_log (agent_instance_id);
CREATE INDEX IF NOT EXISTS telegram_messages_log_user_idx
ON public.telegram_messages_log (user_id);
CREATE INDEX IF NOT EXISTS telegram_messages_log_created_idx
ON public.telegram_messages_log (created_at DESC);
CREATE INDEX IF NOT EXISTS telegram_messages_log_first_message_idx
ON public.telegram_messages_log (agent_instance_id)
WHERE is_first_message = true;
CREATE INDEX IF NOT EXISTS telegram_messages_log_agent_direction_idx
ON public.telegram_messages_log (agent_instance_id, direction, created_at DESC);
-- 3) Tabela telegram_rate_limit_bucket -------------------------
CREATE TABLE IF NOT EXISTS public.telegram_rate_limit_bucket (
agent_instance_id uuid PRIMARY KEY REFERENCES public.agent_instances(id) ON DELETE CASCADE,
window_start timestamptz NOT NULL DEFAULT now(),
request_count int NOT NULL DEFAULT 0,
updated_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE public.telegram_rate_limit_bucket ENABLE ROW LEVEL SECURITY;
-- Sem policies: somente service_role
-- 4) Realtime --------------------------------------------------
ALTER PUBLICATION supabase_realtime ADD TABLE public.telegram_messages_log;