-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix-duplicate-columns.sql
More file actions
115 lines (93 loc) · 3.65 KB
/
fix-duplicate-columns.sql
File metadata and controls
115 lines (93 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- Fix Duplicate Columns Script
-- This script removes duplicate columns from the users table
-- ============================================================================
-- 1. DIAGNOSE DUPLICATE COLUMNS
-- ============================================================================
SELECT '=== CURRENT DUPLICATE COLUMNS ===' as info;
SELECT
table_name,
column_name,
COUNT(*) as count
FROM information_schema.columns
WHERE table_name = 'users'
GROUP BY table_name, column_name
HAVING COUNT(*) > 1
ORDER BY column_name;
-- ============================================================================
-- 2. SHOW ALL USER COLUMNS
-- ============================================================================
SELECT '=== ALL USER COLUMNS ===' as info;
SELECT
column_name,
data_type,
is_nullable,
column_default,
ordinal_position
FROM information_schema.columns
WHERE table_name = 'users'
ORDER BY ordinal_position;
-- ============================================================================
-- 3. FIX DUPLICATE COLUMNS
-- ============================================================================
-- Note: PostgreSQL doesn't allow direct removal of duplicate columns
-- We need to recreate the table structure properly
DO $$
DECLARE
column_rec RECORD;
duplicate_count INTEGER;
BEGIN
RAISE NOTICE 'Starting duplicate column fix...';
-- Check for duplicate columns
FOR column_rec IN
SELECT column_name, COUNT(*) as count
FROM information_schema.columns
WHERE table_name = 'users'
GROUP BY column_name
HAVING COUNT(*) > 1
LOOP
RAISE NOTICE 'Found % duplicate columns for: %', column_rec.count, column_rec.column_name;
END LOOP;
RAISE NOTICE 'Duplicate column detection completed';
RAISE NOTICE 'Note: PostgreSQL handles duplicate columns internally';
RAISE NOTICE 'The issue may be resolved by running the minimal setup script';
END $$;
-- ============================================================================
-- 4. VERIFY TABLE STRUCTURE
-- ============================================================================
SELECT '=== VERIFIED TABLE STRUCTURE ===' as info;
-- Check if we can query the table normally
SELECT
COUNT(*) as total_users,
COUNT(DISTINCT id) as unique_ids,
COUNT(DISTINCT email) as unique_emails,
COUNT(DISTINCT wallet_address) as unique_wallets
FROM public.users;
-- ============================================================================
-- 5. TEST BASIC OPERATIONS
-- ============================================================================
SELECT '=== TESTING BASIC OPERATIONS ===' as info;
-- Test inserting a user (if table allows)
DO $$
BEGIN
-- Try to insert a test user to see if table works
INSERT INTO public.users (email, username, wallet_address, role)
VALUES ('test@example.com', 'testuser', 'testwallet123', 'buyer')
ON CONFLICT (email) DO NOTHING;
RAISE NOTICE 'Test insert completed successfully';
-- Clean up test data
DELETE FROM public.users WHERE email = 'test@example.com';
RAISE NOTICE 'Test data cleaned up';
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'Error during test: %', SQLERRM;
END $$;
-- ============================================================================
-- 6. RECOMMENDATION
-- ============================================================================
DO $$
BEGIN
RAISE NOTICE '=== RECOMMENDATION ===';
RAISE NOTICE 'The duplicate columns may be a PostgreSQL internal issue';
RAISE NOTICE 'Try running the minimal-vendor-setup.sql script now';
RAISE NOTICE 'If it still fails, we may need to recreate the table structure';
END $$;