1+ import process from "node:process"
12import type { Database } from "db0"
23import type { UserInfo } from "#/types"
34
@@ -8,28 +9,58 @@ export class UserTable {
89 }
910
1011 async init ( ) {
11- await this . db . prepare ( `
12- CREATE TABLE IF NOT EXISTS user (
13- id TEXT PRIMARY KEY,
14- email TEXT,
15- data TEXT,
16- type TEXT,
17- created INTEGER,
18- updated INTEGER
19- );
20- ` ) . run ( )
21- await this . db . prepare ( `
22- CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
23- ` ) . run ( )
12+ // Check if we're using MySQL by looking for MySQL environment variables
13+ const isMySQL = process . env . MYSQL_HOST && process . env . MYSQL_USER && process . env . MYSQL_PASSWORD && process . env . MYSQL_DATABASE
14+
15+ if ( isMySQL ) {
16+ // MySQL syntax
17+ await this . db . prepare ( `
18+ CREATE TABLE IF NOT EXISTS user (
19+ id VARCHAR(255) PRIMARY KEY,
20+ email VARCHAR(255),
21+ data TEXT,
22+ type VARCHAR(50),
23+ created BIGINT,
24+ updated BIGINT
25+ );
26+ ` ) . run ( )
27+ await this . db . prepare ( `
28+ CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
29+ ` ) . run ( )
30+ } else {
31+ // SQLite syntax
32+ await this . db . prepare ( `
33+ CREATE TABLE IF NOT EXISTS user (
34+ id TEXT PRIMARY KEY,
35+ email TEXT,
36+ data TEXT,
37+ type TEXT,
38+ created INTEGER,
39+ updated INTEGER
40+ );
41+ ` ) . run ( )
42+ await this . db . prepare ( `
43+ CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
44+ ` ) . run ( )
45+ }
2446 logger . success ( `init user table` )
2547 }
2648
2749 async addUser ( id : string , email : string , type : "github" ) {
2850 const u = await this . getUser ( id )
2951 const now = Date . now ( )
52+ const isMySQL = process . env . MYSQL_HOST && process . env . MYSQL_USER && process . env . MYSQL_PASSWORD && process . env . MYSQL_DATABASE
53+
3054 if ( ! u ) {
31- await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)` )
32- . run ( id , email , "" , type , now , now )
55+ if ( isMySQL ) {
56+ // MySQL syntax - use ON DUPLICATE KEY UPDATE
57+ await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE email = VALUES(email), updated = VALUES(updated)` )
58+ . run ( id , email , "" , type , now , now )
59+ } else {
60+ // SQLite syntax
61+ await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)` )
62+ . run ( id , email , "" , type , now , now )
63+ }
3364 logger . success ( `add user ${ id } ` )
3465 } else if ( u . email !== email && u . type !== type ) {
3566 await this . db . prepare ( `UPDATE user SET email = ?, updated = ? WHERE id = ?` ) . run ( email , now , id )
0 commit comments