add: db migrations & update
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,11 +3,13 @@ package database
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/pressly/goose/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "modernc.org/sqlite"
|
||||
"reichard.io/antholume/config"
|
||||
@@ -22,6 +24,9 @@ type DBManager struct {
|
||||
//go:embed schema.sql
|
||||
var ddl string
|
||||
|
||||
//go:embed migrations/*
|
||||
var migrations embed.FS
|
||||
|
||||
func NewMgr(c *config.Config) *DBManager {
|
||||
// Create Manager
|
||||
dbm := &DBManager{
|
||||
@@ -38,16 +43,27 @@ func NewMgr(c *config.Config) *DBManager {
|
||||
var err error
|
||||
dbm.DB, err = sql.Open("sqlite", dbLocation)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatalf("[NewMgr] Unable to open DB: %v", err)
|
||||
}
|
||||
|
||||
// Single Open Connection
|
||||
dbm.DB.SetMaxOpenConns(1)
|
||||
|
||||
// Execute DDL
|
||||
if _, err := dbm.DB.Exec(ddl, nil); err != nil {
|
||||
log.Info("Exec Error:", err)
|
||||
log.Fatalf("[NewMgr] Error executing schema: %v", err)
|
||||
}
|
||||
|
||||
// Perform Migrations
|
||||
err = dbm.performMigrations()
|
||||
if err != nil && err != goose.ErrNoMigrationFiles {
|
||||
log.Fatalf("[NewMgr] Error running DB migrations: %v", err)
|
||||
}
|
||||
|
||||
// Cache Tables
|
||||
dbm.CacheTempTables()
|
||||
} else {
|
||||
log.Fatal("Unsupported Database")
|
||||
log.Fatal("[NewMgr] Unsupported Database")
|
||||
}
|
||||
|
||||
dbm.Queries = New(dbm.DB)
|
||||
@@ -82,3 +98,13 @@ func (dbm *DBManager) CacheTempTables() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dbm *DBManager) performMigrations() error {
|
||||
// Set DB Migration
|
||||
goose.SetBaseFS(migrations)
|
||||
|
||||
// Run Migrations
|
||||
goose.SetLogger(log.StandardLogger())
|
||||
goose.SetDialect("sqlite")
|
||||
return goose.Up(dbm.DB, "migrations")
|
||||
}
|
||||
|
||||
5
database/migrations/README.md
Normal file
5
database/migrations/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# DB Migrations
|
||||
|
||||
```bash
|
||||
goose create migration_name sql
|
||||
```
|
||||
@@ -167,15 +167,19 @@ CREATE INDEX IF NOT EXISTS activity_user_id_document_id ON activity (
|
||||
document_id
|
||||
);
|
||||
|
||||
|
||||
---------------------------------------------------------------
|
||||
---------------------------- Views ----------------------------
|
||||
---------------------------------------------------------------
|
||||
|
||||
DROP VIEW IF EXISTS view_user_streaks;
|
||||
DROP VIEW IF EXISTS view_document_user_statistics;
|
||||
|
||||
--------------------------------
|
||||
--------- User Streaks ---------
|
||||
--------------------------------
|
||||
|
||||
CREATE VIEW IF NOT EXISTS view_user_streaks AS
|
||||
CREATE VIEW view_user_streaks AS
|
||||
|
||||
WITH document_windows AS (
|
||||
SELECT
|
||||
@@ -290,7 +294,7 @@ LEFT JOIN current_streak ON
|
||||
------- Document Stats ---------
|
||||
--------------------------------
|
||||
|
||||
CREATE VIEW IF NOT EXISTS view_document_user_statistics AS
|
||||
CREATE VIEW view_document_user_statistics AS
|
||||
|
||||
WITH intermediate_ga AS (
|
||||
SELECT
|
||||
@@ -465,11 +469,6 @@ INNER JOIN
|
||||
GROUP BY ga.document_id, ga.user_id
|
||||
ORDER BY total_wpm DESC;
|
||||
|
||||
---------------------------------------------------------------
|
||||
------------------ Populate Temporary Tables ------------------
|
||||
---------------------------------------------------------------
|
||||
INSERT INTO user_streaks SELECT * FROM view_user_streaks;
|
||||
INSERT INTO document_user_statistics SELECT * FROM view_document_user_statistics;
|
||||
|
||||
---------------------------------------------------------------
|
||||
--------------------------- Triggers --------------------------
|
||||
|
||||
Reference in New Issue
Block a user