Skip to content

Commit

Permalink
feat: add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
luigibarbato committed Jan 22, 2024
1 parent 423e7ba commit fe43420
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
run: go get -v -t -d ./...
- name: Install dependencies
run: go mod download
- name: Apply database migrations
run: make migrate
- name: Unit test
run: make test-unit
- name: Integration test
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ test-integration:
build:
@go build --tags=release -o ${_PROJECT_DIRECTORY}/bin/unconditional-server

.PHONY: migrate

migrate:
@migrate --path db/migration/ --database "postgresql://${UNCONDITIONAL_API_DATABASE_USER}:${UNCONDITIONAL_API_DATABASE_PASSWORD}@localhost:5432/${UNCONDITIONAL_API_DATABASE_NAME}?sslmode=disable" --verbose up

# Helpers
check-variable-%: # detection of undefined variables.
@[[ "${${*}}" ]] || (echo '*** Please define variable `${*}` ***' && exit 1)
1 change: 1 addition & 0 deletions db/migration/000001_create_feeds_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE feeds;
11 changes: 11 additions & 0 deletions db/migration/000001_create_feeds_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE feeds (
title VARCHAR(255) NOT NULL,
link VARCHAR(255) PRIMARY KEY,
language VARCHAR(10),
image_title VARCHAR(255),
image_url VARCHAR(255),
summary TEXT,
source VARCHAR(255),
date timestamptz,
embedding vector(384)
);
13 changes: 1 addition & 12 deletions internal/repository/pg/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"testing"
"time"

_ "github.com/lib/pq" // Importa il driver PostgreSQL
_ "github.com/lib/pq"

"github.com/unconditionalday/server/internal/app"
"github.com/unconditionalday/server/internal/repository/pg"
)

func setupTestDB(t *testing.T) (*sql.DB, func()) {
// Imposta la connessione al tuo database di test
dbUser := os.Getenv("UNCONDITIONAL_API_DATABASE_USER")
dbName := os.Getenv("UNCONDITIONAL_API_DATABASE_NAME")
dbPassword := os.Getenv("UNCONDITIONAL_API_DATABASE_PASSWORD")
Expand All @@ -26,25 +25,17 @@ func setupTestDB(t *testing.T) (*sql.DB, func()) {
t.Fatal(err)
}

// Esegui migrazioni o inizializza lo schema del database se necessario
// ...

// Funzione per pulire il database dopo i test
cleanup := func() {
// Pulisci il database (elimina dati di test, ecc.)
// ...
db.Close()
}

return db, cleanup
}

func TestSave(t *testing.T) {
// Imposta il database di test
db, cleanup := setupTestDB(t)
defer cleanup()

// Inizializza il repository
f := pg.NewFeedRepository(db)

testCases := []struct {
Expand Down Expand Up @@ -80,11 +71,9 @@ func TestSave(t *testing.T) {
}

func TestFind(t *testing.T) {
// Imposta il database di test
db, cleanup := setupTestDB(t)
defer cleanup()

// Inizializza il repository
f := pg.NewFeedRepository(db)

testCases := []struct {
Expand Down

0 comments on commit fe43420

Please sign in to comment.