Skip to content

Commit 9d1d1e8

Browse files
authored
Merge pull request #205 from bfabio/psql-ci
Run tests with PostgreSQL as well
2 parents 60d2758 + d4f4a12 commit 9d1d1e8

File tree

9 files changed

+108
-112
lines changed

9 files changed

+108
-112
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
on:
2+
push:
3+
branches: [main]
4+
pull_request:
5+
branches: [main]
6+
7+
jobs:
8+
tests-postgresql:
9+
runs-on: ubuntu-latest
10+
11+
services:
12+
db:
13+
image: postgres:14
14+
env:
15+
POSTGRES_PASSWORD: postgres
16+
POSTGRES_DB: test
17+
options: >-
18+
--health-cmd pg_isready
19+
--health-interval 10s
20+
--health-timeout 5s
21+
--health-retries 5
22+
ports:
23+
- 5432:5432
24+
25+
steps:
26+
- uses: actions/checkout@v3
27+
- uses: actions/setup-go@v4
28+
with:
29+
go-version: 1.18.x
30+
- run: go test -race ./...
31+
env:
32+
DATABASE_DSN: "postgres://postgres:postgres@localhost:5432/test?sslmode=disable"

.github/workflows/tests.yml .github/workflows/tests-sqlite.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ on:
55
branches: [main]
66

77
jobs:
8-
tests:
8+
tests-sqlite:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v3
1212
- uses: actions/setup-go@v4
1313
with:
1414
go-version: 1.18.x
1515
- run: go test -race ./...
16+
env:
17+
DATABASE_DSN: "file:/tmp/test.db"

go.mod

+11-11
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ require (
2525
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2626
github.com/gofiber/adaptor/v2 v2.1.25 // indirect
2727
github.com/golang/protobuf v1.5.2 // indirect
28-
github.com/jackc/pgx/v5 v5.3.1 // indirect
29-
github.com/mattn/go-sqlite3 v1.14.17 // indirect
28+
github.com/lib/pq v1.10.9
29+
github.com/mattn/go-colorable v0.1.13 // indirect
30+
github.com/mattn/go-isatty v0.0.18 // indirect
31+
github.com/mattn/go-runewidth v0.0.14 // indirect
32+
github.com/mattn/go-sqlite3 v1.14.17
3033
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
34+
github.com/philhofer/fwd v1.1.2 // indirect
3135
github.com/prometheus/client_golang v1.12.2 // indirect
3236
github.com/prometheus/client_model v0.2.0 // indirect
3337
github.com/prometheus/common v0.32.1 // indirect
3438
github.com/prometheus/procfs v0.7.3 // indirect
39+
github.com/rivo/uniseg v0.2.0 // indirect
40+
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
41+
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
42+
github.com/tinylib/msgp v1.1.8 // indirect
3543
golang.org/x/net v0.9.0 // indirect
3644
google.golang.org/protobuf v1.26.0 // indirect
3745
)
@@ -53,21 +61,13 @@ require (
5361
github.com/go-playground/universal-translator v0.18.0 // indirect
5462
github.com/jackc/pgpassfile v1.0.0 // indirect
5563
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
64+
github.com/jackc/pgx/v5 v5.3.1 // indirect
5665
github.com/jinzhu/inflection v1.0.0 // indirect
5766
github.com/jinzhu/now v1.1.5 // indirect
5867
github.com/klauspost/compress v1.16.3 // indirect
5968
github.com/leodido/go-urn v1.2.1 // indirect
60-
github.com/lib/pq v1.10.9 // indirect
61-
github.com/mattn/go-colorable v0.1.13 // indirect
62-
github.com/mattn/go-isatty v0.0.18 // indirect
63-
github.com/mattn/go-runewidth v0.0.14 // indirect
64-
github.com/philhofer/fwd v1.1.2 // indirect
6569
github.com/pkg/errors v0.9.1 // indirect
6670
github.com/pmezard/go-difflib v1.0.0 // indirect
67-
github.com/rivo/uniseg v0.2.0 // indirect
68-
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
69-
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
70-
github.com/tinylib/msgp v1.1.8 // indirect
7171
github.com/valyala/bytebufferpool v1.0.0 // indirect
7272
github.com/valyala/fasthttp v1.47.0 // indirect
7373
github.com/valyala/tcplisten v1.0.0 // indirect

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bY
191191
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
192192
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
193193
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
194-
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
195194
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
196195
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
197196
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=

internal/database/database.go

+39-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
11
package database
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

6-
"github.com/italia/developers-italia-api/internal/common"
8+
"github.com/italia/developers-italia-api/internal/models"
9+
10+
"gorm.io/driver/postgres"
11+
"gorm.io/driver/sqlite"
712
"gorm.io/gorm"
13+
"gorm.io/gorm/logger"
814
)
915

10-
type Database interface {
11-
Init(dsn string) (*gorm.DB, error)
12-
}
16+
func NewDatabase(connection string) (*gorm.DB, error) {
17+
var (
18+
database *gorm.DB
19+
err error
20+
)
1321

14-
//nolintlint:ireturn
15-
func NewDatabase(env common.Environment) Database {
16-
if env.IsTest() {
22+
switch {
23+
case strings.HasPrefix(connection, "file:"):
1724
log.Println("using SQLite database")
1825

19-
return &SQLiteDB{
20-
dsn: env.Database,
21-
}
26+
database, err = gorm.Open(sqlite.Open(connection), &gorm.Config{TranslateError: true})
27+
case strings.HasPrefix(connection, "postgres:"):
28+
log.Println("using Postgres database")
29+
30+
database, err = gorm.Open(postgres.Open(connection), &gorm.Config{
31+
TranslateError: true,
32+
PrepareStmt: true,
33+
// Disable logging in production
34+
Logger: logger.Default.LogMode(logger.Silent),
35+
})
2236
}
2337

24-
log.Println("using Postgres database")
38+
if err != nil {
39+
return nil, fmt.Errorf("can't open database: %w", err)
40+
}
2541

26-
return &PostgresDB{
27-
dsn: env.Database,
42+
if err = database.AutoMigrate(
43+
&models.Publisher{},
44+
&models.Event{},
45+
&models.CodeHosting{},
46+
&models.Log{},
47+
&models.Software{},
48+
&models.SoftwareURL{},
49+
&models.Webhook{},
50+
); err != nil {
51+
return nil, fmt.Errorf("can't migrate database: %w", err)
2852
}
53+
54+
return database, nil
2955
}

internal/database/postgres_database.go

-42
This file was deleted.

internal/database/sqlite_database.go

-36
This file was deleted.

main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ func Setup() *fiber.App {
3434
panic(err)
3535
}
3636

37-
db := database.NewDatabase(common.EnvironmentConfig)
38-
39-
gormDB, err := db.Init(common.EnvironmentConfig.Database)
37+
gormDB, err := database.NewDatabase(common.EnvironmentConfig.Database)
4038
if err != nil {
4139
panic(err)
4240
}

main_test.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ import (
1616
"github.com/go-testfixtures/testfixtures/v3"
1717
"github.com/gofiber/fiber/v2"
1818
"github.com/stretchr/testify/assert"
19+
20+
_ "github.com/mattn/go-sqlite3"
21+
_ "github.com/lib/pq"
1922
)
2023

2124
const UUID_REGEXP = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
2225

2326
var (
2427
app *fiber.App
2528
db *sql.DB
29+
dbDriver string
2630
goodToken = "Bearer v2.local.TwwHUQEi8hr2Eo881_Bs5vK9dHOR5BgEU24QRf-U7VmUwI1yOEA6mFT0EsXioMkFT_T-jjrtIJ_Nv8f6hR6ifJXUOuzWEkm9Ijq1mqSjQatD3aDqKMyjjBA"
2731
badToken = "Bearer v2.local.UngfrCDNwGUw4pff2oBNoyxYvOErcbVVqLndl6nzONafUCzktaOeMSmoI7B0h62zoxXXLqTm_Phl"
2832
)
@@ -43,23 +47,36 @@ type TestCase struct {
4347
}
4448

4549
func init() {
46-
_ = os.Remove("./test.db")
50+
// Test on SQLite by default if DATABASE_DSN is not set
51+
if _, exists := os.LookupEnv("DATABASE_DSN"); !exists {
52+
_ = os.Setenv("DATABASE_DSN", "file:./test.db")
53+
_ = os.Remove("./test.db")
54+
}
4755

48-
_ = os.Setenv("DATABASE_DSN", "file:./test.db")
4956
_ = os.Setenv("ENVIRONMENT", "test")
5057

5158
// echo -n 'test-paseto-key-dont-use-in-prod' | base64
5259
_ = os.Setenv("PASETO_KEY", "dGVzdC1wYXNldG8ta2V5LWRvbnQtdXNlLWluLXByb2Q=")
5360

61+
dsn := os.Getenv("DATABASE_DSN")
62+
switch {
63+
case strings.HasPrefix(dsn, "postgres:"):
64+
dbDriver = "postgres"
65+
default:
66+
dbDriver = "sqlite3"
67+
}
68+
5469
var err error
55-
db, err = sql.Open("sqlite3", os.Getenv("DATABASE_DSN"))
70+
db, err = sql.Open(dbDriver, dsn)
5671
if err != nil {
5772
log.Fatal(err)
5873
}
5974

6075
// This is needed, otherwise we get a database-locked error
6176
// TODO: investigate the root cause
62-
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
77+
if dbDriver == "sqlite3" {
78+
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
79+
}
6380

6481
// Setup the app as it is done in the main function
6582
app = Setup()
@@ -74,7 +91,7 @@ func TestMain(m *testing.M) {
7491
func loadFixtures(t *testing.T) {
7592
fixtures, err := testfixtures.New(
7693
testfixtures.Database(db),
77-
testfixtures.Dialect("sqlite"),
94+
testfixtures.Dialect(dbDriver),
7895
testfixtures.Directory("test/testdata/fixtures/"),
7996
)
8097
assert.Nil(t, err)

0 commit comments

Comments
 (0)