Merge branch 'aliases'

* aliases:
  fact: fix reverse detection
This commit is contained in:
Chris Sexton 2017-10-25 18:17:10 -04:00
commit df29c89d84
1 changed files with 6 additions and 4 deletions

View File

@ -67,15 +67,17 @@ func findAlias(db *sqlx.DB, fact string) (bool, *factoid) {
} }
func (a *alias) save(db *sqlx.DB) error { func (a *alias) save(db *sqlx.DB) error {
ok, _ := findAlias(db, a.Fact) q := `select * from factoid_alias where fact=?`
if ok { var offender alias
err := db.Get(&offender, q, a.Next)
if err == nil {
return fmt.Errorf("DANGER: an opposite alias already exists") return fmt.Errorf("DANGER: an opposite alias already exists")
} }
_, err := a.resolve(db) _, err = a.resolve(db)
if err != nil { if err != nil {
return fmt.Errorf("there is no fact at that destination") return fmt.Errorf("there is no fact at that destination")
} }
q := `insert or replace into factoid_alias (fact, next) values (?, ?)` q = `insert or replace into factoid_alias (fact, next) values (?, ?)`
_, err = db.Exec(q, a.Fact, a.Next) _, err = db.Exec(q, a.Fact, a.Next)
if err != nil { if err != nil {
return err return err