Skip to content

Commit

Permalink
fix: add atlas migrate, move logic from handler to service, use dto, …
Browse files Browse the repository at this point in the history
…add logic update
  • Loading branch information
AKKatung159 committed Dec 25, 2023
1 parent 2179f74 commit 1f94c1c
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 197 deletions.
54 changes: 54 additions & 0 deletions Migrate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Migrating with Atlas

### Overview

Atlas provides a streamlined way to manage and migrate database schemas effortlessly. Follow the steps below to set up and migrate your database using Atlas.

### Step-by-Step Guide

#### 1. Installation

Firstly, install Atlas using the provided script:

```bash
curl -sSf https://atlasgo.sh | sh
```

This will ensure you have the necessary tools to inspect and apply schema changes seamlessly.

#### 2. Inspecting the Database and Generating Schema

Whenever you make changes to your database schema, you must update your schema definition. To do this, inspect your current database setup and generate a `schema.sql` file:

```bash
atlas schema inspect \
--url "postgres://postgres:[email protected]:5432/postgres?search_path=public&sslmode=disable" \
--format "{{ sql . }}" > schema.sql
```

This command fetches the current schema structure and outputs it to a `schema.sql` file, ensuring you have an up-to-date representation of your database schema.

#### 3. Applying Schema Changes

Once you've made the necessary updates to the `schema.sql` file, you can apply these changes to your database:

```bash
atlas schema apply \
--url "postgres://postgres:[email protected]:5432/postgres?&sslmode=disable" \
--to "file://schema.sql" \
--dev-url "docker://postgres/15"
```

Here's what each parameter does:

- `--url`: Specifies the connection URL to your target database where changes will be applied.
- `--to`: Indicates the path to the `schema.sql` file containing the schema changes.
- `--dev-url`: Provides a development URL for rolling back changes if necessary, ensuring a safe migration process.

#### 4. Confirm and Apply

After executing the migration command, review the changes to ensure everything aligns with your expectations. If satisfied, proceed with the migration to finalize the schema changes in your database.

---

This improved documentation offers a structured approach, providing clarity on each step and its purpose.
1 change: 1 addition & 0 deletions apperror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ var (
InvalidToken = &AppError{"invalid-token", http.StatusUnauthorized}
InvalidEmail = &AppError{"invalid-email", http.StatusNotFound}
DuplicateEmail = &AppError{"duplicate-email", http.StatusConflict}
Unauthorized = &AppError{"unauthorized", http.StatusUnauthorized}
)
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func main() {
r := gin.Default()

r.GET("/_hc", container.HcHandler.HealthCheck)
r.GET("/featureflag/live", container.FeatureflagHandler.GetLivestreamInfo)
r.GET("/live", container.FeatureflagHandler.GetLivestreamInfo)
r.GET("/events", container.EventHandler.GetAllEvents)
r.GET("/events/:eventId", container.EventHandler.GetEventById)
Expand Down
2 changes: 1 addition & 1 deletion di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/isd-sgcu/oph66-backend/cache"
"github.com/isd-sgcu/oph66-backend/cfgldr"
"github.com/isd-sgcu/oph66-backend/database"
auth "github.com/isd-sgcu/oph66-backend/internal/auth"
event "github.com/isd-sgcu/oph66-backend/internal/event"
featureflag "github.com/isd-sgcu/oph66-backend/internal/feature_flag"
healthcheck "github.com/isd-sgcu/oph66-backend/internal/health_check"
auth "github.com/isd-sgcu/oph66-backend/internal/auth"
"github.com/isd-sgcu/oph66-backend/logger"
"go.uber.org/zap"
)
Expand Down
4 changes: 2 additions & 2 deletions di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,24 @@ require (
github.com/redis/go-redis/v9 v9.3.0
github.com/spf13/viper v1.18.1
go.uber.org/zap v1.26.0
golang.org/x/oauth2 v0.15.0
google.golang.org/api v0.153.0
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
)

require (
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/coreos/go-oidc/v3 v3.9.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/pquerna/cachecontrol v0.2.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)

require (
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
Expand All @@ -39,6 +34,9 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -63,13 +61,16 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 1f94c1c

Please sign in to comment.