|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 5 | +database_url="${SUPABASE_DB_URL:-postgresql://postgres:postgres@127.0.0.1:54322/postgres}" |
| 6 | +database_password="${SUPABASE_DB_PASSWORD:-postgres}" |
| 7 | +test_user_id="66666666-6666-4666-8666-666666666666" |
| 8 | +result_dir="$(mktemp -d)" |
| 9 | +first_pid="" |
| 10 | +second_pid="" |
| 11 | + |
| 12 | +cleanup() { |
| 13 | + touch "$result_dir/release-first" |
| 14 | + if [ -n "$first_pid" ]; then |
| 15 | + wait "$first_pid" 2>/dev/null || true |
| 16 | + fi |
| 17 | + if [ -n "$second_pid" ]; then |
| 18 | + wait "$second_pid" 2>/dev/null || true |
| 19 | + fi |
| 20 | + PGPASSWORD="$database_password" psql "$database_url" -qX --set ON_ERROR_STOP=1 \ |
| 21 | + --command "DELETE FROM auth.users WHERE id = '$test_user_id'" >/dev/null |
| 22 | + rm -rf "$result_dir" |
| 23 | +} |
| 24 | +trap cleanup EXIT |
| 25 | + |
| 26 | +( |
| 27 | + cd "$repo_root" |
| 28 | + PGPASSWORD="$database_password" psql "$database_url" -qX --set ON_ERROR_STOP=1 <<'SQL' |
| 29 | +BEGIN; |
| 30 | +INSERT INTO auth.users (id) |
| 31 | +VALUES ('77777777-7777-4777-8777-777777777777'); |
| 32 | +DROP FUNCTION public.consume_daily_quota(uuid, integer, text); |
| 33 | +CREATE FUNCTION public.consume_daily_quota(uuid, integer) |
| 34 | +RETURNS TABLE (allowed boolean, current_count integer) |
| 35 | +LANGUAGE sql |
| 36 | +AS $$ SELECT false, 0 $$; |
| 37 | +ALTER TABLE public.daily_usage_counters DROP CONSTRAINT daily_usage_counters_pkey; |
| 38 | +ALTER TABLE public.daily_usage_counters DROP COLUMN quota_key; |
| 39 | +ALTER TABLE public.daily_usage_counters |
| 40 | + ADD CONSTRAINT daily_usage_counters_pkey PRIMARY KEY (user_id, usage_date); |
| 41 | +INSERT INTO public.daily_usage_counters (user_id, usage_date, request_count) |
| 42 | +VALUES ('77777777-7777-4777-8777-777777777777', CURRENT_DATE - 1, 7); |
| 43 | +\i supabase/migrations/20260920125555_scope_daily_usage_counters.sql |
| 44 | +DO $$ |
| 45 | +BEGIN |
| 46 | + IF NOT EXISTS ( |
| 47 | + SELECT 1 |
| 48 | + FROM public.daily_usage_counters |
| 49 | + WHERE user_id = '77777777-7777-4777-8777-777777777777' |
| 50 | + AND usage_date = CURRENT_DATE - 1 |
| 51 | + AND quota_key = 'ai' |
| 52 | + AND request_count = 7 |
| 53 | + ) THEN |
| 54 | + RAISE EXCEPTION 'legacy daily usage row was not preserved under the ai quota'; |
| 55 | + END IF; |
| 56 | +END; |
| 57 | +$$; |
| 58 | +ROLLBACK; |
| 59 | +SQL |
| 60 | +) |
| 61 | +echo "Legacy quota row migration: count 7 preserved under ai" |
| 62 | + |
| 63 | +PGPASSWORD="$database_password" psql "$database_url" -qX --set ON_ERROR_STOP=1 \ |
| 64 | + --command "DELETE FROM auth.users WHERE id = '$test_user_id'; INSERT INTO auth.users (id) VALUES ('$test_user_id')" \ |
| 65 | + >/dev/null |
| 66 | + |
| 67 | +reserve_first() { |
| 68 | + PGPASSWORD="$database_password" psql "$database_url" -qAtX --set ON_ERROR_STOP=1 <<SQL |
| 69 | +BEGIN; |
| 70 | +SET LOCAL ROLE service_role; |
| 71 | +SELECT set_config('request.jwt.claim.role', 'service_role', true); |
| 72 | +SELECT allowed, current_count |
| 73 | +FROM public.consume_daily_quota('$test_user_id', 1, 'concurrent'); |
| 74 | +\! touch "$result_dir/first-holds-lock" |
| 75 | +\! while [ ! -e "$result_dir/release-first" ]; do sleep 0.05; done |
| 76 | +COMMIT; |
| 77 | +SQL |
| 78 | +} |
| 79 | + |
| 80 | +reserve_second() { |
| 81 | + PGPASSWORD="$database_password" psql "$database_url" -qAtX --set ON_ERROR_STOP=1 <<SQL |
| 82 | +BEGIN; |
| 83 | +SET LOCAL ROLE service_role; |
| 84 | +SELECT set_config('request.jwt.claim.role', 'service_role', true); |
| 85 | +SELECT allowed, current_count |
| 86 | +FROM public.consume_daily_quota('$test_user_id', 1, 'concurrent') /* daily_quota_concurrency_second */; |
| 87 | +COMMIT; |
| 88 | +SQL |
| 89 | +} |
| 90 | + |
| 91 | +reserve_first >"$result_dir/first" & |
| 92 | +first_pid=$! |
| 93 | + |
| 94 | +for _ in {1..100}; do |
| 95 | + if [ -e "$result_dir/first-holds-lock" ]; then |
| 96 | + break |
| 97 | + fi |
| 98 | + if ! kill -0 "$first_pid" 2>/dev/null; then |
| 99 | + wait "$first_pid" |
| 100 | + fi |
| 101 | + sleep 0.05 |
| 102 | +done |
| 103 | +if [ ! -e "$result_dir/first-holds-lock" ]; then |
| 104 | + echo "first quota reservation did not acquire its row lock" |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +reserve_second >"$result_dir/second" & |
| 109 | +second_pid=$! |
| 110 | + |
| 111 | +second_is_waiting=false |
| 112 | +for _ in {1..100}; do |
| 113 | + if [ "$(PGPASSWORD="$database_password" psql "$database_url" -qAtX --set ON_ERROR_STOP=1 --command \ |
| 114 | + "SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND wait_event_type = 'Lock' AND query LIKE '%daily_quota_concurrency_second%')")" = "t" ]; then |
| 115 | + second_is_waiting=true |
| 116 | + break |
| 117 | + fi |
| 118 | + if ! kill -0 "$second_pid" 2>/dev/null; then |
| 119 | + break |
| 120 | + fi |
| 121 | + sleep 0.05 |
| 122 | +done |
| 123 | +if [ "$second_is_waiting" != true ]; then |
| 124 | + echo "second quota reservation did not wait on the first row lock" |
| 125 | + exit 1 |
| 126 | +fi |
| 127 | + |
| 128 | +touch "$result_dir/release-first" |
| 129 | + |
| 130 | +first_result=0 |
| 131 | +second_result=0 |
| 132 | +wait "$first_pid" || first_result=$? |
| 133 | +wait "$second_pid" || second_result=$? |
| 134 | +if [ "$first_result" -ne 0 ] || [ "$second_result" -ne 0 ]; then |
| 135 | + exit 1 |
| 136 | +fi |
| 137 | + |
| 138 | +actual_results="$(grep -hE '^[tf]\|[0-9]+$' "$result_dir/first" "$result_dir/second" | sort)" |
| 139 | +expected_results=$'f|1\nt|1' |
| 140 | +if [ "$actual_results" != "$expected_results" ]; then |
| 141 | + echo "unexpected concurrent quota results:" |
| 142 | + echo "$actual_results" |
| 143 | + exit 1 |
| 144 | +fi |
| 145 | + |
| 146 | +stored_count="$(PGPASSWORD="$database_password" psql "$database_url" -qAtX --set ON_ERROR_STOP=1 --command \ |
| 147 | + "SELECT request_count FROM public.daily_usage_counters WHERE user_id = '$test_user_id' AND usage_date = CURRENT_DATE AND quota_key = 'concurrent'")" |
| 148 | +if [ "$stored_count" != "1" ]; then |
| 149 | + echo "expected one stored concurrent reservation, got: $stored_count" |
| 150 | + exit 1 |
| 151 | +fi |
| 152 | + |
| 153 | +echo "Concurrent quota reservations: 1 allowed, 1 rejected, stored count 1" |
0 commit comments