From ccce73c939951c90d36dadb1697f5a7d05d1123e Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Sun, 14 Jan 2024 15:52:07 +0100 Subject: [PATCH] :technologist: add backup solution for data Co-authored-by: haraldnilsen Signed-off-by: Sindre Kjelsrud --- backend/cmd/db/backups/answer_backup.csv | 0 backend/cmd/db/backups/bugreport_backup.csv | 0 backend/cmd/db/backups/evaluation_backup.csv | 0 backend/cmd/db/insert_bug_report.go | 24 +++++++++++++++ backend/cmd/db/insert_evaluation.go | 24 +++++++++++++++ backend/cmd/db/insert_user_answers.go | 32 ++++++++++++++++++++ 6 files changed, 80 insertions(+) create mode 100644 backend/cmd/db/backups/answer_backup.csv create mode 100644 backend/cmd/db/backups/bugreport_backup.csv create mode 100644 backend/cmd/db/backups/evaluation_backup.csv diff --git a/backend/cmd/db/backups/answer_backup.csv b/backend/cmd/db/backups/answer_backup.csv new file mode 100644 index 0000000..e69de29 diff --git a/backend/cmd/db/backups/bugreport_backup.csv b/backend/cmd/db/backups/bugreport_backup.csv new file mode 100644 index 0000000..e69de29 diff --git a/backend/cmd/db/backups/evaluation_backup.csv b/backend/cmd/db/backups/evaluation_backup.csv new file mode 100644 index 0000000..e69de29 diff --git a/backend/cmd/db/insert_bug_report.go b/backend/cmd/db/insert_bug_report.go index 3c9fdf3..da25bcf 100644 --- a/backend/cmd/db/insert_bug_report.go +++ b/backend/cmd/db/insert_bug_report.go @@ -2,8 +2,10 @@ package db import ( "database/sql" + "encoding/csv" "fmt" "log" + "os" _ "github.com/lib/pq" ) @@ -27,6 +29,28 @@ func InsertBugReport(bugText string) (error) { log.Fatalf("Error connecting to the database: %v\n", err) } + // Write bugText to backup-file + file, err := os.OpenFile("cmd/db/backups/bugreport_backup.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + log.Fatalln("error opening file", err) + } + defer file.Close() + + // Prepare the row to be written + rowSlice := []string{bugText} + + // Create a CSV writer and write the row + csvwriter := csv.NewWriter(file) + if err := csvwriter.Write(rowSlice); err != nil { + log.Fatalln("error writing record to file", err) + } + + // Flush the writer and check for errors + csvwriter.Flush() + if err := csvwriter.Error(); err != nil { + log.Fatal(err) + } + insertStatement := ` INSERT INTO FeilRapport (feiltekst) VALUES ($1) diff --git a/backend/cmd/db/insert_evaluation.go b/backend/cmd/db/insert_evaluation.go index f218bce..f3009e9 100644 --- a/backend/cmd/db/insert_evaluation.go +++ b/backend/cmd/db/insert_evaluation.go @@ -2,8 +2,10 @@ package db import ( "database/sql" + "encoding/csv" "fmt" "log" + "os" _ "github.com/lib/pq" ) @@ -27,6 +29,28 @@ func InsertEvaluation(evaluationText string) (error) { log.Fatalf("Error connecting to the database: %v\n", err) } + // Write evaluationText to backup-file + file, err := os.OpenFile("cmd/db/backups/evaluation_backup.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + log.Fatalln("error opening file", err) + } + defer file.Close() + + // Prepare the row to be written + rowSlice := []string{evaluationText} + + // Create a CSV writer and write the row + csvwriter := csv.NewWriter(file) + if err := csvwriter.Write(rowSlice); err != nil { + log.Fatalln("error writing record to file", err) + } + + // Flush the writer and check for errors + csvwriter.Flush() + if err := csvwriter.Error(); err != nil { + log.Fatal(err) + } + insertStatement := ` INSERT INTO Evaluering (evalueringtekst) VALUES ($1) diff --git a/backend/cmd/db/insert_user_answers.go b/backend/cmd/db/insert_user_answers.go index ba69c93..65505e3 100644 --- a/backend/cmd/db/insert_user_answers.go +++ b/backend/cmd/db/insert_user_answers.go @@ -2,9 +2,12 @@ package db import ( "database/sql" + "encoding/csv" "encoding/json" "fmt" "log" + "os" + "strconv" _ "github.com/lib/pq" ) @@ -16,6 +19,11 @@ type FormAnswer struct { type AllFormAnswers []FormAnswer +type CSVRow struct { + RespondentID string + AllAnswers string +} + func InsertUserAnswers(respondentId int, allAnswers string) (error) { // Connection string @@ -36,6 +44,30 @@ func InsertUserAnswers(respondentId int, allAnswers string) (error) { log.Fatalf("Error connecting to the database: %v\n", err) } + // Write answers to backup-file + file, err := os.OpenFile("cmd/db/backups/answer_backup.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + fmt.Println(err) + } + + var row = CSVRow{strconv.Itoa(respondentId), allAnswers} + + rowSlice := []string{row.RespondentID, row.AllAnswers} + + csvwriter := csv.NewWriter(file) + + if err := csvwriter.Write(rowSlice); err != nil { + log.Fatalln("error writing record to file", err) + } + + csvwriter.Flush() + + // Check for errors from flushing + if err := csvwriter.Error(); err != nil { + log.Fatal(err) + } + + // Convert answer format var convertedFormAnswers = convertAnswerFormat(allAnswers) for _,formanswer := range convertedFormAnswers {