aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/train_stop.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-05-12 22:58:22 +0200
committerJulien Dessaux2021-05-12 22:58:22 +0200
commit1475b95491be100927460bc17a97bfc85ab51d3c (patch)
tree657f17d6d3bbcb296f2808a021ee629fc9e0d248 /pkg/database/train_stop.go
parentFixed sessions schema (diff)
downloadtrains-1475b95491be100927460bc17a97bfc85ab51d3c.tar.gz
trains-1475b95491be100927460bc17a97bfc85ab51d3c.tar.bz2
trains-1475b95491be100927460bc17a97bfc85ab51d3c.zip
Added train_stops to schema
Diffstat (limited to '')
-rw-r--r--pkg/database/train_stop.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/pkg/database/train_stop.go b/pkg/database/train_stop.go
new file mode 100644
index 0000000..27fb910
--- /dev/null
+++ b/pkg/database/train_stop.go
@@ -0,0 +1,36 @@
+package database
+
+import "git.adyxax.org/adyxax/trains/pkg/model"
+
+func (env *DBEnv) ReplaceAndImportTrainStops(trainStops []model.TrainStop) error {
+ pre_query := `DELETE FROM train_stops;`
+ query := `
+ INSERT INTO train_stops
+ (id, name)
+ VALUES
+ ($1, $2);`
+ tx, err := env.db.Begin()
+ if err != nil {
+ return newTransactionError("Could not Begin()", err)
+ }
+ _, err = tx.Exec(pre_query)
+ if err != nil {
+ tx.Rollback()
+ return newQueryError("Could not run database query: most likely the schema is corrupted", err)
+ }
+ for i := 0; i < len(trainStops); i++ {
+ _, err = tx.Exec(
+ query,
+ trainStops[i].Id,
+ trainStops[i].Name,
+ )
+ if err != nil {
+ tx.Rollback()
+ return newQueryError("Could not run database query: ", err)
+ }
+ }
+ if err := tx.Commit(); err != nil {
+ return newTransactionError("Could not commit transaction", err)
+ }
+ return nil
+}