-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-supabase-table.js
More file actions
260 lines (220 loc) · 8.31 KB
/
Copy pathsetup-supabase-table.js
File metadata and controls
260 lines (220 loc) · 8.31 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<<<<<<< HEAD
const { createClient } = require('@supabase/supabase-js');
const dotenv = require('dotenv');
// Load environment variables
dotenv.config();
// Get Supabase credentials from environment variables
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
// Validate credentials
if (!supabaseUrl || !supabaseKey) {
console.error('Error: Supabase URL and Key are required in environment variables');
console.error('Please add them to your .env file:');
console.error('SUPABASE_URL=your_supabase_project_url_here');
console.error('SUPABASE_KEY=your_supabase_anon_key_here');
process.exit(1);
}
// Create Supabase client
const supabase = createClient(supabaseUrl, supabaseKey);
// Function to create the users table
async function createUsersTable() {
try {
console.log('Connecting to Supabase...');
// Test the connection first
const { data, error } = await supabase
.from('users')
.select('id')
.limit(1);
// If we get an error about the table not existing, that's expected
if (error && !error.message.includes('relation "users" does not exist')) {
console.error('Connection error:', error.message);
process.exit(1);
}
console.log('Connection successful!');
console.log('Creating users table...');
// Create the users table using raw SQL
const { error: createError } = await supabase.rpc('exec_sql', {
sql: `
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
`
});
if (createError) {
// If rpc method doesn't work, try direct approach
console.log('Trying alternative method...');
// Since we can't directly execute DDL statements through the JS client,
// we'll show the SQL that needs to be executed
console.log('\nPlease execute the following SQL in your Supabase SQL Editor:');
console.log(`
-- Create the users table for astrological analysis reports
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
-- Enable RLS (Row Level Security) if needed
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
`);
process.exit(1);
} else {
console.log('Users table created successfully!');
}
} catch (error) {
console.error('Error:', error.message);
// Provide instructions for manual setup
console.log('\nPlease execute the following SQL in your Supabase SQL Editor:');
console.log(`
-- Create the users table for astrological analysis reports
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
`);
process.exit(1);
}
}
// Run the function
=======
const { createClient } = require('@supabase/supabase-js');
const dotenv = require('dotenv');
// Load environment variables
dotenv.config();
// Get Supabase credentials from environment variables
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
// Validate credentials
if (!supabaseUrl || !supabaseKey) {
console.error('Error: Supabase URL and Key are required in environment variables');
console.error('Please add them to your .env file:');
console.error('SUPABASE_URL=your_supabase_project_url_here');
console.error('SUPABASE_KEY=your_supabase_anon_key_here');
process.exit(1);
}
// Create Supabase client
const supabase = createClient(supabaseUrl, supabaseKey);
// Function to create the users table
async function createUsersTable() {
try {
console.log('Connecting to Supabase...');
// Test the connection first
const { data, error } = await supabase
.from('users')
.select('id')
.limit(1);
// If we get an error about the table not existing, that's expected
if (error && !error.message.includes('relation "users" does not exist')) {
console.error('Connection error:', error.message);
process.exit(1);
}
console.log('Connection successful!');
console.log('Creating users table...');
// Create the users table using raw SQL
const { error: createError } = await supabase.rpc('exec_sql', {
sql: `
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
`
});
if (createError) {
// If rpc method doesn't work, try direct approach
console.log('Trying alternative method...');
// Since we can't directly execute DDL statements through the JS client,
// we'll show the SQL that needs to be executed
console.log('\nPlease execute the following SQL in your Supabase SQL Editor:');
console.log(`
-- Create the users table for astrological analysis reports
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
-- Enable RLS (Row Level Security) if needed
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
`);
process.exit(1);
} else {
console.log('Users table created successfully!');
}
} catch (error) {
console.error('Error:', error.message);
// Provide instructions for manual setup
console.log('\nPlease execute the following SQL in your Supabase SQL Editor:');
console.log(`
-- Create the users table for astrological analysis reports
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birth_date DATE NOT NULL,
birth_time TIME NOT NULL,
birth_place TEXT NOT NULL,
face_image_path TEXT,
hand_image_path TEXT,
analysis_result JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Add indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
CREATE INDEX IF NOT EXISTS idx_users_last_name ON users (last_name);
`);
process.exit(1);
}
}
// Run the function
>>>>>>> 4939126ee5c953624a0df2c93b0c18153fd12173
createUsersTable();