api-go
|
|-------> internal
| |------> db
| | |-----> postgres.go
| |
| |------> domain
| | |-----> website.go
| |
| |------> dto
| | |-----> websites.go
| |
| |------> handlers
| | |-----> websites.go
| |
| |------> repository
| | |-----> website_repository_pg.go
| | |-----> website_repository.go
| |
| |
| |------> routes
| | |-----> websites.go
| |
| |------> service
| |-----> websites.go
|
|------> migrations
| |------> 20251226095007_create_websites_table.down.sql
| |------> 20251226095007_create_websites_table.up.sql
|
|------> .env
postgres via dockerdocker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres
golang-migratebrew install golang-migrate
# STEP 1
cd apps/api-go
# STEP 2
migrate create -ext sql -dir migrations create_websites_table

20251226095007_create_websites_table.up.sql
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TABLE website (
id UUID NOT NULL DEFAULT gen_random_uuid(),
url TEXT NOT NULL UNIQUE,
time_added TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Website_pkey" PRIMARY KEY ("id")
);
apps/api-go/migrations/20251226095007_create_websites_table.down.sql
DROP TABLE website;
cd apps/api-go
migrate -database "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" -path migrations up
OR
# CLI is unable to read DATABASE_URL variable from .env unless its exported
export DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres_go?sslmode=disable"
# Check if DATABASE_URL is set
echo $DATABASE_URL
cd apps/api-go # (Run from root of project )
migrate -database "$DATABASE_URL" -path migrations up