-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-supabase.js
More file actions
63 lines (53 loc) · 2.11 KB
/
Copy pathsetup-supabase.js
File metadata and controls
63 lines (53 loc) · 2.11 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
const { Client } = require('pg');
const dns = require('dns');
// Force Node.js to prefer IPv6 since Supabase db host only has AAAA record
dns.setDefaultResultOrder('verbatim');
const client = new Client({
host: 'db.sgmjstwrvldwvdpkpsbq.supabase.co',
port: 5432,
database: 'postgres',
user: 'postgres',
password: 'Jeederh123@',
ssl: { rejectUnauthorized: false },
});
async function run() {
await client.connect();
console.log('Connected to Supabase');
// Create users table
await client.query(`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`);
console.log('users table created');
// Migration 001: Add wallet columns
await client.query(`ALTER TABLE users ADD COLUMN IF NOT EXISTS wallet_id TEXT;`);
await client.query(`ALTER TABLE users ADD COLUMN IF NOT EXISTS wallet_address TEXT;`);
console.log('Migration 001 done: wallet columns added');
// Migration 002: Create transactions table
await client.query(`
CREATE TABLE IF NOT EXISTS transactions (
id SERIAL PRIMARY KEY,
sender_id INTEGER REFERENCES users(id),
receiver_id INTEGER REFERENCES users(id),
amount NUMERIC NOT NULL,
tx_hash TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`);
console.log('Migration 002 done: transactions table created');
// Migration 003: Nullable receiver_id + receiver_address
await client.query(`ALTER TABLE transactions ALTER COLUMN receiver_id DROP NOT NULL;`);
await client.query(`ALTER TABLE transactions ADD COLUMN IF NOT EXISTS receiver_address TEXT;`);
console.log('Migration 003 done: nullable receiver_id + receiver_address');
// Verify
const tables = await client.query(`SELECT tablename FROM pg_tables WHERE schemaname = 'public';`);
console.log('Tables:', tables.rows.map(r => r.tablename).join(', '));
await client.end();
console.log('Done!');
}
run().catch(err => { console.error(err); process.exit(1); });