From fe434208d25ec647a2c860d310987d1460ba3a30 Mon Sep 17 00:00:00 2001 From: Luigi Date: Mon, 22 Jan 2024 19:43:09 +0100 Subject: [PATCH] feat: add migrations --- .github/workflows/build.yaml | 2 ++ Makefile | 5 +++++ db/migration/000001_create_feeds_table.down.sql | 1 + db/migration/000001_create_feeds_table.up.sql | 11 +++++++++++ internal/repository/pg/feed_test.go | 13 +------------ 5 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 db/migration/000001_create_feeds_table.down.sql create mode 100644 db/migration/000001_create_feeds_table.up.sql diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 2342aaa..6702111 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 diff --git a/Makefile b/Makefile index cb0d66d..1c6d574 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/db/migration/000001_create_feeds_table.down.sql b/db/migration/000001_create_feeds_table.down.sql new file mode 100644 index 0000000..2af1d9e --- /dev/null +++ b/db/migration/000001_create_feeds_table.down.sql @@ -0,0 +1 @@ +DROP TABLE feeds; diff --git a/db/migration/000001_create_feeds_table.up.sql b/db/migration/000001_create_feeds_table.up.sql new file mode 100644 index 0000000..2851453 --- /dev/null +++ b/db/migration/000001_create_feeds_table.up.sql @@ -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) +); diff --git a/internal/repository/pg/feed_test.go b/internal/repository/pg/feed_test.go index d625336..f4a0ac7 100644 --- a/internal/repository/pg/feed_test.go +++ b/internal/repository/pg/feed_test.go @@ -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") @@ -26,13 +25,7 @@ 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() } @@ -40,11 +33,9 @@ func setupTestDB(t *testing.T) (*sql.DB, func()) { } 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 { @@ -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 {