summaryrefslogtreecommitdiff
path: root/srv/src/post/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'srv/src/post/store.go')
-rw-r--r--srv/src/post/store.go69
1 files changed, 8 insertions, 61 deletions
diff --git a/srv/src/post/store.go b/srv/src/post/store.go
index 6bdccc2..5da17a9 100644
--- a/srv/src/post/store.go
+++ b/srv/src/post/store.go
@@ -9,8 +9,6 @@ import (
"time"
_ "github.com/mattn/go-sqlite3" // we need dis
- "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
- migrate "github.com/rubenv/sql-migrate"
)
var (
@@ -69,66 +67,15 @@ type Store interface {
Close() error
}
-var migrations = []*migrate.Migration{
- &migrate.Migration{
- Id: "1",
- Up: []string{
- `CREATE TABLE posts (
- id TEXT NOT NULL PRIMARY KEY,
- title TEXT NOT NULL,
- description TEXT NOT NULL,
- series TEXT,
-
- published_at INTEGER NOT NULL,
- last_updated_at INTEGER,
-
- body TEXT NOT NULL
- )`,
- `CREATE TABLE post_tags (
- post_id TEXT NOT NULL,
- tag TEXT NOT NULL,
- UNIQUE(post_id, tag)
- )`,
- },
- Down: []string{
- "DROP TABLE post_tags",
- "DROP TABLE posts",
- },
- },
-}
-
-// Params are parameters used to initialize a new Store. All fields are required
-// unless otherwise noted.
-type StoreParams struct {
- DataDir cfg.DataDir
-}
-
type store struct {
- params StoreParams
- db *sql.DB
+ db *sql.DB
}
-// NewStore initializes a new Store using a sqlite3 database at the given file
-// path.
-func NewStore(params StoreParams) (Store, error) {
-
- path := path.Join(params.DataDir.Path, "post.sqlite3")
-
- db, err := sql.Open("sqlite3", path)
- if err != nil {
- return nil, fmt.Errorf("opening sqlite file at %q: %w", path, err)
- }
-
- migrations := &migrate.MemoryMigrationSource{Migrations: migrations}
-
- if _, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up); err != nil {
- return nil, fmt.Errorf("running migrations: %w", err)
- }
-
+// NewStore initializes a new Store using an existing SQLDB.
+func NewStore(db *SQLDB) Store {
return &store{
- params: params,
- db: db,
- }, nil
+ db: db.db,
+ }
}
func (s *store) Close() error {
@@ -168,7 +115,7 @@ func (s *store) Set(post Post, now time.Time) error {
nowTS := now.Unix()
- nowSql := sql.NullInt64{Int64: nowTS, Valid: !now.IsZero()}
+ nowSQL := sql.NullInt64{Int64: nowTS, Valid: !now.IsZero()}
_, err := tx.Exec(
`INSERT INTO posts (
@@ -186,9 +133,9 @@ func (s *store) Set(post Post, now time.Time) error {
post.Title,
post.Description,
&sql.NullString{String: post.Series, Valid: post.Series != ""},
- nowSql,
+ nowSQL,
post.Body,
- nowSql,
+ nowSQL,
)
if err != nil {