2023-08-19 17:33:25 +03:00
|
|
|
//go:build ignore
|
|
|
|
|
2023-08-09 14:31:23 +03:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
2023-08-11 20:12:15 +03:00
|
|
|
"context"
|
2023-08-09 14:31:23 +03:00
|
|
|
"database/sql"
|
|
|
|
"embed"
|
|
|
|
"errors"
|
2023-08-11 20:12:15 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-08-09 14:31:23 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
"github.com/golang-migrate/migrate/v3"
|
2023-08-11 20:12:15 +03:00
|
|
|
mpgx "github.com/golang-migrate/migrate/v4/database/pgx"
|
2023-08-09 14:31:23 +03:00
|
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
2023-08-11 20:12:15 +03:00
|
|
|
"github.com/lib/pq"
|
2024-12-07 02:35:30 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2024-12-18 21:29:01 +03:00
|
|
|
"go.unistack.org/pkgdash/internal/config"
|
|
|
|
"go.unistack.org/pkgdash/internal/models"
|
|
|
|
pb "go.unistack.org/pkgdash/proto"
|
2023-08-09 14:31:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pathMigration = `migrations/postgres`
|
|
|
|
)
|
|
|
|
|
|
|
|
type Postgres struct {
|
|
|
|
db *sql.DB
|
|
|
|
fs embed.FS
|
|
|
|
}
|
|
|
|
|
2023-08-13 00:09:57 +03:00
|
|
|
func NewStorage() func(*sql.DB, embed.FS) interface{} {
|
|
|
|
return func(db *sql.DB, fs embed.FS) interface{} {
|
|
|
|
return &Postgres{db: db, fs: fs}
|
2023-08-09 14:31:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Postgres) MigrateUp() error {
|
2023-08-11 20:12:15 +03:00
|
|
|
driver, err := mpgx.WithInstance(s.db, &mpgx.Config{
|
|
|
|
MigrationsTable: mpgx.DefaultMigrationsTable,
|
2023-08-09 14:31:23 +03:00
|
|
|
DatabaseName: config.ServiceName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
source, err := iofs.New(s.fs, pathMigration)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: pass own logger
|
|
|
|
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Postgres) MigrateDown() error {
|
2023-08-11 20:12:15 +03:00
|
|
|
driver, err := mpgx.WithInstance(s.db, &mpgx.Config{
|
|
|
|
MigrationsTable: mpgx.DefaultMigrationsTable,
|
2023-08-09 14:31:23 +03:00
|
|
|
DatabaseName: config.ServiceName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
source, err := iofs.New(s.fs, pathMigration)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: pass own logger
|
|
|
|
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Postgres) PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
panic("need implement")
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Postgres) PackagesList(ctx context.Context, req *pb.PackagesListReq) (models.ListPackage, error) {
|
2023-08-11 20:12:15 +03:00
|
|
|
rows, err := s.db.QueryContext(ctx, queryListPackage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = rows.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
}()
|
|
|
|
|
|
|
|
result := make([]*models.Package, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
tmp := &models.Package{}
|
|
|
|
if err = rows.Scan(
|
|
|
|
&tmp.ID,
|
|
|
|
&tmp.Name,
|
|
|
|
&tmp.URL,
|
|
|
|
pq.Array(&tmp.Comments),
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Postgres) CommentsCreate(ctx context.Context, req *pb.CommentsCreateReq) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "AddComment: unable to rollback: %v", rollbackErr)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
res, err := tx.ExecContext(ctx, queryAddComment, req.Text, req.PackageId)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aff, affErr := res.RowsAffected(); err != nil {
|
|
|
|
err = affErr
|
|
|
|
} else if aff == 0 {
|
|
|
|
err = errors.New("rows affected is 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:17:42 +03:00
|
|
|
func (s *Postgres) PackagesCreate(ctx context.Context, req *pb.PackagesCreateReq) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "AddPackage: unable to rollback: %v", rollbackErr)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
res, err := tx.ExecContext(ctx, queryAddPackage, req.Name, req.Url, pq.Array(req.Modules))
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aff, affErr := res.RowsAffected(); err != nil {
|
|
|
|
err = affErr
|
|
|
|
} else if aff == 0 {
|
|
|
|
err = errors.New("rows affected is 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Postgres) InsertButchModules(ctx context.Context, req []models.Module) ([]uint64, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "AddPackage: unable to rollback: %v", rollbackErr)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
query := generateQuery(req)
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = rows.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
}()
|
|
|
|
|
|
|
|
result := make([]uint64, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
tmp := uint64(0)
|
|
|
|
if err = rows.Scan(&tmp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateQuery(rsp []models.Module) string {
|
|
|
|
const pattern = `%c('%s', '%s', '%s')`
|
|
|
|
build := strings.Builder{}
|
|
|
|
comma := ' '
|
|
|
|
for i := range rsp {
|
|
|
|
str := fmt.Sprintf(pattern, comma, rsp[i].Name, rsp[i].Version, rsp[i].LastVersion)
|
|
|
|
build.WriteString(str)
|
|
|
|
comma = ','
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(queryInsMsgGetIDs, build.String())
|
|
|
|
}
|