Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recreate db view on startup #248

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand Down
29 changes: 17 additions & 12 deletions internal/driver/db_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
createTable = iota
getColumns = iota
alterTable = iota
checkView = iota
dropView = iota
createView = iota
checkIndex = iota
createIndex = iota
Expand All @@ -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)",
Expand All @@ -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)",
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/db_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand All @@ -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;
`,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/models/licensing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
Loading