Skip to content

Commit

Permalink
Merge pull request #267 from vsimakhin/feature/add-db-schema-version
Browse files Browse the repository at this point in the history
add shcema version and check to exclude recreate views on every start up
  • Loading branch information
vsimakhin authored Dec 13, 2024
2 parents 6b3cecc + ccf506e commit 7b5fcd4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- Fix: Add database schema version and check, so the app will not recreate DB views on every start up.
- Update: There are few major updates for the docker files and image containers
- The base image changed from `debian:bookworm-slim` to `alpine`, which reduced the container image size 3x - from 104MB to 37MB
- The image supports `linux/amd64` and `linux/arm64` now
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You also can easily export all flight records into EASA style pdf format, print

## [Unreleased]

- Fix: Add database schema version and check, so the app will not recreate DB views on every start up.
- Update: There are few major updates for the docker files and image containers
- The base image changed from `debian:bookworm-slim` to `alpine`, which reduced the container image size 3x - from 104MB to 37MB
- The image supports `linux/amd64` and `linux/arm64` now
Expand Down
8 changes: 8 additions & 0 deletions internal/driver/db_structure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package driver

var (
schemaVersion = "2.0.1"

UUID = ColumnType{SQLite: "TEXT", MySQL: "VARCHAR(36)"}
DateTime = ColumnType{SQLite: "TEXT", MySQL: "VARCHAR(10)"}
SmallText = ColumnType{SQLite: "TEXT", MySQL: "VARCHAR(255)"}
Expand All @@ -12,6 +14,12 @@ var (
Blob = ColumnType{SQLite: "BLOB", MySQL: "LONGBLOB"}
)

var metadataTable = NewTable("metadata", "id", ColumnType{SQLite: "INTEGER", MySQL: "INT UNSIGNED AUTO_INCREMENT"},
[]Column{
{Name: "version", Type: ColumnType{SQLite: "VARCHAR(32)", MySQL: "VARCHAR(32)"}, Properties: "NOT NULL"},
{Name: "created_at", Type: DateTime},
})

var logbookTable = NewTable("logbook", "uuid", UUID,
[]Column{
{Name: "date", Type: DateTime, Properties: "NOT NULL"},
Expand Down
74 changes: 59 additions & 15 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"time"

_ "embed"
Expand Down Expand Up @@ -39,32 +40,75 @@ func OpenDB(engine string, dsn string) (*sql.DB, error) {

// validateDB creates db structure in case it's a first run and the schema is empty
func validateDB(db *sql.DB, engine string) error {
// check tables
tables := []*Table{logbookTable, airportsTable, customAirportsTable,
settingsTable, licensingTable, attachmentsTable, sessionsTable,
}
metadataTable.initTable(db, engine)
isNewSchema := newShema(db)
if isNewSchema {
// check tables
tables := []*Table{logbookTable, airportsTable, customAirportsTable,
settingsTable, licensingTable, attachmentsTable, sessionsTable,
}

for _, table := range tables {
if err := table.initTable(db, engine); err != nil {
return err
}
}

// check views
views := []*View{logbookView, airportsView}
for _, view := range views {
if err := view.initView(db, engine); err != nil {
return err
}
}

for _, table := range tables {
if err := table.initTable(db, engine); err != nil {
// check settings table it's not empty
err := checkSettingsTable(db)
if err != nil {
return err
}
}

// check views
views := []*View{logbookView, airportsView}
for _, view := range views {
if err := view.initView(db, engine); err != nil {
// update schema version
err = updateSchemaVersion(db)
if err != nil {
return err
}
}

// check settings table it's not empty
err := checkSettingsTable(db)
return nil
}

func newShema(db *sql.DB) bool {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

query := "SELECT version FROM metadata ORDER BY created_at DESC LIMIT 1"
var version string
err := db.QueryRowContext(ctx, query).Scan(&version)
if err != nil {
return err
if err == sql.ErrNoRows {
fmt.Printf("No rows found in 'metadata'. Initializing version %s...\n", schemaVersion)
return true
}
fmt.Println(err)
return true
}

return nil
if version != schemaVersion {
fmt.Printf("Schema version (%s) mismatch. Initializing version %s...\n", version, schemaVersion)
return true
}

return false
}

func updateSchemaVersion(db *sql.DB) error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

query := "INSERT INTO metadata (version, created_at) VALUES (?,?)"
_, err := db.ExecContext(ctx, query, schemaVersion, time.Now().Format("02.01.2006 15:04:05"))
return err
}

// checkSettingsTable verifies the proper transition to the new settings table
Expand Down

0 comments on commit 7b5fcd4

Please sign in to comment.