summaryrefslogtreecommitdiff
path: root/srv/src/post/sql.go
diff options
context:
space:
mode:
Diffstat (limited to 'srv/src/post/sql.go')
-rw-r--r--srv/src/post/sql.go43
1 files changed, 39 insertions, 4 deletions
diff --git a/srv/src/post/sql.go b/srv/src/post/sql.go
index 16cdc95..c768c9a 100644
--- a/srv/src/post/sql.go
+++ b/srv/src/post/sql.go
@@ -38,10 +38,18 @@ var migrations = &migrate.MemoryMigrationSource{Migrations: []*migrate.Migration
body BLOB NOT NULL
)`,
},
- Down: []string{
- "DROP TABLE assets",
- "DROP TABLE post_tags",
- "DROP TABLE posts",
+ },
+ {
+ Id: "2",
+ Up: []string{
+ `CREATE TABLE post_drafts (
+ id TEXT NOT NULL PRIMARY KEY,
+ title TEXT NOT NULL,
+ description TEXT NOT NULL,
+ tags TEXT,
+ series TEXT,
+ body TEXT NOT NULL
+ )`,
},
},
}}
@@ -89,3 +97,30 @@ func NewInMemSQLDB() *SQLDB {
func (db *SQLDB) Close() error {
return db.db.Close()
}
+
+func (db *SQLDB) withTx(cb func(*sql.Tx) error) error {
+
+ tx, err := db.db.Begin()
+
+ if err != nil {
+ return fmt.Errorf("starting transaction: %w", err)
+ }
+
+ if err := cb(tx); err != nil {
+
+ if rollbackErr := tx.Rollback(); rollbackErr != nil {
+ return fmt.Errorf(
+ "rolling back transaction: %w (original error: %v)",
+ rollbackErr, err,
+ )
+ }
+
+ return fmt.Errorf("performing transaction: %w (rolled back)", err)
+ }
+
+ if err := tx.Commit(); err != nil {
+ return fmt.Errorf("committing transaction: %w", err)
+ }
+
+ return nil
+}