InitDB creates a new sqlite database based on the file at path.
(path string)
| 47 | |
| 48 | // InitDB creates a new sqlite database based on the file at path. |
| 49 | func InitDB(path string) error { |
| 50 | db, err := sql.Open("sqlite", path) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | defer db.Close() |
| 55 | for _, tableSQL := range SQLCreateTables() { |
| 56 | if _, err := db.Exec(tableSQL); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Use Write Ahead Logging which improves SQLite concurrency. |
| 62 | // Requires SQLite >= 3.7.0 |
| 63 | if _, err := db.Exec("PRAGMA journal_mode = WAL"); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | // Check if the WAL mode was set correctly |
| 68 | var journalMode string |
| 69 | if err = db.QueryRow("PRAGMA journal_mode").Scan(&journalMode); err != nil { |
| 70 | log.Fatalf("Unable to determine sqlite3 journal_mode: %v", err) |
| 71 | } |
| 72 | if journalMode != "wal" { |
| 73 | log.Fatal("SQLite Write Ahead Logging (introducted in v3.7.0) is required. See http://perkeep.org/issue/114") |
| 74 | } |
| 75 | |
| 76 | _, err = db.Exec(fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, SchemaVersion())) |
| 77 | return err |
| 78 | } |
no test coverage detected