Skip to content

Commit a087695

Browse files
authored
Merge pull request #229 from codeunia-dev/fix/username-update-database-function
Fix: Resolve username update database function error
2 parents 754843e + 4a73bfc commit a087695

File tree

7 files changed

+590
-1
lines changed

7 files changed

+590
-1
lines changed

check-database-status.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- =====================================================
2+
-- DATABASE STATUS CHECK
3+
-- =====================================================
4+
-- Copy and paste this entire query into your Supabase SQL Editor
5+
-- and share the results with me
6+
7+
-- 1. Check if profiles table exists
8+
SELECT
9+
'Table Check' as check_type,
10+
table_name,
11+
CASE
12+
WHEN table_name = 'profiles' THEN 'EXISTS'
13+
ELSE 'OTHER TABLE'
14+
END as status
15+
FROM information_schema.tables
16+
WHERE table_schema = 'public'
17+
AND table_name = 'profiles';
18+
19+
-- 2. Check if update_username function exists
20+
SELECT
21+
'Function Check' as check_type,
22+
proname as function_name,
23+
CASE
24+
WHEN proname = 'update_username' THEN 'EXISTS'
25+
ELSE 'OTHER FUNCTION'
26+
END as status
27+
FROM pg_proc
28+
WHERE proname = 'update_username';
29+
30+
-- 3. Check if set_username function exists
31+
SELECT
32+
'Function Check' as check_type,
33+
proname as function_name,
34+
CASE
35+
WHEN proname = 'set_username' THEN 'EXISTS'
36+
ELSE 'OTHER FUNCTION'
37+
END as status
38+
FROM pg_proc
39+
WHERE proname = 'set_username';
40+
41+
-- 4. List all tables in public schema
42+
SELECT
43+
'All Tables' as check_type,
44+
table_name,
45+
'EXISTS' as status
46+
FROM information_schema.tables
47+
WHERE table_schema = 'public'
48+
ORDER BY table_name;
49+
50+
-- 5. List all functions in public schema
51+
SELECT
52+
'All Functions' as check_type,
53+
proname as function_name,
54+
'EXISTS' as status
55+
FROM pg_proc
56+
WHERE pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public')
57+
ORDER BY proname;

check-schema-permissions.sql

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- =====================================================
2+
-- CHECK SCHEMA AND PERMISSIONS
3+
-- =====================================================
4+
-- Copy and paste this query to check schema and permissions
5+
6+
-- 1. Check current schema
7+
SELECT
8+
'Current Schema' as check_type,
9+
current_schema() as schema_name,
10+
'Current schema being used' as info;
11+
12+
-- 2. Check if profiles table exists in different schemas
13+
SELECT
14+
'Table Schema Check' as check_type,
15+
table_schema,
16+
table_name,
17+
'EXISTS' as status
18+
FROM information_schema.tables
19+
WHERE table_name = 'profiles'
20+
ORDER BY table_schema;
21+
22+
-- 3. Check table permissions for current user
23+
SELECT
24+
'Table Permissions' as check_type,
25+
table_name,
26+
privilege_type,
27+
grantee
28+
FROM information_schema.table_privileges
29+
WHERE table_name = 'profiles'
30+
AND table_schema = 'public';
31+
32+
-- 4. Check function permissions
33+
SELECT
34+
'Function Permissions' as check_type,
35+
routine_name,
36+
privilege_type,
37+
grantee
38+
FROM information_schema.routine_privileges
39+
WHERE routine_name = 'update_username'
40+
AND routine_schema = 'public';
41+
42+
-- 5. Check if we can access profiles table directly
43+
SELECT
44+
'Direct Access Test' as check_type,
45+
COUNT(*) as row_count,
46+
'Can access profiles table' as info
47+
FROM public.profiles;
48+
49+
-- 6. Check search_path
50+
SELECT
51+
'Search Path' as check_type,
52+
setting as search_path,
53+
'Current search path' as info
54+
FROM pg_settings
55+
WHERE name = 'search_path';

components/UsernameField.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ export function UsernameField({
109109
})
110110

111111
if (error) {
112-
if (error.message.includes('Username can only be changed once')) {
112+
// Handle specific database errors
113+
if (error.message.includes('relation "profiles" does not exist')) {
114+
setError("Database setup incomplete. Please contact support.")
115+
console.error('Profiles table does not exist. Database migration needed.')
116+
} else if (error.message.includes('Username can only be changed once')) {
113117
setError("You can only change your username once")
114118
} else if (error.message.includes('Username is already taken')) {
115119
setError("Username is already taken")

fix-username-function.sql

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- =====================================================
2+
-- FIX UPDATE_USERNAME FUNCTION
3+
-- =====================================================
4+
-- Copy and paste this query to fix the update_username function
5+
6+
-- Drop and recreate the function with proper schema references
7+
DROP FUNCTION IF EXISTS update_username(UUID, TEXT);
8+
9+
CREATE OR REPLACE FUNCTION update_username(
10+
user_id UUID,
11+
new_username TEXT
12+
)
13+
RETURNS BOOLEAN
14+
LANGUAGE plpgsql
15+
SECURITY DEFINER
16+
AS $$
17+
DECLARE
18+
can_edit BOOLEAN;
19+
username_exists BOOLEAN;
20+
BEGIN
21+
-- Check if user can still edit username
22+
SELECT username_editable INTO can_edit
23+
FROM public.profiles
24+
WHERE id = user_id;
25+
26+
IF NOT FOUND THEN
27+
RETURN FALSE;
28+
END IF;
29+
30+
IF NOT can_edit THEN
31+
RETURN FALSE; -- Username already edited once
32+
END IF;
33+
34+
-- Check if new username already exists
35+
SELECT EXISTS(SELECT 1 FROM public.profiles WHERE username = new_username AND id != user_id)
36+
INTO username_exists;
37+
38+
IF username_exists THEN
39+
RETURN FALSE; -- Username already taken
40+
END IF;
41+
42+
-- Update username and mark as non-editable
43+
UPDATE public.profiles
44+
SET username = new_username, username_editable = FALSE
45+
WHERE id = user_id;
46+
47+
RETURN TRUE;
48+
END;
49+
$$;
50+
51+
-- Grant execute permission to authenticated users
52+
GRANT EXECUTE ON FUNCTION update_username(UUID, TEXT) TO authenticated;
53+
GRANT EXECUTE ON FUNCTION update_username(UUID, TEXT) TO anon;
54+
GRANT EXECUTE ON FUNCTION update_username(UUID, TEXT) TO service_role;
55+
56+
-- Test the function
57+
SELECT
58+
'Function Fixed' as status,
59+
'update_username function has been updated with proper schema references' as message;

0 commit comments

Comments
 (0)