Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql #250

Closed
wants to merge 2 commits into from
Closed

Sql #250

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
🐛Fix bug on bookmark insert for pgsql
Bookmark id auto increment did not permit to remove a bookmak
and then insert a new one with postgresql.

Signed-off-by: Stany MARCEL <[email protected]>
  • Loading branch information
ynsta committed May 19, 2020
commit 9b566382a600d4e9bd83fa71f32f3e021fac2ecc
49 changes: 21 additions & 28 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ func OpenPGDatabase(connString string) (pgDB *PGDatabase, err error) {
CONSTRAINT tag_name_UNIQUE UNIQUE (name))`)

tx.MustExec(`CREATE TABLE IF NOT EXISTS bookmark_tag(
bookmark_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY(bookmark_id, tag_id),
CONSTRAINT bookmark_tag_bookmark_id_FK FOREIGN KEY (bookmark_id) REFERENCES bookmark (id),
CONSTRAINT bookmark_tag_tag_id_FK FOREIGN KEY (tag_id) REFERENCES tag (id))`)
bookmark_id SERIAL REFERENCES bookmark(id) ON DELETE CASCADE,
tag_id SERIAL REFERENCES tag(id) ON DELETE CASCADE,
PRIMARY KEY(bookmark_id, tag_id))`)

// Create indices
tx.MustExec(`CREATE INDEX IF NOT EXISTS bookmark_tag_bookmark_id_FK ON bookmark_tag (bookmark_id)`)
Expand Down Expand Up @@ -110,14 +108,15 @@ func (db *PGDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []model
(url, title, excerpt, author, public, content, html, modified)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT(url) DO UPDATE SET
url = $1,
title = $2,
excerpt = $3,
author = $4,
public = $5,
content = $6,
html = $7,
modified = $8`)
url = $1,
title = $2,
excerpt = $3,
author = $4,
public = $5,
content = $6,
html = $7,
modified = $8
RETURNING id`)
checkError(err)

stmtGetTag, err := tx.Preparex(`SELECT id FROM tag WHERE name = $1`)
Expand Down Expand Up @@ -157,7 +156,7 @@ func (db *PGDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []model
book.Modified = modifiedTime

// Save bookmark
stmtInsertBook.MustExec(
stmtInsertBook.Get(&(book.ID),
book.URL, book.Title, book.Excerpt, book.Author,
book.Public, book.Content, book.HTML, book.Modified)

Expand Down Expand Up @@ -316,6 +315,9 @@ func (db *PGDatabase) GetBookmarks(opts GetBookmarksOptions) ([]model.Bookmark,

// Expand query, because some of the args might be an array
query, args, err := sqlx.Named(query, arg)
if err != nil {
return nil, fmt.Errorf("failed to create named query: %v", err)
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return nil, fmt.Errorf("failed to expand query: %v", err)
Expand Down Expand Up @@ -432,6 +434,9 @@ func (db *PGDatabase) GetBookmarksCount(opts GetBookmarksOptions) (int, error) {

// Expand query, because some of the args might be an array
query, args, err := sqlx.Named(query, arg)
if err != nil {
return 0, fmt.Errorf("failed to create named query: %v", err)
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return 0, fmt.Errorf("failed to expand query: %v", err)
Expand Down Expand Up @@ -468,21 +473,16 @@ func (db *PGDatabase) DeleteBookmarks(ids ...int) (err error) {

// Prepare queries
delBookmark := `DELETE FROM bookmark`
delBookmarkTag := `DELETE FROM bookmark_tag`

// Delete bookmark(s)
if len(ids) == 0 {
tx.MustExec(delBookmarkTag)
tx.MustExec(delBookmark)
} else {
delBookmark += ` WHERE id = $1`
delBookmarkTag += ` WHERE bookmark_id = $1`

stmtDelBookmark, _ := tx.Preparex(delBookmark)
stmtDelBookmarkTag, _ := tx.Preparex(delBookmarkTag)

for _, id := range ids {
stmtDelBookmarkTag.MustExec(id)
stmtDelBookmark.MustExec(id)
}
}
Expand Down Expand Up @@ -626,13 +626,6 @@ func (db *PGDatabase) RenameTag(id int, newName string) error {

// CreateNewID creates new ID for specified table
func (db *PGDatabase) CreateNewID(table string) (int, error) {
var tableID int
query := fmt.Sprintf(`SELECT last_value from %s_id_seq;`, table)

err := db.Get(&tableID, query)
if err != nil && err != sql.ErrNoRows {
return -1, err
}

return tableID, nil
// dummy id as not used on postgresql with autoincrement serial
return 1, nil
}