summaryrefslogtreecommitdiff
path: root/golang/pkg/database
diff options
context:
space:
mode:
authorJulien Dessaux2024-06-01 01:25:16 +0200
committerJulien Dessaux2024-06-01 01:25:16 +0200
commit44477a6dbd1398657c892d649b364439739c473c (patch)
tree8762cebe5988506bc8d5879e984cb599add93c68 /golang/pkg/database
parent[golang] simplified the api design some more (diff)
downloadspacetraders-44477a6dbd1398657c892d649b364439739c473c.tar.gz
spacetraders-44477a6dbd1398657c892d649b364439739c473c.tar.bz2
spacetraders-44477a6dbd1398657c892d649b364439739c473c.zip
[golang] fixed sql migrations
Diffstat (limited to 'golang/pkg/database')
-rw-r--r--golang/pkg/database/migrations.go11
-rw-r--r--golang/pkg/database/sql/000_init.sql6
2 files changed, 12 insertions, 5 deletions
diff --git a/golang/pkg/database/migrations.go b/golang/pkg/database/migrations.go
index 94207a5..81cebaa 100644
--- a/golang/pkg/database/migrations.go
+++ b/golang/pkg/database/migrations.go
@@ -61,15 +61,22 @@ func DBInit(ctx context.Context, url string) (myDB *DB, err error) {
return nil, err
}
+ tx, err := db.Begin()
+ if err != nil {
+ return nil, err
+ }
for version < len(statements) {
- if _, err = db.ExecContext(ctx, statements[version]); err != nil {
+ if _, err = tx.ExecContext(ctx, statements[version]); err != nil {
+ tx.Rollback()
return nil, err
}
version++
}
- if _, err = db.ExecContext(ctx, `DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (?);`, version); err != nil {
+ if _, err = tx.ExecContext(ctx, `DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (?);`, version); err != nil {
+ tx.Rollback()
return nil, err
}
+ tx.Commit()
return &DB{ctx: ctx, db: db}, nil
}
diff --git a/golang/pkg/database/sql/000_init.sql b/golang/pkg/database/sql/000_init.sql
index c06d2d3..bd92821 100644
--- a/golang/pkg/database/sql/000_init.sql
+++ b/golang/pkg/database/sql/000_init.sql
@@ -1,7 +1,7 @@
CREATE TABLE schema_version (
- version INTEGER NOT NULL
+ version INTEGER NOT NULL
);
CREATE TABLE tokens (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
);