🧑‍💻 add backup solution for data

Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
Sindre Kjelsrud 2024-01-14 15:52:07 +01:00
parent 48c17084df
commit ccce73c939
Signed by untrusted user who does not match committer: sidski
GPG key ID: D2BBDF3EDE6BA9A6
6 changed files with 80 additions and 0 deletions

View file

@ -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)