Compare commits
No commits in common. "c2ab455d3e653b8d7cab9e2df812a45d6e033116" and "6aa1c2b7b6cfe4757489c182ef508fdfe354cf81" have entirely different histories.
c2ab455d3e
...
6aa1c2b7b6
@ -1,42 +0,0 @@
|
|||||||
package postgres
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExecuteRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) error {
|
|
||||||
if query == "" {
|
|
||||||
return errors.New("query can not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if args == nil {
|
|
||||||
return errors.New("arguments can not be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := db.WithContext(ctx).Exec(query, args...)
|
|
||||||
if result.Error != nil {
|
|
||||||
return result.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func QueryRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) (*sql.Rows, error) {
|
|
||||||
if query == "" {
|
|
||||||
return nil, errors.New("query can not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if args == nil {
|
|
||||||
return nil, errors.New("arguments can not be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := db.WithContext(ctx).Raw(query, args...).Rows()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
@ -1,125 +0,0 @@
|
|||||||
package postgres
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestExecuteRawStatement(t *testing.T) {
|
|
||||||
// Setup trow away container
|
|
||||||
ctx := context.Background()
|
|
||||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
||||||
}
|
|
||||||
defer container.Terminate(ctx)
|
|
||||||
|
|
||||||
// Test
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
db *gorm.DB
|
|
||||||
query string
|
|
||||||
args []any
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *sql.Rows
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test 01: Empty Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
db: gormDB,
|
|
||||||
query: "",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test 02: Nil Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
db: gormDB,
|
|
||||||
query: "aasd",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
err := ExecuteRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryRawStatement(t *testing.T) {
|
|
||||||
// Setup trow away container
|
|
||||||
ctx := context.Background()
|
|
||||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
||||||
}
|
|
||||||
defer container.Terminate(ctx)
|
|
||||||
|
|
||||||
// Test
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
db *gorm.DB
|
|
||||||
query string
|
|
||||||
args []any
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *sql.Rows
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test 01: Empty Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
db: gormDB,
|
|
||||||
query: "",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test 02: Nil Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
db: gormDB,
|
|
||||||
query: "aasd",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got, err := QueryRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,7 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,10 +27,4 @@ type OtterSpace interface {
|
|||||||
|
|
||||||
// TagGroup contains all function that are needed to manage the TagGroup
|
// TagGroup contains all function that are needed to manage the TagGroup
|
||||||
TagGroup
|
TagGroup
|
||||||
|
|
||||||
// ExecuteRawStatement run a custom query.
|
|
||||||
ExecuteRawStatement(ctx context.Context, query string, args ...any) error
|
|
||||||
|
|
||||||
// QueryRawStatement runs a custom query and returns the table
|
|
||||||
QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
log2 "log"
|
log2 "log"
|
||||||
@ -161,6 +160,8 @@ func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDoma
|
|||||||
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
|
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NEW FUNCTIONS
|
||||||
|
|
||||||
func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
|
func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
|
||||||
return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime)
|
return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime)
|
||||||
}
|
}
|
||||||
@ -230,14 +231,6 @@ func (p *postgresqlConnection) CreateTagGroupInBatch(ctx context.Context, tagGro
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresqlConnection) ExecuteRawStatement(ctx context.Context, query string, args ...any) error {
|
|
||||||
return postgres.ExecuteRawStatement(ctx, p.db, query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *postgresqlConnection) QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
|
|
||||||
return postgres.QueryRawStatement(ctx, p.db, query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HELPER
|
// HELPER
|
||||||
|
|
||||||
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
|
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
|
||||||
|
@ -2,7 +2,6 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@ -3672,120 +3671,3 @@ func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) boo
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_postgresqlConnection_ExecuteRawStatement(t *testing.T) {
|
|
||||||
// Setup trow away container
|
|
||||||
ctx := context.Background()
|
|
||||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
||||||
}
|
|
||||||
defer container.Terminate(ctx)
|
|
||||||
|
|
||||||
// Test
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
query string
|
|
||||||
args []any
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *sql.Rows
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test 01: Empty Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
query: "",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test 02: Nil Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
query: "aasd",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
p := &postgresqlConnection{
|
|
||||||
db: gormDB,
|
|
||||||
debug: true,
|
|
||||||
}
|
|
||||||
err := p.ExecuteRawStatement(tt.args.ctx, tt.args.query, tt.args.args...)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_postgresqlConnection_QueryRawStatement(t *testing.T) {
|
|
||||||
// Setup trow away container
|
|
||||||
ctx := context.Background()
|
|
||||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not start PostgreSQL container: %v", err)
|
|
||||||
}
|
|
||||||
defer container.Terminate(ctx)
|
|
||||||
|
|
||||||
// Test
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
query string
|
|
||||||
args []any
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *sql.Rows
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test 01: Empty Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
query: "",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test 02: Nil Query",
|
|
||||||
args: args{
|
|
||||||
ctx: ctx,
|
|
||||||
query: "aasd",
|
|
||||||
args: nil,
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
p := &postgresqlConnection{
|
|
||||||
db: gormDB,
|
|
||||||
debug: true,
|
|
||||||
}
|
|
||||||
got, err := p.QueryRawStatement(tt.args.ctx, tt.args.query, tt.args.args...)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user