Update to use pg/v10

This commit is contained in:
Las Zenow 2020-11-30 19:03:31 +00:00
parent 94c75b1d2a
commit 1536d97d8d
13 changed files with 449 additions and 98 deletions

View file

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v10"
)
// Book metadata
@ -23,11 +23,10 @@ type Book struct {
Isbn string `pg:"type:varchar(13)"`
FileSize int `pg:"type:integer"`
FileHash []byte
Cover bool `pg:",notnull"`
Active bool `pg:",notnull"`
Cover bool `pg:",use_zero"`
Active bool `pg:",use_zero"`
UploadDate time.Time `pg:"type:timestamp"`
Tsv string `pg:"type:tsvector"`
Visit *Visit
}
// AddBook to the database
@ -37,7 +36,8 @@ func (db *pgDB) AddBook(book Book) error {
book.UploadDate = time.Now()
}
return db.sql.Insert(&book)
_, err := db.sql.Model(&book).Insert()
return err
}
// GetBooks matching query

View file

@ -3,8 +3,8 @@ package database
import (
"time"
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v9/orm"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type DB interface {
@ -108,6 +108,7 @@ func (db pgDB) Close() error {
}
func (db pgDB) create() error {
orm.RegisterTable(&BookListEntry{})
models := []interface{}{&Book{}, &New{}, &User{}, &Visit{}, &Submission{}, &BookList{}, &BookListEntry{}}
for _, model := range models {
options := &orm.CreateTableOptions{
@ -119,7 +120,7 @@ func (db pgDB) create() error {
// XXX: ON DELETE SET NULL contraint is not supported
options.FKConstraints = false
}
err := db.sql.CreateTable(model, options)
err := db.sql.Model(model).CreateTable(options)
if err != nil {
return err
}

View file

@ -7,7 +7,7 @@ import (
func testDbInit(t *testing.T) (DB, func()) {
db, err := _init(Options{
Name: "test_trantor",
Addr: "/var/run/postgresql/.s.PGSQL.5433",
Addr: "/var/run/postgresql/.s.PGSQL.5432",
// TODO: can it be done with a local user?
User: "trantor",
Password: "trantor",

View file

@ -1,7 +1,7 @@
package database
import (
"github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v10"
)
type BookLister interface {
@ -20,16 +20,16 @@ type BookList struct {
Title string
Description []string `pg:"description,array"`
UserID int `pg:"type:integer"`
User *User
Books []Book `pg:"many2many:book_list_entries"`
User *User `pg:"rel:has-one"`
Books []Book `pg:"many2many:book_list_entries"`
}
type BookListEntry struct {
ID int `pg:"type:serial"`
BookListID int `pg:"type:integer"`
BookList *BookList
BookID string `pg:"type:varchar(16)"`
Book *Book
ID int `pg:"type:serial"`
BookListID int `pg:"type:integer"`
BookList *BookList `pg:"rel:has-one"`
BookID string `pg:"type:varchar(16)"`
Book *Book `pg:"rel:has-one"`
}
func (db *pgDB) NewBookList(listID, title, username string, description []string) error {
@ -37,12 +37,14 @@ func (db *pgDB) NewBookList(listID, title, username string, description []string
if err != nil {
return err
}
return db.sql.Insert(&BookList{
_, err = db.sql.Model(&BookList{
ListID: listID,
Title: title,
Description: description,
UserID: user.ID,
})
}).Insert()
return err
}
func (db *pgDB) AddBookToList(listID, bookID string) error {
@ -51,10 +53,11 @@ func (db *pgDB) AddBookToList(listID, bookID string) error {
return err
}
return db.sql.Insert(&BookListEntry{
_, err = db.sql.Model(&BookListEntry{
BookListID: list.ID,
BookID: bookID,
})
}).Insert()
return err
}
func (db *pgDB) DeleteBookFromList(listID, bookID string) error {

View file

@ -18,10 +18,11 @@ func (db *pgDB) AddNews(text string) error {
}
func (db *pgDB) AddRawNews(text string, date time.Time) error {
return db.sql.Insert(&New{
_, err := db.sql.Model(&New{
Text: text,
Date: date,
})
}).Insert()
return err
}
// GetNews returns all the news for the last 'days' limiting with a maximum of 'num' results

View file

@ -10,18 +10,18 @@ func TestNews(t *testing.T) {
err := db.AddNews(text)
if err != nil {
t.Errorf("db.News(", text, ") return an error: ", err)
t.Errorf("db.News(%s) return an error: %v", text, err)
}
news, err := db.GetNews(1, 1)
if err != nil {
t.Fatalf("db.GetNews() return an error: ", err)
t.Fatalf("db.GetNews() return an error: %v", err)
}
if len(news) < 1 {
t.Fatalf("No news found.")
}
if news[0].Text != text {
t.Errorf("News text don't match : '", news[0].Text, "' <=> '", text, "'")
t.Errorf("News text don't match : '%s' <=> '%s'", news[0].Text, text)
}
}
@ -34,17 +34,17 @@ func TestTwoNews(t *testing.T) {
err := db.AddNews(text)
if err != nil {
t.Errorf("db.News(", text, ") return an error: ", err)
t.Errorf("db.News(%s) return an error: %v", text, err)
}
err = db.AddNews(text2)
if err != nil {
t.Errorf("db.News(", text, ") return an error: ", err)
t.Errorf("db.News(%s) return an error: %v", text, err)
}
news, err := db.GetNews(2, 1)
if err != nil {
t.Fatalf("db.GetNews() return an error: ", err)
t.Fatalf("db.GetNews() return an error: %v", err)
}
if len(news) < 2 {
t.Fatalf("No news found.")

View file

@ -11,7 +11,7 @@ type Visit struct {
Downloads int `pg:"type:integer,notnull"`
Views int `pg:"type:integer,notnull"`
BookID string `pg:"type:varchar(16),unique"`
Book *Book
Book *Book `pg:"rel:has-one"`
}
func (db *pgDB) IncViews(ID string) error {

View file

@ -18,10 +18,10 @@ type Submission struct {
Status string
Comment string
LastModified time.Time
UserID int `pg:"type:integer,unique"`
User *User
UserID int `pg:"type:integer,unique"`
User *User `pg:"rel:has-one"`
BookID string `pg:"type:varchar(16),unique"`
Book *Book
Book *Book `pg:"rel:has-one"`
}
func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, err error) {
@ -32,7 +32,7 @@ func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, e
}
}
submission.LastModified = time.Now()
err = db.sql.Insert(&submission)
_, err = db.sql.Model(&submission).Insert()
return submission.ID, err
}

View file

@ -81,7 +81,8 @@ func (db *pgDB) AddRawUser(name string, hpass []byte, salt []byte, role string)
Salt: salt,
Role: role,
}
return db.sql.Insert(&u)
_, err := db.sql.Model(&u).Insert()
return err
}
func (db *pgDB) GetRole(name string) (string, error) {

View file

@ -54,7 +54,7 @@ func cleanLink(link string) string {
for i := 0; i < len(link); i++ {
if link[i] == '%' {
c, _ := strconv.ParseInt(link[i+1:i+3], 16, 0)
link = link[:i] + string(c) + link[i+3:]
link = link[:i] + string(rune(c)) + link[i+3:]
}
}
return link

View file

@ -36,7 +36,7 @@ func TestInit(t *testing.T) {
t.Fatal("An error ocurred =>", err)
}
if !info.Mode().IsDir() {
t.Errorf(test_path, " is not dir.")
t.Errorf("%s is not dir.", test_path)
}
info, err = os.Stat(test_path + "/a/M")
@ -44,7 +44,7 @@ func TestInit(t *testing.T) {
t.Fatal("An error ocurred =>", err)
}
if !info.Mode().IsDir() {
t.Errorf(test_path, " is not dir.")
t.Errorf("%s is not dir.", test_path)
}
}