Add format column to post tables
This commit is contained in:
parent
4878495914
commit
e7b5b55f67
@ -49,22 +49,24 @@ func (s *draftStore) Set(post Post) error {
|
|||||||
|
|
||||||
_, err = s.db.db.Exec(
|
_, err = s.db.db.Exec(
|
||||||
`INSERT INTO post_drafts (
|
`INSERT INTO post_drafts (
|
||||||
id, title, description, tags, series, body
|
id, title, description, tags, series, body, format
|
||||||
)
|
)
|
||||||
VALUES
|
VALUES
|
||||||
(?, ?, ?, ?, ?, ?)
|
(?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT (id) DO UPDATE SET
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
title=excluded.title,
|
title=excluded.title,
|
||||||
description=excluded.description,
|
description=excluded.description,
|
||||||
tags=excluded.tags,
|
tags=excluded.tags,
|
||||||
series=excluded.series,
|
series=excluded.series,
|
||||||
body=excluded.body`,
|
body=excluded.body,
|
||||||
|
format=excluded.format`,
|
||||||
post.ID,
|
post.ID,
|
||||||
post.Title,
|
post.Title,
|
||||||
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
|
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
|
||||||
&sql.NullString{String: string(tagsJSON), Valid: len(post.Tags) > 0},
|
&sql.NullString{String: string(tagsJSON), Valid: len(post.Tags) > 0},
|
||||||
&sql.NullString{String: post.Series, Valid: post.Series != ""},
|
&sql.NullString{String: post.Series, Valid: post.Series != ""},
|
||||||
post.Body,
|
post.Body,
|
||||||
|
post.Format,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -86,7 +88,7 @@ func (s *draftStore) get(
|
|||||||
|
|
||||||
query := `
|
query := `
|
||||||
SELECT
|
SELECT
|
||||||
p.id, p.title, p.description, p.tags, p.series, p.body
|
p.id, p.title, p.description, p.tags, p.series, p.body, p.format
|
||||||
FROM post_drafts p
|
FROM post_drafts p
|
||||||
` + where + `
|
` + where + `
|
||||||
ORDER BY p.id ASC`
|
ORDER BY p.id ASC`
|
||||||
@ -118,7 +120,7 @@ func (s *draftStore) get(
|
|||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&post.ID, &post.Title, &description, &tags, &series,
|
&post.ID, &post.Title, &description, &tags, &series,
|
||||||
&post.Body,
|
&post.Body, &post.Format,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,6 +72,7 @@ func TestDraftStore(t *testing.T) {
|
|||||||
post.Series = "whatever"
|
post.Series = "whatever"
|
||||||
post.Body = "anything"
|
post.Body = "anything"
|
||||||
post.Tags = []string{"bar", "baz"}
|
post.Tags = []string{"bar", "baz"}
|
||||||
|
post.Format = FormatGemtext
|
||||||
|
|
||||||
err = h.store.Set(post)
|
err = h.store.Set(post)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
44
src/post/format.go
Normal file
44
src/post/format.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package post
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// ErrFormatStringMalformed indicates that a string could not be converted to a
|
||||||
|
// Format.
|
||||||
|
var ErrFormatStringMalformed = errors.New("format string malformed")
|
||||||
|
|
||||||
|
// Format describes the format of the body of a Post.
|
||||||
|
type Format string
|
||||||
|
|
||||||
|
// Enumeration of possible formats.
|
||||||
|
const (
|
||||||
|
FormatMarkdown Format = "md"
|
||||||
|
FormatGemtext Format = "gmi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Formats slice of all possible Formats.
|
||||||
|
var Formats = []Format{
|
||||||
|
FormatMarkdown,
|
||||||
|
FormatGemtext,
|
||||||
|
}
|
||||||
|
|
||||||
|
var strsToFormats = func() map[string]Format {
|
||||||
|
|
||||||
|
m := map[string]Format{}
|
||||||
|
|
||||||
|
for _, f := range Formats {
|
||||||
|
m[string(f)] = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
|
}()
|
||||||
|
|
||||||
|
// FormatFromString parses a string into a Format, or returns
|
||||||
|
// ErrFormatStringMalformed.
|
||||||
|
func FormatFromString(str string) (Format, error) {
|
||||||
|
|
||||||
|
if f, ok := strsToFormats[str]; ok {
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", ErrFormatStringMalformed
|
||||||
|
}
|
@ -34,6 +34,7 @@ type Post struct {
|
|||||||
Tags []string // only alphanumeric supported
|
Tags []string // only alphanumeric supported
|
||||||
Series string
|
Series string
|
||||||
Body string
|
Body string
|
||||||
|
Format Format
|
||||||
}
|
}
|
||||||
|
|
||||||
// StoredPost is a Post which has been stored in a Store, and has been given
|
// StoredPost is a Post which has been stored in a Store, and has been given
|
||||||
@ -103,22 +104,24 @@ func (s *store) Set(post Post, now time.Time) (bool, error) {
|
|||||||
|
|
||||||
_, err := tx.Exec(
|
_, err := tx.Exec(
|
||||||
`INSERT INTO posts (
|
`INSERT INTO posts (
|
||||||
id, title, description, series, published_at, body
|
id, title, description, series, published_at, body, format
|
||||||
)
|
)
|
||||||
VALUES
|
VALUES
|
||||||
(?, ?, ?, ?, ?, ?)
|
(?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT (id) DO UPDATE SET
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
title=excluded.title,
|
title=excluded.title,
|
||||||
description=excluded.description,
|
description=excluded.description,
|
||||||
series=excluded.series,
|
series=excluded.series,
|
||||||
last_updated_at=?,
|
last_updated_at=?,
|
||||||
body=excluded.body`,
|
body=excluded.body,
|
||||||
|
format=excluded.format`,
|
||||||
post.ID,
|
post.ID,
|
||||||
post.Title,
|
post.Title,
|
||||||
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
|
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
|
||||||
&sql.NullString{String: post.Series, Valid: post.Series != ""},
|
&sql.NullString{String: post.Series, Valid: post.Series != ""},
|
||||||
nowSQL,
|
nowSQL,
|
||||||
post.Body,
|
post.Body,
|
||||||
|
post.Format,
|
||||||
nowSQL,
|
nowSQL,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -174,8 +177,7 @@ func (s *store) get(
|
|||||||
query := `
|
query := `
|
||||||
SELECT
|
SELECT
|
||||||
p.id, p.title, p.description, p.series, GROUP_CONCAT(pt.tag),
|
p.id, p.title, p.description, p.series, GROUP_CONCAT(pt.tag),
|
||||||
p.published_at, p.last_updated_at,
|
p.published_at, p.last_updated_at, p.body, p.format
|
||||||
p.body
|
|
||||||
FROM posts p
|
FROM posts p
|
||||||
LEFT JOIN post_tags pt ON (p.id = pt.post_id)
|
LEFT JOIN post_tags pt ON (p.id = pt.post_id)
|
||||||
` + where + `
|
` + where + `
|
||||||
@ -208,8 +210,7 @@ func (s *store) get(
|
|||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&post.ID, &post.Title, &description, &series, &tag,
|
&post.ID, &post.Title, &description, &series, &tag,
|
||||||
&publishedAt, &lastUpdatedAt,
|
&publishedAt, &lastUpdatedAt, &post.Body, &post.Format,
|
||||||
&post.Body,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -37,9 +37,10 @@ func TestNewID(t *testing.T) {
|
|||||||
func testPost(i int) Post {
|
func testPost(i int) Post {
|
||||||
istr := strconv.Itoa(i)
|
istr := strconv.Itoa(i)
|
||||||
return Post{
|
return Post{
|
||||||
ID: istr,
|
ID: istr,
|
||||||
Title: istr,
|
Title: istr,
|
||||||
Body: istr,
|
Body: istr,
|
||||||
|
Format: FormatMarkdown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +109,7 @@ func TestStore(t *testing.T) {
|
|||||||
post.Tags = []string{"foo", "bar"}
|
post.Tags = []string{"foo", "bar"}
|
||||||
|
|
||||||
first, err := h.store.Set(post, now)
|
first, err := h.store.Set(post, now)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err, "post:%+v", post)
|
||||||
assert.True(t, first)
|
assert.True(t, first)
|
||||||
|
|
||||||
gotPost, err := h.store.GetByID(post.ID)
|
gotPost, err := h.store.GetByID(post.ID)
|
||||||
@ -130,6 +131,7 @@ func TestStore(t *testing.T) {
|
|||||||
post.Series = "whatever"
|
post.Series = "whatever"
|
||||||
post.Body = "anything"
|
post.Body = "anything"
|
||||||
post.Tags = []string{"bar", "baz"}
|
post.Tags = []string{"bar", "baz"}
|
||||||
|
post.Format = FormatGemtext
|
||||||
|
|
||||||
first, err = h.store.Set(post, newNow)
|
first, err = h.store.Set(post, newNow)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -66,6 +66,13 @@ var migrations = &migrate.MemoryMigrationSource{Migrations: []*migrate.Migration
|
|||||||
`ALTER TABLE posts DROP COLUMN description_old`,
|
`ALTER TABLE posts DROP COLUMN description_old`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "4",
|
||||||
|
Up: []string{
|
||||||
|
`ALTER TABLE post_drafts ADD COLUMN format TEXT DEFAULT 'md'`,
|
||||||
|
`ALTER TABLE posts ADD COLUMN format TEXT DEFAULT 'md'`,
|
||||||
|
},
|
||||||
|
},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
// SQLDB is a sqlite3 database which can be used by storage interfaces within
|
// SQLDB is a sqlite3 database which can be used by storage interfaces within
|
||||||
|
Loading…
Reference in New Issue
Block a user