Skip to content

Commit

Permalink
feat: DB migration (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
AKKatung159 authored Dec 26, 2023
1 parent c74f4cc commit 7668fdf
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 10 deletions.
55 changes: 45 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Postgres
- Redis
- Gin
- Atlas

# Contribution

Expand All @@ -13,18 +14,52 @@
```sh
docker compose up -d
```
Migration is need to still be made manually because migration method is
currently to be determined. Feel free to give any suggestion :)
```sh
docker exec \
-it \
oph66-db \
psql -U postgres -c $'
CREATE TABLE feature_flags(key VARCHAR(50) PRIMARY KEY, enabled BOOLEAN NOT NULL, cache_duration INT NOT NULL, extra_info JSONB NOT NULL);
INSERT INTO feature_flags(key, enabled, cache_duration, extra_info) VALUES (\'livestream\', TRUE, 10, \'{\"url\": \"https://www.youtube.com/watch?v=0tOXxuLcaog\"}\');
'

### Migrating with Atlas
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.
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 `init.sql` file:

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

This command fetches the current schema structure and outputs it to a `init.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 `init.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://./migrations/init.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.

### Using wire
This repository use [wire](https://github.com/google/wire) as dependency
injection tools. To add provider/injector, take a look at `/di/wire.go`. Don't
Expand Down
78 changes: 78 additions & 0 deletions migrations/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- Create "feature_flags" table
CREATE TABLE "feature_flags" (
"key" character varying(50) NOT NULL,
"value" boolean NOT NULL,
"cache_duration" integer NOT NULL,
PRIMARY KEY ("key")
);
-- Create "users" table
CREATE TABLE "users" (
"id" bigserial NOT NULL,
"gender" text NULL,
"first_name" text NULL,
"last_name" text NULL,
"email" text NULL,
"school" text NULL,
"birth_date" text NULL,
"address" text NULL,
"from_abroad" text NULL,
"allergy" text NULL,
"medical_condition" text NULL,
"join_cu_reason" text NULL,
"news_source" text NULL,
"status" text NULL,
"grade" text NULL,
PRIMARY KEY ("id")
);
-- Create "desired_rounds" table
CREATE TABLE "desired_rounds" (
"id" serial NOT NULL,
"round" character varying(128) NOT NULL,
"user_id" bigint NULL,
PRIMARY KEY ("id"),
CONSTRAINT "desired_rounds_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "faculties" table
CREATE TABLE "faculties" (
"code" smallint NOT NULL,
"name_en" character varying(128) NOT NULL,
"name_th" character varying(128) NOT NULL,
PRIMARY KEY ("code")
);
-- Create "events" table
CREATE TABLE "events" (
"id" character varying(128) NOT NULL,
"name_en" character varying(128) NOT NULL,
"name_th" character varying(128) NOT NULL,
"faculty_code" smallint NOT NULL,
"department_en" character varying(128) NOT NULL,
"department_th" character varying(128) NOT NULL,
"require_registration" boolean NOT NULL,
"max_capacity" integer NULL,
"location_en" character varying(128) NOT NULL,
"location_th" character varying(128) NOT NULL,
"description_en" character varying(2048) NULL,
"description_th" character varying(2048) NULL,
PRIMARY KEY ("id"),
CONSTRAINT "events_faculty_code_fkey" FOREIGN KEY ("faculty_code") REFERENCES "faculties" ("code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "interested_faculties" table
CREATE TABLE "interested_faculties" (
"id" bigserial NOT NULL,
"faculty" text NULL,
"department" text NULL,
"section" text NULL,
"user_id" bigint NULL,
PRIMARY KEY ("id"),
CONSTRAINT "fk_users_interested_faculties" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "idx_interested_faculties_user_id" to table: "interested_faculties"
CREATE INDEX "idx_interested_faculties_user_id" ON "interested_faculties" ("user_id");
-- Create "schedules" table
CREATE TABLE "schedules" (
"event_id" character varying(128) NOT NULL,
"starts_at" timestamptz NOT NULL,
"ends_at" timestamptz NOT NULL,
PRIMARY KEY ("event_id", "starts_at", "ends_at"),
CONSTRAINT "schedules_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);

0 comments on commit 7668fdf

Please sign in to comment.