Brijesh's Git Server — whodis @ 9855dd11af38dc3c6f883757b5d07e963c3dc2e2

built this as a refresher on handling webauthn

core/internal/database/create-tables.go (view raw)

 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
package database

import (
	"context"
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

func (s *service) CreateTables(ctx context.Context) error {
	return s.withTransaction(ctx, func(tx *sql.Tx) error {
		_, err := tx.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS users (
			id TEXT PRIMARY KEY,
			name TEXT NOT NULL,
			display_name TEXT NOT NULL,
			created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
		);`)
		if err != nil {
			return err
		}

		_, err = tx.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS credentials (
			id TEXT PRIMARY KEY,
			user_id TEXT NOT NULL,
			public_key BLOB NOT NULL,
			credential_id BLOB NOT NULL,
			sign_count INTEGER NOT NULL,
			aaguid BLOB,
			clone_warning BOOLEAN NOT NULL DEFAULT false,
			attachment TEXT,
			backup_eligible BOOLEAN NOT NULL DEFAULT false,
			backup_state BOOLEAN NOT NULL DEFAULT false,
			created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
			FOREIGN KEY (user_id) REFERENCES users(id)
		);`)
		if err != nil {
			return err
		}

		return nil
	})
}