weaker interfaces for migrator

This commit is contained in:
Umputun
2018-03-22 19:35:45 -05:00
parent 4138a5f05a
commit 855de450f1
8 changed files with 38 additions and 26 deletions
+5 -5
View File
@@ -77,10 +77,10 @@ func main() {
if p.Active != nil && p.Command.Find("import") == p.Active {
// import mode
params := migrator.ImportParams{
DataStore: dataStore,
InputFile: opts.ImportCommand.InputFile,
Provider: opts.ImportCommand.Provider,
SiteID: opts.ImportCommand.SiteID,
CommentCreator: dataStore,
InputFile: opts.ImportCommand.InputFile,
Provider: opts.ImportCommand.Provider,
SiteID: opts.ImportCommand.SiteID,
}
if err := migrator.ImportComments(params); err != nil {
log.Fatalf("[ERROR] failed to import, %+v", err)
@@ -102,7 +102,7 @@ func main() {
return sess
}()
exporter := &migrator.Remark{DataStore: dataStore}
exporter := &migrator.Remark{CommentFinder: dataStore}
avatarProxy := &auth.AvatarProxy{
StorePath: opts.ServerCommand.AvatarStore,
+2 -2
View File
@@ -14,7 +14,7 @@ import (
// Disqus implements Importer from disqus xml
type Disqus struct {
DataStore store.Interface
CommentCreator
}
type disqusThread struct {
@@ -56,7 +56,7 @@ func (d *Disqus) Import(r io.Reader, siteID string) (err error) {
commentsCh := d.convert(r, siteID)
failed, passed := 0, 0
for c := range commentsCh {
if _, err = d.DataStore.Create(c); err != nil {
if _, err = d.Create(c); err != nil {
failed++
continue
}
+1 -1
View File
@@ -16,7 +16,7 @@ func TestDisqus_Import(t *testing.T) {
defer os.Remove("/tmp/remark-test.db")
dataStore, err := store.NewBoltDB(store.BoltSite{FileName: "/tmp/remark-test.db", SiteID: "test"})
require.Nil(t, err, "create store")
d := Disqus{DataStore: dataStore}
d := Disqus{CommentCreator: dataStore}
err = d.Import(strings.NewReader(xmlTest), "test")
assert.Nil(t, err)
+14 -3
View File
@@ -23,9 +23,20 @@ type Exporter interface {
Export(w io.Writer, siteID string) error
}
// CommentCreator is a minimal interface used by importer to make comments
type CommentCreator interface {
Create(comment store.Comment) (commentID string, err error)
}
// CommentFinder is a minimal interface used by exporter to find comments and list posts
type CommentFinder interface {
Find(locator store.Locator, sort string) ([]store.Comment, error)
List(siteID string, limit int, skip int) ([]store.PostInfo, error)
}
// ImportParams defines everything needed to run import
type ImportParams struct {
DataStore store.Interface
CommentCreator
InputFile string
Provider string
SiteID string
@@ -38,9 +49,9 @@ func ImportComments(p ImportParams) error {
var importer Importer
switch p.Provider {
case "disqus":
importer = &Disqus{DataStore: p.DataStore}
importer = &Disqus{CommentCreator: p.CommentCreator}
case "native":
importer = &Remark{DataStore: p.DataStore}
importer = &Remark{CommentCreator: p.CommentCreator}
default:
return errors.Errorf("unsupported import provider %s", p.Provider)
}
+8 -8
View File
@@ -24,10 +24,10 @@ func TestMigrator_ImportDisqus(t *testing.T) {
require.Nil(t, err, "create store")
err = ImportComments(ImportParams{
DataStore: dataStore,
InputFile: "/tmp/disqus-test.xml",
SiteID: "test",
Provider: "disqus",
CommentCreator: dataStore,
InputFile: "/tmp/disqus-test.xml",
SiteID: "test",
Provider: "disqus",
})
assert.Nil(t, err)
@@ -52,10 +52,10 @@ func TestMigrator_ImportRemark(t *testing.T) {
require.Nil(t, err, "create store")
err = ImportComments(ImportParams{
DataStore: dataStore,
InputFile: "/tmp/disqus-test.r42",
SiteID: "radio-t",
Provider: "native",
CommentCreator: dataStore,
InputFile: "/tmp/disqus-test.r42",
SiteID: "radio-t",
Provider: "native",
})
assert.Nil(t, err)
+5 -4
View File
@@ -14,12 +14,13 @@ import (
// Remark implements exporter and importer for internal store
type Remark struct {
DataStore store.Interface
CommentCreator
CommentFinder
}
// Export all comments to writer as json strings. Each comment is one string, separated by "\n"
func (r *Remark) Export(w io.Writer, siteID string) error {
topics, err := r.DataStore.List(siteID, 0, 0)
topics, err := r.List(siteID, 0, 0)
if err != nil {
return err
}
@@ -28,7 +29,7 @@ func (r *Remark) Export(w io.Writer, siteID string) error {
commentsCount := 0
for i := len(topics) - 1; i >= 0; i-- { // topics from List sorted in opposite direction
topic := topics[i]
comments, err := r.DataStore.Find(store.Locator{SiteID: siteID, URL: topic.URL}, "time")
comments, err := r.Find(store.Locator{SiteID: siteID, URL: topic.URL}, "time")
if err != nil {
return err
}
@@ -70,7 +71,7 @@ func (r *Remark) Import(reader io.Reader, siteID string) error {
log.Printf("[WARN] unmarshal failed for %s, %s", string(rec), err)
continue
}
if _, err := r.DataStore.Create(comment); err != nil {
if _, err := r.Create(comment); err != nil {
failed++
log.Printf("[WARN] can't write %+v to store, %s", comment, err)
continue
+2 -2
View File
@@ -16,7 +16,7 @@ var testDb = "/tmp/test-remark.db"
func TestRemark_Export(t *testing.T) {
b := prep(t)
r := Remark{DataStore: b}
r := Remark{CommentFinder: b}
buf := &bytes.Buffer{}
err := r.Export(buf, "radio-t")
@@ -41,7 +41,7 @@ func TestRemark_Import(t *testing.T) {
os.Remove(testDb)
b, err := store.NewBoltDB(store.BoltSite{SiteID: "radio-t", FileName: testDb})
assert.Nil(t, err)
r := Remark{DataStore: b}
r := Remark{CommentCreator: b}
err = r.Import(buf, "radio-t")
assert.Nil(t, err)
+1 -1
View File
@@ -420,7 +420,7 @@ func prep(t *testing.T) (srv *Rest, port int) {
AvatarProxy: &auth.AvatarProxy{StorePath: "/tmp", RoutePath: "/api/v1/avatar"},
Admins: []string{"a1", "a2"},
},
Exporter: &migrator.Remark{DataStore: dataStore},
Exporter: &migrator.Remark{CommentFinder: dataStore},
Cache: &mockCache{},
WebRoot: "/tmp",
}