Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete_on_cascade #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ clean-all: clean clean-test
## Example: `postgres-state`.
%-state:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) status
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) status

.PHONY: %-migration
## Create migrations with specifed name and type (`sql` - default, `go`).
Expand All @@ -206,7 +206,7 @@ clean-all: clean clean-test
## `postgres-migration name=create_table type=go`.
%-migration:
$(eval type := $(or $(type), sql))
@goose -dir $(MIGRATIONS_PATH) $* -s create $(name) $(type)
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) create $(name) $(type)

.PHONY: %-migrate
## Apply all available migrations to the database.
Expand All @@ -216,7 +216,7 @@ clean-all: clean clean-test
## Example: `postgres-migrate`.
%-migrate:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) up
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) up

.PHONY: %-migrate-to
## Migrate up to a specific version.
Expand All @@ -226,7 +226,7 @@ clean-all: clean clean-test
## Example: `postgres-migrate-to version=20170506082420`.
%-migrate-to:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) up-to $(version)
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) up-to $(version)

.PHONY: %-rollback
## Roll back a single migration from the current version.
Expand All @@ -236,7 +236,7 @@ clean-all: clean clean-test
## Example: `postgres-rollback`.
%-rollback:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) down
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) down

.PHONY: %-rollback-to
## Roll back migrations to a specific version.
Expand All @@ -246,7 +246,7 @@ clean-all: clean clean-test
## Example: `postgres-rollback-to version=20170506082527`.
%-rollback-to:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) down-to $(version)
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) down-to $(version)

.PHONY: %-reset
## Roll back all migrations.
Expand All @@ -256,4 +256,4 @@ clean-all: clean clean-test
## Example: `postgres-reset`.
%-reset:
$(eval DB := $(shell echo $* | tr '[:lower:]' '[:upper:]'))
@goose -dir $(MIGRATIONS_PATH) $* $($(DB)_CONNECTION) reset
@GOOSE_DRIVER=$* goose -dir $(MIGRATIONS_PATH) $($(DB)_CONNECTION) reset
4 changes: 2 additions & 2 deletions docker/.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
SERVICE_NAME=novamusic
VERSION=v0.4

ENV=prod
ENV=dev

CONFIG_PATH=config/
CONFIG_NAME=prod
CONFIG_NAME=dev
CONFIG_TYPE=yaml

POSTGRES_HOST=postgres
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ CREATE TABLE IF NOT EXISTS "user" (
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS "user";
DROP TYPE IF EXISTS role_type;
-- +goose StatementEnd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS "album" (
artist_id INT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT current_timestamp,
FOREIGN KEY (artist_id) REFERENCES artist (id)
FOREIGN KEY (artist_id) REFERENCES artist (id) ON DELETE CASCADE
);
-- +goose StatementEnd

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ CREATE TABLE IF NOT EXISTS "track" (
release_date TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT current_timestamp,
FOREIGN KEY (artist_id) REFERENCES artist (id),
FOREIGN KEY (album_id) REFERENCES album (id)
FOREIGN KEY (artist_id) REFERENCES artist (id) ON DELETE CASCADE,
FOREIGN KEY (album_id) REFERENCES album (id) ON DELETE CASCADE
);
-- +goose StatementEnd

Expand Down
12 changes: 6 additions & 6 deletions internal/db/postgres/migrations/20240929202312_seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ VALUES
('Houdini', 'Hip-Hop', 123, 'test filepath', 'tracks/Houdini.jpeg', 5, 5);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
TRUNCATE TABLE artist;
TRUNCATE TABLE album;
TRUNCATE TABLE track;
-- +goose StatementEnd
-- -- +goose Down
-- -- +goose StatementBegin
-- TRUNCATE TABLE artist;
-- TRUNCATE TABLE album;
-- TRUNCATE TABLE track;
-- -- +goose StatementEnd