-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from bfabio/psql-ci
Run tests with PostgreSQL as well
- Loading branch information
Showing
9 changed files
with
108 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
tests-postgresql: | ||
runs-on: ubuntu-latest | ||
|
||
services: | ||
db: | ||
image: postgres:14 | ||
env: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: test | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.18.x | ||
- run: go test -race ./... | ||
env: | ||
DATABASE_DSN: "postgres://postgres:postgres@localhost:5432/test?sslmode=disable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/italia/developers-italia-api/internal/common" | ||
"github.com/italia/developers-italia-api/internal/models" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
type Database interface { | ||
Init(dsn string) (*gorm.DB, error) | ||
} | ||
func NewDatabase(connection string) (*gorm.DB, error) { | ||
var ( | ||
database *gorm.DB | ||
err error | ||
) | ||
|
||
//nolintlint:ireturn | ||
func NewDatabase(env common.Environment) Database { | ||
if env.IsTest() { | ||
switch { | ||
case strings.HasPrefix(connection, "file:"): | ||
log.Println("using SQLite database") | ||
|
||
return &SQLiteDB{ | ||
dsn: env.Database, | ||
} | ||
database, err = gorm.Open(sqlite.Open(connection), &gorm.Config{TranslateError: true}) | ||
case strings.HasPrefix(connection, "postgres:"): | ||
log.Println("using Postgres database") | ||
|
||
database, err = gorm.Open(postgres.Open(connection), &gorm.Config{ | ||
TranslateError: true, | ||
PrepareStmt: true, | ||
// Disable logging in production | ||
Logger: logger.Default.LogMode(logger.Silent), | ||
}) | ||
} | ||
|
||
log.Println("using Postgres database") | ||
if err != nil { | ||
return nil, fmt.Errorf("can't open database: %w", err) | ||
} | ||
|
||
return &PostgresDB{ | ||
dsn: env.Database, | ||
if err = database.AutoMigrate( | ||
&models.Publisher{}, | ||
&models.Event{}, | ||
&models.CodeHosting{}, | ||
&models.Log{}, | ||
&models.Software{}, | ||
&models.SoftwareURL{}, | ||
&models.Webhook{}, | ||
); err != nil { | ||
return nil, fmt.Errorf("can't migrate database: %w", err) | ||
} | ||
|
||
return database, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters