-
Notifications
You must be signed in to change notification settings - Fork 10
/
db.go
46 lines (35 loc) · 821 Bytes
/
db.go
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
package main
import (
"database/sql"
"log"
)
var db *sql.DB
func GetConnection() *sql.DB {
if db != nil {
return db
}
db, err := sql.Open("sqlite3", "notesDB.sqlite")
if err != nil {
log.Fatalf("🔥 failed to connect to the database: %s", err.Error())
}
log.Println("🚀 Connected Successfully to the Database")
return db
}
func MakeMigrations() error {
db := GetConnection()
stmt := `CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(64) UNIQUE CHECK(title IS NULL OR length(title) <= 64),
description VARCHAR(255) NULL,
completed BOOLEAN DEFAULT(FALSE),
created_at TIMESTAMP DEFAULT DATETIME
);`
_, err := db.Exec(stmt)
if err != nil {
return err
}
return nil
}
/*
https://noties.io/blog/2019/08/19/sqlite-toggle-boolean/index.html
*/