Improve comments in database.go+

This commit is contained in:
Marco Realacci 2022-12-10 02:08:05 +01:00
parent 0d02082785
commit 607fde717d

View file

@ -97,13 +97,14 @@ func New(db *sql.DB) (AppDatabase, error) {
// Check if tables exist. If not, the database is empty, and we need to create the structure // Check if tables exist. If not, the database is empty, and we need to create the structure
var tableName string var tableName string
err := db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='users';`).Scan(&tableName) //todo: check for all the tables //todo: check for all the tables, not just users
err := db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='users';`).Scan(&tableName)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
sqlStmt := `CREATE TABLE "users" ( sqlStmt := `CREATE TABLE "users" (
"uid" TEXT NOT NULL, "uid" TEXT NOT NULL,
"name" TEXT NOT NULL UNIQUE, "name" TEXT NOT NULL UNIQUE,
PRIMARY KEY("uid") PRIMARY KEY("uid")
)` //todo: one query is enough! Why do I need to do this? )` // todo: one query is enough! We are we doing a query per table?
_, err = db.Exec(sqlStmt) _, err = db.Exec(sqlStmt)
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating database structure: %w", err) return nil, fmt.Errorf("error creating database structure: %w", err)