InitDB initializes the database. Ideally this process must be a part of migration sequence. But it is performed seaprately because it is a prerequisite for legacy migration.
(ctx context.DnoteCtx)
| 187 | // Ideally this process must be a part of migration sequence. But it is performed |
| 188 | // seaprately because it is a prerequisite for legacy migration. |
| 189 | func InitDB(ctx context.DnoteCtx) error { |
| 190 | log.Debug("initializing the database\n") |
| 191 | |
| 192 | db := ctx.DB |
| 193 | |
| 194 | _, err := db.Exec(`CREATE TABLE IF NOT EXISTS notes |
| 195 | ( |
| 196 | id integer PRIMARY KEY AUTOINCREMENT, |
| 197 | uuid text NOT NULL, |
| 198 | book_uuid text NOT NULL, |
| 199 | content text NOT NULL, |
| 200 | added_on integer NOT NULL, |
| 201 | edited_on integer DEFAULT 0, |
| 202 | public bool DEFAULT false |
| 203 | )`) |
| 204 | if err != nil { |
| 205 | return errors.Wrap(err, "creating notes table") |
| 206 | } |
| 207 | |
| 208 | _, err = db.Exec(`CREATE TABLE IF NOT EXISTS books |
| 209 | ( |
| 210 | uuid text PRIMARY KEY, |
| 211 | label text NOT NULL |
| 212 | )`) |
| 213 | if err != nil { |
| 214 | return errors.Wrap(err, "creating books table") |
| 215 | } |
| 216 | |
| 217 | _, err = db.Exec(`CREATE TABLE IF NOT EXISTS system |
| 218 | ( |
| 219 | key string NOT NULL, |
| 220 | value text NOT NULL |
| 221 | )`) |
| 222 | if err != nil { |
| 223 | return errors.Wrap(err, "creating system table") |
| 224 | } |
| 225 | |
| 226 | _, err = db.Exec(`CREATE TABLE IF NOT EXISTS actions |
| 227 | ( |
| 228 | uuid text PRIMARY KEY, |
| 229 | schema integer NOT NULL, |
| 230 | type text NOT NULL, |
| 231 | data text NOT NULL, |
| 232 | timestamp integer NOT NULL |
| 233 | )`) |
| 234 | if err != nil { |
| 235 | return errors.Wrap(err, "creating actions table") |
| 236 | } |
| 237 | |
| 238 | _, err = db.Exec(` |
| 239 | CREATE UNIQUE INDEX IF NOT EXISTS idx_books_label ON books(label); |
| 240 | CREATE UNIQUE INDEX IF NOT EXISTS idx_notes_uuid ON notes(uuid); |
| 241 | CREATE UNIQUE INDEX IF NOT EXISTS idx_books_uuid ON books(uuid); |
| 242 | CREATE INDEX IF NOT EXISTS idx_notes_book_uuid ON notes(book_uuid);`) |
| 243 | if err != nil { |
| 244 | return errors.Wrap(err, "creating indices") |
| 245 | } |
| 246 |
no test coverage detected