From 6dc6689787431fd30e45c2de9f6e719de57eb375 Mon Sep 17 00:00:00 2001 From: Vladimir Simakhin Date: Thu, 29 Aug 2024 09:59:14 +0200 Subject: [PATCH 1/3] recreate db view on startup --- CHANGELOG.md | 4 ++++ README.md | 10 ++++------ internal/driver/db_operations.go | 29 +++++++++++++++++------------ 3 files changed, 25 insertions(+), 18 deletions(-) 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. From e1725b00976040f65fd9fd0f194de9956d30c7f9 Mon Sep 17 00:00:00 2001 From: Vladimir Simakhin Date: Thu, 29 Aug 2024 10:09:37 +0200 Subject: [PATCH 2/3] update logbook_view --- internal/driver/db_structure.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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; `, }, From 7316fff5f97eb683c10e1e2502064c169dd6e5fc Mon Sep 17 00:00:00 2001 From: Vladimir Simakhin Date: Thu, 29 Aug 2024 10:23:11 +0200 Subject: [PATCH 3/3] update select queries for licensing table --- internal/models/licensing.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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)