- // YubiKeys table
- $db->exec(
- "CREATE TABLE IF NOT EXISTS yubikeys (
- yubikey_id SERIAL PRIMARY KEY,
- user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
- credential_id TEXT NOT NULL UNIQUE,
- public_key TEXT NOT NULL,
- counter BIGINT DEFAULT 0,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- UNIQUE(user_id, credential_id)
- );"
- );
-
- // Triplets table
- $db->exec(
- "CREATE TABLE IF NOT EXISTS triplets (
- triplet_id SERIAL PRIMARY KEY,
- user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
- label VARCHAR(255) NOT NULL,
- keyword VARCHAR(255) NOT NULL,
- action VARCHAR(255) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
- );"
- );
-
- // Settings table for user preferences
- $db->exec(
- "CREATE TABLE IF NOT EXISTS user_settings (
- setting_id SERIAL PRIMARY KEY,
- user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
- setting_name VARCHAR(255) NOT NULL,
- setting_value TEXT,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- UNIQUE(user_id, setting_name)
- );"
- );
-
- // Create indexes
- $db->exec("CREATE INDEX IF NOT EXISTS idx_triplets_user_id ON triplets(user_id);");
- $db->exec("CREATE INDEX IF NOT EXISTS idx_yubikeys_user_id ON yubikeys(user_id);");
- $db->exec("CREATE INDEX IF NOT EXISTS idx_triplets_keyword ON triplets(keyword);");
- $db->exec("CREATE INDEX IF NOT EXISTS idx_settings_user ON user_settings(user_id);");
+ if (!$users_exists) {
+ // Users table
+ $db->exec(
+ "CREATE TABLE IF NOT EXISTS users (
+ user_id SERIAL PRIMARY KEY,
+ username VARCHAR(255) NOT NULL UNIQUE,
+ yubikey_id INT UNIQUE,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ );"
+ );
+
+ // YubiKeys table
+ $db->exec(
+ "CREATE TABLE IF NOT EXISTS yubikeys (
+ yubikey_id SERIAL PRIMARY KEY,
+ user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
+ credential_id TEXT NOT NULL UNIQUE,
+ public_key TEXT NOT NULL,
+ counter BIGINT DEFAULT 0,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ UNIQUE(user_id, credential_id)
+ );"
+ );
+
+ // Add foreign key constraint for users.yubikey_id
+ $result = $db->query("SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'users_yubikey_id_fkey' AND table_name = 'users'");
+ $constraint_exists = $result->fetchColumn();
+ if (!$constraint_exists) {
+ $db->exec("ALTER TABLE users ADD CONSTRAINT users_yubikey_id_fkey FOREIGN KEY (yubikey_id) REFERENCES yubikeys(yubikey_id) ON DELETE SET NULL;");
+ }
+
+ // Triplets table
+ $db->exec(
+ "CREATE TABLE IF NOT EXISTS triplets (
+ triplet_id SERIAL PRIMARY KEY,
+ user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
+ label VARCHAR(255) NOT NULL,
+ keyword VARCHAR(255) NOT NULL,
+ action VARCHAR(255) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ );"
+ );
+
+ // Settings table for user preferences
+ $db->exec(
+ "CREATE TABLE IF NOT EXISTS user_settings (
+ setting_id SERIAL PRIMARY KEY,
+ user_id INT NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
+ setting_name VARCHAR(255) NOT NULL,
+ setting_value TEXT,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ UNIQUE(user_id, setting_name)
+ );"
+ );
+
+ // Create indexes if they don't exist
+ $db->exec("CREATE INDEX IF NOT EXISTS idx_triplets_user_id ON triplets(user_id);");
+ $db->exec("CREATE INDEX IF NOT EXISTS idx_yubikeys_user_id ON yubikeys(user_id);");
+ $db->exec("CREATE INDEX IF NOT EXISTS idx_triplets_keyword ON triplets(keyword);");
+ $db->exec("CREATE INDEX IF NOT EXISTS idx_settings_user ON user_settings(user_id);");
+ }