Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(db): add the YouTube connection, upload job and quota tables

spacesdrivece78be49a5f5
diff --git a/supabase/migrations/20261105035821_create_youtube_connections.sql b/supabase/migrations/20261105035821_create_youtube_connections.sql
new file mode 100644
index 0000000..4ec9dee
--- /dev/null
+++ b/supabase/migrations/20261105035821_create_youtube_connections.sql
@@ -0,0 +1,21 @@
+-- creates youtube_connections, one Google sign-in per channel a person connected for publishing, with its tokens sealed
+create table public.youtube_connections (
+ id text primary key,
+ user_id uuid not null references auth.users (id) on delete cascade,
+ -- the analytics account for the same channel, when the person has one
+ account_id text,
+ channel_id text not null,
+ channel_title text not null default '',
+ scopes text[] not null default '{}',
+ status text not null default 'active',
+ -- AES-GCM sealed with a key derived from TOKEN_ENCRYPTION_KEY; never sent to a browser, a log or Claude
+ refresh_token_enc text not null,
+ access_token_enc text,
+ access_token_expires_at timestamptz,
+ created_at timestamptz not null default now(),
+ updated_at timestamptz not null default now(),
+ constraint youtube_connections_user_channel_key unique (user_id, channel_id),
+ constraint youtube_connections_status_check check (status in ('active', 'needs_reconnect')),
+ constraint youtube_connections_account_fkey foreign key (account_id, user_id)
+ references public.accounts (id, user_id) on delete set null (account_id)
+);
diff --git a/supabase/migrations/20261105035824_revoke_browser_access_youtube_connections.sql b/supabase/migrations/20261105035824_revoke_browser_access_youtube_connections.sql
new file mode 100644
index 0000000..9a06ca6
--- /dev/null
+++ b/supabase/migrations/20261105035824_revoke_browser_access_youtube_connections.sql
@@ -0,0 +1,3 @@
+-- turns on row level security and removes browser access to youtube_connections
+alter table public.youtube_connections enable row level security;
+revoke all on table public.youtube_connections from anon, authenticated;
diff --git a/supabase/migrations/20261105035827_add_youtube_connections_updated_at_trigger.sql b/supabase/migrations/20261105035827_add_youtube_connections_updated_at_trigger.sql
new file mode 100644
index 0000000..3a2af4d
--- /dev/null
+++ b/supabase/migrations/20261105035827_add_youtube_connections_updated_at_trigger.sql
@@ -0,0 +1,3 @@
+-- adds the trigger that keeps updated_at current on youtube_connections
+create trigger youtube_connections_set_updated_at before update on public.youtube_connections
+ for each row execute function public.set_updated_at();
diff --git a/supabase/migrations/20261105035830_create_youtube_uploads.sql b/supabase/migrations/20261105035830_create_youtube_uploads.sql
new file mode 100644
index 0000000..549d04a
--- /dev/null
+++ b/supabase/migrations/20261105035830_create_youtube_uploads.sql
@@ -0,0 +1,44 @@
+-- creates youtube_uploads, one job per video that the scheduled runs upload to YouTube in resumable chunks
+create table public.youtube_uploads (
+ id text primary key,
+ user_id uuid not null references auth.users (id) on delete cascade,
+ connection_id text not null references public.youtube_connections (id) on delete cascade,
+ -- the source on the instance's Cloudinary account, read in byte ranges
+ source_url text not null,
+ source_bytes bigint not null,
+ content_type text not null,
+ thumbnail_url text,
+ is_short boolean not null default false,
+ -- a snapshot of what the person asked for; editing is only possible before the upload starts
+ title text not null,
+ description text not null default '',
+ tags text[] not null default '{}',
+ category_id text not null default '22',
+ privacy text not null,
+ publish_at timestamptz,
+ made_for_kids boolean not null default false,
+ synthetic_media boolean not null default false,
+ notify_subscribers boolean not null default true,
+ state text not null default 'queued',
+ -- saved before any bytes are sent, so a lost answer never starts a second upload of the same video
+ session_uri text,
+ bytes_confirmed bigint not null default 0,
+ video_id text,
+ attempts integer not null default 0,
+ next_attempt_at timestamptz not null default now(),
+ lease_owner text,
+ lease_until timestamptz,
+ last_error text,
+ client_request_id text,
+ created_at timestamptz not null default now(),
+ updated_at timestamptz not null default now(),
+ constraint youtube_uploads_user_request_key unique (user_id, client_request_id),
+ constraint youtube_uploads_state_check check (state in ('queued', 'uploading', 'processing', 'finalizing', 'done', 'waiting_quota', 'needs_reconnect', 'needs_review', 'failed', 'cancelled')),
+ constraint youtube_uploads_privacy_check check (privacy in ('private', 'unlisted', 'public')),
+ constraint youtube_uploads_title_check check (char_length(title) between 1 and 100),
+ constraint youtube_uploads_source_url_check check (source_url ~ '^https://'),
+ constraint youtube_uploads_source_bytes_check check (source_bytes > 0),
+ constraint youtube_uploads_bytes_confirmed_check check (bytes_confirmed between 0 and source_bytes),
+ -- YouTube only schedules private videos
+ constraint youtube_uploads_publish_at_check check (publish_at is null or privacy = 'private')
+);
diff --git a/supabase/migrations/20261105035833_revoke_browser_access_youtube_uploads.sql b/supabase/migrations/20261105035833_revoke_browser_access_youtube_uploads.sql
new file mode 100644
index 0000000..6fb88ad
--- /dev/null
+++ b/supabase/migrations/20261105035833_revoke_browser_access_youtube_uploads.sql
@@ -0,0 +1,3 @@
+-- turns on row level security and removes browser access to youtube_uploads
+alter table public.youtube_uploads enable row level security;
+revoke all on table public.youtube_uploads from anon, authenticated;
diff --git a/supabase/migrations/20261105035835_add_youtube_uploads_updated_at_trigger.sql b/supabase/migrations/20261105035835_add_youtube_uploads_updated_at_trigger.sql
new file mode 100644
index 0000000..12f1b38
--- /dev/null
+++ b/supabase/migrations/20261105035835_add_youtube_uploads_updated_at_trigger.sql
@@ -0,0 +1,3 @@
+-- adds the trigger that keeps updated_at current on youtube_uploads
+create trigger youtube_uploads_set_updated_at before update on public.youtube_uploads
+ for each row execute function public.set_updated_at();
diff --git a/supabase/migrations/20261105035838_index_youtube_uploads_due.sql b/supabase/migrations/20261105035838_index_youtube_uploads_due.sql
new file mode 100644
index 0000000..c83e490
--- /dev/null
+++ b/supabase/migrations/20261105035838_index_youtube_uploads_due.sql
@@ -0,0 +1,3 @@
+-- adds an index that finds the upload jobs a run may work on, oldest due first
+create index youtube_uploads_due_idx on public.youtube_uploads (next_attempt_at)
+ where state in ('queued', 'uploading', 'processing', 'finalizing', 'waiting_quota');
diff --git a/supabase/migrations/20261105035841_index_youtube_uploads_user_created.sql b/supabase/migrations/20261105035841_index_youtube_uploads_user_created.sql
new file mode 100644
index 0000000..6c30a1e
--- /dev/null
+++ b/supabase/migrations/20261105035841_index_youtube_uploads_user_created.sql
@@ -0,0 +1,2 @@
+-- adds an index that lists a person's uploads newest first
+create index youtube_uploads_user_created_idx on public.youtube_uploads (user_id, created_at desc);
diff --git a/supabase/migrations/20261105035844_create_claim_youtube_upload_function.sql b/supabase/migrations/20261105035844_create_claim_youtube_upload_function.sql
new file mode 100644
index 0000000..eb9aa37
--- /dev/null
+++ b/supabase/migrations/20261105035844_create_claim_youtube_upload_function.sql
@@ -0,0 +1,26 @@
+-- creates claim_youtube_upload, which leases one due upload job to a run so no two runs work on the same video
+create or replace function public.claim_youtube_upload(p_owner text, p_lease_seconds integer)
+returns setof public.youtube_uploads
+language plpgsql
+set search_path = ''
+as $$
+begin
+ return query
+ update public.youtube_uploads u
+ set lease_owner = p_owner, lease_until = now() + make_interval(secs => p_lease_seconds)
+ where u.id = (
+ select j.id from public.youtube_uploads j
+ where j.state in ('queued', 'uploading', 'processing', 'finalizing', 'waiting_quota')
+ and j.next_attempt_at <= now()
+ and (j.lease_until is null or j.lease_until < now())
+ order by j.next_attempt_at, j.id
+ limit 1
+ for update skip locked
+ )
+ returning u.*;
+end;
+$$;
+
+-- functions in public are exposed through the Data API, so only the service role may call it
+revoke execute on function public.claim_youtube_upload(text, integer) from public, anon, authenticated;
+grant execute on function public.claim_youtube_upload(text, integer) to service_role;
diff --git a/supabase/migrations/20261105035846_create_youtube_quota_usage.sql b/supabase/migrations/20261105035846_create_youtube_quota_usage.sql
new file mode 100644
index 0000000..a957d6b
--- /dev/null
+++ b/supabase/migrations/20261105035846_create_youtube_quota_usage.sql
@@ -0,0 +1,10 @@
+-- creates youtube_quota_usage, the YouTube Data API units the instance's Google project used per Pacific day and bucket
+create table public.youtube_quota_usage (
+ day date not null,
+ -- uploads: videos.insert calls, 100 a day; default: every other call, 10,000 units a day
+ bucket text not null,
+ units integer not null default 0,
+ constraint youtube_quota_usage_pkey primary key (day, bucket),
+ constraint youtube_quota_usage_bucket_check check (bucket in ('uploads', 'default')),
+ constraint youtube_quota_usage_units_check check (units >= 0)
+);
diff --git a/supabase/migrations/20261105035849_revoke_browser_access_youtube_quota_usage.sql b/supabase/migrations/20261105035849_revoke_browser_access_youtube_quota_usage.sql
new file mode 100644
index 0000000..7c5adbd
--- /dev/null
+++ b/supabase/migrations/20261105035849_revoke_browser_access_youtube_quota_usage.sql
@@ -0,0 +1,3 @@
+-- turns on row level security and removes browser access to youtube_quota_usage
+alter table public.youtube_quota_usage enable row level security;
+revoke all on table public.youtube_quota_usage from anon, authenticated;
diff --git a/supabase/migrations/20261105035852_create_reserve_youtube_quota_function.sql b/supabase/migrations/20261105035852_create_reserve_youtube_quota_function.sql
new file mode 100644
index 0000000..816df7e
--- /dev/null
+++ b/supabase/migrations/20261105035852_create_reserve_youtube_quota_function.sql
@@ -0,0 +1,28 @@
+-- creates reserve_youtube_quota, which takes units from a day's bucket only while they fit under its limit
+create or replace function public.reserve_youtube_quota(p_day date, p_bucket text, p_units integer, p_limit integer)
+returns boolean
+language plpgsql
+set search_path = ''
+as $$
+declare
+ reserved integer;
+begin
+ insert into public.youtube_quota_usage as q (day, bucket, units)
+ values (p_day, p_bucket, p_units)
+ on conflict (day, bucket) do update set units = q.units + excluded.units
+ where q.units + excluded.units <= p_limit
+ returning q.units into reserved;
+ -- a first row over the limit is still inserted by the plain insert, so it is checked here too
+ if reserved is null or reserved > p_limit then
+ if reserved is not null then
+ update public.youtube_quota_usage set units = units - p_units where day = p_day and bucket = p_bucket;
+ end if;
+ return false;
+ end if;
+ return true;
+end;
+$$;
+
+-- functions in public are exposed through the Data API, so only the service role may call it
+revoke execute on function public.reserve_youtube_quota(date, text, integer, integer) from public, anon, authenticated;
+grant execute on function public.reserve_youtube_quota(date, text, integer, integer) to service_role;
diff --git a/supabase/rollbacks/20261105035821_create_youtube_connections.sql b/supabase/rollbacks/20261105035821_create_youtube_connections.sql
new file mode 100644
index 0000000..01b06dc
--- /dev/null
+++ b/supabase/rollbacks/20261105035821_create_youtube_connections.sql
@@ -0,0 +1,2 @@
+-- removes youtube_connections; every queued upload goes with it and people must connect YouTube again
+drop table if exists public.youtube_connections;
diff --git a/supabase/rollbacks/20261105035824_revoke_browser_access_youtube_connections.sql b/supabase/rollbacks/20261105035824_revoke_browser_access_youtube_connections.sql
new file mode 100644
index 0000000..6a0ba9a
--- /dev/null
+++ b/supabase/rollbacks/20261105035824_revoke_browser_access_youtube_connections.sql
@@ -0,0 +1,2 @@
+-- turns row level security off on youtube_connections; it had no browser grants to give back
+alter table public.youtube_connections disable row level security;
diff --git a/supabase/rollbacks/20261105035827_add_youtube_connections_updated_at_trigger.sql b/supabase/rollbacks/20261105035827_add_youtube_connections_updated_at_trigger.sql
new file mode 100644
index 0000000..e2fa89d
--- /dev/null
+++ b/supabase/rollbacks/20261105035827_add_youtube_connections_updated_at_trigger.sql
@@ -0,0 +1,2 @@
+-- removes the trigger that keeps updated_at current on youtube_connections
+drop trigger if exists youtube_connections_set_updated_at on public.youtube_connections;
diff --git a/supabase/rollbacks/20261105035830_create_youtube_uploads.sql b/supabase/rollbacks/20261105035830_create_youtube_uploads.sql
new file mode 100644
index 0000000..5172242
--- /dev/null
+++ b/supabase/rollbacks/20261105035830_create_youtube_uploads.sql
@@ -0,0 +1,2 @@
+-- removes youtube_uploads with every upload job and its history
+drop table if exists public.youtube_uploads;
diff --git a/supabase/rollbacks/20261105035833_revoke_browser_access_youtube_uploads.sql b/supabase/rollbacks/20261105035833_revoke_browser_access_youtube_uploads.sql
new file mode 100644
index 0000000..280952f
--- /dev/null
+++ b/supabase/rollbacks/20261105035833_revoke_browser_access_youtube_uploads.sql
@@ -0,0 +1,2 @@
+-- turns row level security off on youtube_uploads; it had no browser grants to give back
+alter table public.youtube_uploads disable row level security;
diff --git a/supabase/rollbacks/20261105035835_add_youtube_uploads_updated_at_trigger.sql b/supabase/rollbacks/20261105035835_add_youtube_uploads_updated_at_trigger.sql
new file mode 100644
index 0000000..1d5ebf3
--- /dev/null
+++ b/supabase/rollbacks/20261105035835_add_youtube_uploads_updated_at_trigger.sql
@@ -0,0 +1,2 @@
+-- removes the trigger that keeps updated_at current on youtube_uploads
+drop trigger if exists youtube_uploads_set_updated_at on public.youtube_uploads;
diff --git a/supabase/rollbacks/20261105035838_index_youtube_uploads_due.sql b/supabase/rollbacks/20261105035838_index_youtube_uploads_due.sql
new file mode 100644
index 0000000..a2b4ea0
--- /dev/null
+++ b/supabase/rollbacks/20261105035838_index_youtube_uploads_due.sql
@@ -0,0 +1,2 @@
+-- removes the index that finds due upload jobs
+drop index if exists public.youtube_uploads_due_idx;
diff --git a/supabase/rollbacks/20261105035841_index_youtube_uploads_user_created.sql b/supabase/rollbacks/20261105035841_index_youtube_uploads_user_created.sql
new file mode 100644
index 0000000..5ae433c
--- /dev/null
+++ b/supabase/rollbacks/20261105035841_index_youtube_uploads_user_created.sql
@@ -0,0 +1,2 @@
+-- removes the index that lists a person's uploads
+drop index if exists public.youtube_uploads_user_created_idx;
diff --git a/supabase/rollbacks/20261105035844_create_claim_youtube_upload_function.sql b/supabase/rollbacks/20261105035844_create_claim_youtube_upload_function.sql
new file mode 100644
index 0000000..5787aaf
--- /dev/null
+++ b/supabase/rollbacks/20261105035844_create_claim_youtube_upload_function.sql
@@ -0,0 +1,2 @@
+-- removes claim_youtube_upload
+drop function if exists public.claim_youtube_upload(text, integer);
diff --git a/supabase/rollbacks/20261105035846_create_youtube_quota_usage.sql b/supabase/rollbacks/20261105035846_create_youtube_quota_usage.sql
new file mode 100644
index 0000000..f4e47c1
--- /dev/null
+++ b/supabase/rollbacks/20261105035846_create_youtube_quota_usage.sql
@@ -0,0 +1,2 @@
+-- removes youtube_quota_usage; the next reservations start counting from zero
+drop table if exists public.youtube_quota_usage;
diff --git a/supabase/rollbacks/20261105035849_revoke_browser_access_youtube_quota_usage.sql b/supabase/rollbacks/20261105035849_revoke_browser_access_youtube_quota_usage.sql
new file mode 100644
index 0000000..648b92d
--- /dev/null
+++ b/supabase/rollbacks/20261105035849_revoke_browser_access_youtube_quota_usage.sql
@@ -0,0 +1,2 @@
+-- turns row level security off on youtube_quota_usage; it had no browser grants to give back
+alter table public.youtube_quota_usage disable row level security;
diff --git a/supabase/rollbacks/20261105035852_create_reserve_youtube_quota_function.sql b/supabase/rollbacks/20261105035852_create_reserve_youtube_quota_function.sql
new file mode 100644
index 0000000..8b816de
--- /dev/null
+++ b/supabase/rollbacks/20261105035852_create_reserve_youtube_quota_function.sql
@@ -0,0 +1,2 @@
+-- removes reserve_youtube_quota
+drop function if exists public.reserve_youtube_quota(date, text, integer, integer);
diff --git a/supabase/schemas/.pgdelta-export.json b/supabase/schemas/.pgdelta-export.json
index 8559571..f88353e 100644
--- a/supabase/schemas/.pgdelta-export.json
+++ b/supabase/schemas/.pgdelta-export.json
@@ -21,12 +21,16 @@
"public/tables/scheduled_posts.sql",
"public/tables/settings.sql",
"public/tables/tracked_posts.sql",
+ "public/tables/youtube_connections.sql",
+ "public/tables/youtube_quota_usage.sql",
+ "public/tables/youtube_uploads.sql",
"public/functions/automation_followups_for_messages.sql",
"public/functions/automation_public_review_candidates.sql",
"public/functions/automation_queue_health.sql",
"public/functions/automation_review_candidates.sql",
"public/functions/automation_rules_for_ig_user.sql",
"public/functions/automation_stats.sql",
+ "public/functions/claim_youtube_upload.sql",
"public/functions/claim_automation_comments.sql",
"public/functions/claim_automation_followups.sql",
"public/functions/claim_automation_public_replies.sql",
@@ -38,6 +42,7 @@
"public/functions/merge_scheduled_post.sql",
"public/functions/merge_tracked_post_data.sql",
"public/functions/purge_automation_comments.sql",
+ "public/functions/reserve_youtube_quota.sql",
"public/functions/record_automation_public_review.sql",
"public/functions/record_automation_review.sql",
"public/functions/reserve_automation_followup.sql",
@@ -60,6 +65,7 @@
"public/functions/automation_review_candidates.sql",
"public/functions/automation_rules_for_ig_user.sql",
"public/functions/automation_stats.sql",
+ "public/functions/claim_youtube_upload.sql",
"public/functions/claim_automation_comments.sql",
"public/functions/claim_automation_followups.sql",
"public/functions/claim_automation_public_replies.sql",
@@ -71,6 +77,7 @@
"public/functions/merge_scheduled_post.sql",
"public/functions/merge_tracked_post_data.sql",
"public/functions/purge_automation_comments.sql",
+ "public/functions/reserve_youtube_quota.sql",
"public/functions/record_automation_public_review.sql",
"public/functions/record_automation_review.sql",
"public/functions/reserve_automation_followup.sql",
@@ -92,6 +99,9 @@
"public/tables/mcp_client_grants.sql",
"public/tables/scheduled_posts.sql",
"public/tables/settings.sql",
- "public/tables/tracked_posts.sql"
+ "public/tables/tracked_posts.sql",
+ "public/tables/youtube_connections.sql",
+ "public/tables/youtube_quota_usage.sql",
+ "public/tables/youtube_uploads.sql"
]
}
diff --git a/supabase/schemas/public/functions/claim_youtube_upload.sql b/supabase/schemas/public/functions/claim_youtube_upload.sql
new file mode 100644
index 0000000..a50188f
--- /dev/null
+++ b/supabase/schemas/public/functions/claim_youtube_upload.sql
@@ -0,0 +1,29 @@
+create or replace function public.claim_youtube_upload(p_owner text, p_lease_seconds integer)
+returns setof public.youtube_uploads
+language plpgsql
+set search_path = ''
+as $$
+begin
+ return query
+ update public.youtube_uploads u
+ set lease_owner = p_owner, lease_until = now() + make_interval(secs => p_lease_seconds)
+ where u.id = (
+ select j.id from public.youtube_uploads j
+ where j.state in ('queued', 'uploading', 'processing', 'finalizing', 'waiting_quota')
+ and j.next_attempt_at <= now()
+ and (j.lease_until is null or j.lease_until < now())
+ order by j.next_attempt_at, j.id
+ limit 1
+ for update skip locked
+ )
+ returning u.*;
+end;
+$$;
+
+GRANT EXECUTE ON FUNCTION "public"."claim_youtube_upload"(text, integer) TO "service_role";
+
+REVOKE ALL ON FUNCTION "public"."claim_youtube_upload"(text, integer) FROM "postgres";
+
+GRANT EXECUTE ON FUNCTION "public"."claim_youtube_upload"(text, integer) TO "postgres";
+
+REVOKE ALL ON FUNCTION "public"."claim_youtube_upload"(text, integer) FROM PUBLIC, "anon", "authenticated";
diff --git a/supabase/schemas/public/functions/reserve_youtube_quota.sql b/supabase/schemas/public/functions/reserve_youtube_quota.sql
new file mode 100644
index 0000000..0f56928
--- /dev/null
+++ b/supabase/schemas/public/functions/reserve_youtube_quota.sql
@@ -0,0 +1,31 @@
+create or replace function public.reserve_youtube_quota(p_day date, p_bucket text, p_units integer, p_limit integer)
+returns boolean
+language plpgsql
+set search_path = ''
+as $$
+declare
+ reserved integer;
+begin
+ insert into public.youtube_quota_usage as q (day, bucket, units)
+ values (p_day, p_bucket, p_units)
+ on conflict (day, bucket) do update set units = q.units + excluded.units
+ where q.units + excluded.units <= p_limit
+ returning q.units into reserved;
+ -- a first row over the limit is still inserted by the plain insert, so it is checked here too
+ if reserved is null or reserved > p_limit then
+ if reserved is not null then
+ update public.youtube_quota_usage set units = units - p_units where day = p_day and bucket = p_bucket;
+ end if;
+ return false;
+ end if;
+ return true;
+end;
+$$;
+
+GRANT EXECUTE ON FUNCTION "public"."reserve_youtube_quota"(date, text, integer, integer) TO "service_role";
+
+REVOKE ALL ON FUNCTION "public"."reserve_youtube_quota"(date, text, integer, integer) FROM "postgres";
+
+GRANT EXECUTE ON FUNCTION "public"."reserve_youtube_quota"(date, text, integer, integer) TO "postgres";
+
+REVOKE ALL ON FUNCTION "public"."reserve_youtube_quota"(date, text, integer, integer) FROM PUBLIC, "anon", "authenticated";
diff --git a/supabase/schemas/public/tables/youtube_connections.sql b/supabase/schemas/public/tables/youtube_connections.sql
new file mode 100644
index 0000000..0cbd3e7
--- /dev/null
+++ b/supabase/schemas/public/tables/youtube_connections.sql
@@ -0,0 +1,36 @@
+create table public.youtube_connections (
+ id text primary key,
+ user_id uuid not null references auth.users (id) on delete cascade,
+ -- the analytics account for the same channel, when the person has one
+ account_id text,
+ channel_id text not null,
+ channel_title text not null default '',
+ scopes text[] not null default '{}',
+ status text not null default 'active',
+ -- AES-GCM sealed with a key derived from TOKEN_ENCRYPTION_KEY; never sent to a browser, a log or Claude
+ refresh_token_enc text not null,
+ access_token_enc text,
+ access_token_expires_at timestamptz,
+ created_at timestamptz not null default now(),
+ updated_at timestamptz not null default now(),
+ constraint youtube_connections_user_channel_key unique (user_id, channel_id),
+ constraint youtube_connections_status_check check (status in ('active', 'needs_reconnect')),
+ constraint youtube_connections_account_fkey foreign key (account_id, user_id)
+ references public.accounts (id, user_id) on delete set null (account_id)
+);
+
+ALTER TABLE "public"."youtube_connections"
+ ENABLE ROW LEVEL SECURITY;
+
+CREATE TRIGGER youtube_connections_set_updated_at
+ BEFORE UPDATE ON public.youtube_connections
+ FOR EACH ROW
+ EXECUTE FUNCTION public.set_updated_at();
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_connections" TO "service_role";
+
+REVOKE ALL ON TABLE "public"."youtube_connections" FROM "postgres";
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_connections" TO "postgres";
+
+REVOKE ALL ON TABLE "public"."youtube_connections" FROM "anon", "authenticated";
diff --git a/supabase/schemas/public/tables/youtube_quota_usage.sql b/supabase/schemas/public/tables/youtube_quota_usage.sql
new file mode 100644
index 0000000..a18c4d6
--- /dev/null
+++ b/supabase/schemas/public/tables/youtube_quota_usage.sql
@@ -0,0 +1,20 @@
+create table public.youtube_quota_usage (
+ day date not null,
+ -- uploads: videos.insert calls, 100 a day; default: every other call, 10,000 units a day
+ bucket text not null,
+ units integer not null default 0,
+ constraint youtube_quota_usage_pkey primary key (day, bucket),
+ constraint youtube_quota_usage_bucket_check check (bucket in ('uploads', 'default')),
+ constraint youtube_quota_usage_units_check check (units >= 0)
+);
+
+ALTER TABLE "public"."youtube_quota_usage"
+ ENABLE ROW LEVEL SECURITY;
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_quota_usage" TO "service_role";
+
+REVOKE ALL ON TABLE "public"."youtube_quota_usage" FROM "postgres";
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_quota_usage" TO "postgres";
+
+REVOKE ALL ON TABLE "public"."youtube_quota_usage" FROM "anon", "authenticated";
diff --git a/supabase/schemas/public/tables/youtube_uploads.sql b/supabase/schemas/public/tables/youtube_uploads.sql
new file mode 100644
index 0000000..32895e8
--- /dev/null
+++ b/supabase/schemas/public/tables/youtube_uploads.sql
@@ -0,0 +1,63 @@
+create table public.youtube_uploads (
+ id text primary key,
+ user_id uuid not null references auth.users (id) on delete cascade,
+ connection_id text not null references public.youtube_connections (id) on delete cascade,
+ -- the source on the instance's Cloudinary account, read in byte ranges
+ source_url text not null,
+ source_bytes bigint not null,
+ content_type text not null,
+ thumbnail_url text,
+ is_short boolean not null default false,
+ -- a snapshot of what the person asked for; editing is only possible before the upload starts
+ title text not null,
+ description text not null default '',
+ tags text[] not null default '{}',
+ category_id text not null default '22',
+ privacy text not null,
+ publish_at timestamptz,
+ made_for_kids boolean not null default false,
+ synthetic_media boolean not null default false,
+ notify_subscribers boolean not null default true,
+ state text not null default 'queued',
+ -- saved before any bytes are sent, so a lost answer never starts a second upload of the same video
+ session_uri text,
+ bytes_confirmed bigint not null default 0,
+ video_id text,
+ attempts integer not null default 0,
+ next_attempt_at timestamptz not null default now(),
+ lease_owner text,
+ lease_until timestamptz,
+ last_error text,
+ client_request_id text,
+ created_at timestamptz not null default now(),
+ updated_at timestamptz not null default now(),
+ constraint youtube_uploads_user_request_key unique (user_id, client_request_id),
+ constraint youtube_uploads_state_check check (state in ('queued', 'uploading', 'processing', 'finalizing', 'done', 'waiting_quota', 'needs_reconnect', 'needs_review', 'failed', 'cancelled')),
+ constraint youtube_uploads_privacy_check check (privacy in ('private', 'unlisted', 'public')),
+ constraint youtube_uploads_title_check check (char_length(title) between 1 and 100),
+ constraint youtube_uploads_source_url_check check (source_url ~ '^https://'),
+ constraint youtube_uploads_source_bytes_check check (source_bytes > 0),
+ constraint youtube_uploads_bytes_confirmed_check check (bytes_confirmed between 0 and source_bytes),
+ -- YouTube only schedules private videos
+ constraint youtube_uploads_publish_at_check check (publish_at is null or privacy = 'private')
+);
+
+ALTER TABLE "public"."youtube_uploads"
+ ENABLE ROW LEVEL SECURITY;
+
+CREATE INDEX youtube_uploads_due_idx ON public.youtube_uploads USING btree (next_attempt_at) WHERE (state = ANY (ARRAY['queued'::text, 'uploading'::text, 'processing'::text, 'finalizing'::text, 'waiting_quota'::text]));
+
+CREATE INDEX youtube_uploads_user_created_idx ON public.youtube_uploads USING btree (user_id, created_at DESC);
+
+CREATE TRIGGER youtube_uploads_set_updated_at
+ BEFORE UPDATE ON public.youtube_uploads
+ FOR EACH ROW
+ EXECUTE FUNCTION public.set_updated_at();
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_uploads" TO "service_role";
+
+REVOKE ALL ON TABLE "public"."youtube_uploads" FROM "postgres";
+
+GRANT DELETE, INSERT, MAINTAIN, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON TABLE "public"."youtube_uploads" TO "postgres";
+
+REVOKE ALL ON TABLE "public"."youtube_uploads" FROM "anon", "authenticated";