diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f8914..2a6eda4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] + +- Fix: Recreate the database view during startup in case changes didn't propagate after a version change. + ## [2.41.0] - 11.08.2024 - Update: Update go minor version (1.21.13) and related packages (bug and security fixes). diff --git a/README.md b/README.md index 3ddb945..d652f53 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ You also can easily export all flight records into EASA style pdf format, print # Changelog +## [Unreleased] + +- Fix: Recreate the database view during startup in case changes didn't propagate after a version change. + ## [2.41.0] - 11.08.2024 - Update: Update go minor version (1.21.13) and related packages (bug and security fixes). @@ -32,12 +36,6 @@ You also can easily export all flight records into EASA style pdf format, print - Fix: Incorrect night time calculation when flying inside the polar circle -## [2.38.0] - 25.06.2024 - -- Fix: Bug with columns for the extended PDF format, both A4 and A5. -- Update: Update golang to 1.21.11 and golang packages -- Fix: The daterange picker on the main Logbook page didn't recognize the settings for the first day of the week (Monday or Sunday) - The full changelog is [here](https://github.com/vsimakhin/web-logbook/blob/main/CHANGELOG.md) # Usage diff --git a/internal/driver/db_operations.go b/internal/driver/db_operations.go index a6b0196..efeed64 100644 --- a/internal/driver/db_operations.go +++ b/internal/driver/db_operations.go @@ -15,7 +15,7 @@ const ( createTable = iota getColumns = iota alterTable = iota - checkView = iota + dropView = iota createView = iota checkIndex = iota createIndex = iota @@ -30,7 +30,7 @@ var queries = map[string]map[int]string{ createTable: "CREATE TABLE %s (%s %s PRIMARY KEY)", getColumns: "PRAGMA table_info(%s)", alterTable: "ALTER TABLE %s ADD COLUMN %s %s", - checkView: "SELECT * FROM %s WHERE 1=2", + dropView: "DROP VIEW IF EXISTS %s", createView: "CREATE VIEW %s AS %s", checkIndex: "PRAGMA index_list(%s)", createIndex: "CREATE INDEX %s ON %s (%s)", @@ -40,7 +40,7 @@ var queries = map[string]map[int]string{ createTable: "CREATE TABLE %s (%s %s PRIMARY KEY);", getColumns: "SHOW COLUMNS FROM %s", alterTable: "ALTER TABLE %s ADD COLUMN %s %s", - checkView: "SELECT * FROM %s WHERE 1=2", + dropView: "DROP VIEW IF EXISTS %s", createView: "CREATE VIEW %s AS %s", checkIndex: "SHOW INDEX FROM %s", createIndex: "CREATE INDEX %s ON %s (%s)", @@ -225,21 +225,26 @@ func (v *View) initView(db *sql.DB, engine string) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if !v.isExists(ctx, db, engine) { - err := v.createView(ctx, db, engine) - if err != nil { - return err - } + v.dropView(db, engine) + err := v.createView(ctx, db, engine) + if err != nil { + return err } return nil } -// isExists checks if the view exists in the database. -func (v *View) isExists(ctx context.Context, db *sql.DB, engine string) bool { - query := fmt.Sprintf(queries[engine][checkView], v.Name) +// dropView drops the view from the database. +func (v *View) dropView(db *sql.DB, engine string) error { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + query := fmt.Sprintf(queries[engine][dropView], v.Name) _, err := db.ExecContext(ctx, query) + if err != nil { + return err + } - return err == nil + return nil } // createView creates the view in the database. diff --git a/internal/driver/db_structure.go b/internal/driver/db_structure.go index 710da89..f27e595 100644 --- a/internal/driver/db_structure.go +++ b/internal/driver/db_structure.go @@ -103,7 +103,7 @@ var logbookView = NewView("logbook_view", iif(day_landings='',0,day_landings) as day_landings, iif(night_landings='',0,night_landings) as night_landings, night_time, ifr_time, pic_time, co_pilot_time, dual_time, - instructor_time, sim_type, sim_time, pic_name, remarks, update_time + instructor_time, sim_type, sim_time, pic_name, remarks, IFNULL(update_time,0) as update_time FROM logbook; `, MySQL: ` @@ -114,7 +114,7 @@ var logbookView = NewView("logbook_view", IF(day_landings='',0,day_landings) as day_landings, IF(night_landings='',0,night_landings) as night_landings, night_time, ifr_time, pic_time, co_pilot_time, dual_time, - instructor_time, sim_type, sim_time, pic_name, remarks, update_time + instructor_time, sim_type, sim_time, pic_name, remarks, IFNULL(update_time,0) as update_time FROM logbook; `, }, diff --git a/internal/models/licensing.go b/internal/models/licensing.go index e15a162..e225244 100644 --- a/internal/models/licensing.go +++ b/internal/models/licensing.go @@ -14,7 +14,7 @@ func (m *DBModel) GetLicenses() ([]License, error) { var licenses []License query := "SELECT uuid, category, name, number, issued, " + - "valid_from, valid_until, document_name, document, update_time " + + "valid_from, valid_until, document_name, document, IFNULL(update_time,0) as update_time " + "FROM licensing ORDER BY category, name" rows, err := m.DB.QueryContext(ctx, query) @@ -44,7 +44,7 @@ func (m *DBModel) GetLicenseRecordByID(uuid string) (License, error) { var lic License query := "SELECT uuid, category, name, number, issued, " + - "valid_from, valid_until, remarks, document_name, document, update_time " + + "valid_from, valid_until, remarks, document_name, document, IFNULL(update_time,0) as update_time " + "FROM licensing WHERE uuid = ?" row := m.DB.QueryRowContext(ctx, query, uuid)