-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
81 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
"embed" | ||
"github.com/pressly/goose/v3" | ||
"log" | ||
"news-api/internal/config" | ||
) | ||
|
||
//go:embed migrations/*.sql | ||
var embedMigrations embed.FS | ||
|
||
func Migrations(cfg *config.Config, db *sql.DB) { | ||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.SetDialect("postgres"); err != nil { | ||
log.Fatalf("Failed to set goose dialect: %v", err) | ||
} | ||
|
||
if err := goose.Up(db, "migrations"); err != nil { | ||
log.Fatalf("Failed to apply migrations: %v", err) | ||
} | ||
|
||
log.Println("Success migrations") | ||
} |
28 changes: 28 additions & 0 deletions
28
internal/migrations/migrations/20241806221532_create_news_tables.sql
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,28 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS News ( | ||
id SERIAL PRIMARY KEY, | ||
title VARCHAR(255) NOT NULL, | ||
content TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS NewsCategories ( | ||
news_id INT REFERENCES News(id), | ||
category_id INT, | ||
PRIMARY KEY (news_id, category_id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS Categories ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
-- +goose StatementEnd | ||
|
||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS NewsCategories; | ||
DROP TABLE IF EXISTS Categories; | ||
DROP TABLE IF EXISTS News; | ||
-- +goose StatementEnd |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.