From a74c7612ecd15bc7b2d60d7af3337de998fdcc7b Mon Sep 17 00:00:00 2001 From: Ravi Chandora Date: Wed, 12 Apr 2023 00:14:47 +0530 Subject: [PATCH] TP-0000 | DB migration (#13) --- Dockerfile => Dockerfile.houston | 0 Dockerfile.migrationDown | 20 +++ Dockerfile.migrationUp | 20 +++ Makefile | 10 +- db/migration/000001_init_schema.down.sql | 23 ++++ db/migration/000001_init_schema.up.sql | 148 +++++++++++++++++++++++ go.mod | 1 + script/create-db-migration-file.sh | 3 + 8 files changed, 224 insertions(+), 1 deletion(-) rename Dockerfile => Dockerfile.houston (100%) create mode 100644 Dockerfile.migrationDown create mode 100644 Dockerfile.migrationUp create mode 100644 db/migration/000001_init_schema.down.sql create mode 100644 db/migration/000001_init_schema.up.sql create mode 100755 script/create-db-migration-file.sh diff --git a/Dockerfile b/Dockerfile.houston similarity index 100% rename from Dockerfile rename to Dockerfile.houston diff --git a/Dockerfile.migrationDown b/Dockerfile.migrationDown new file mode 100644 index 0000000..83b408e --- /dev/null +++ b/Dockerfile.migrationDown @@ -0,0 +1,20 @@ +#ARG GOLANG_TAG=193044292705.dkr.ecr.ap-south-1.amazonaws.com/common/golang:1.19 + +# To run locally, use +ARG GOLANG_TAG=registry.cmd.navi-tech.in/common/golang:1.19 + +FROM ${GOLANG_TAG} as builder + +ARG DSN +ENV POSTGRES_DSN $DSN + +RUN curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add - +RUN echo "deb https://packagecloud.io/golang-migrate/migrate/ubuntu/ bionic main" > /etc/apt/sources.list.d/migrate.list +RUN apt-get update +RUN apt-get install -y migrate + +RUN mkdir -p /build +WORKDIR /build +COPY . /build + +CMD /bin/bash -c "make migration-down POSTGRES_DSN=${POSTGRES_DSN}" diff --git a/Dockerfile.migrationUp b/Dockerfile.migrationUp new file mode 100644 index 0000000..5dd56f0 --- /dev/null +++ b/Dockerfile.migrationUp @@ -0,0 +1,20 @@ +#ARG GOLANG_TAG=193044292705.dkr.ecr.ap-south-1.amazonaws.com/common/golang:1.19 + +# To run locally, use +ARG GOLANG_TAG=registry.cmd.navi-tech.in/common/golang:1.19 + +FROM ${GOLANG_TAG} as builder + +ARG DSN +ENV POSTGRES_DSN $DSN + +RUN curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add - +RUN echo "deb https://packagecloud.io/golang-migrate/migrate/ubuntu/ bionic main" > /etc/apt/sources.list.d/migrate.list +RUN apt-get update +RUN apt-get install -y migrate + +RUN mkdir -p /build +WORKDIR /build +COPY . /build + +CMD /bin/bash -c "make migration-up POSTGRES_DSN=${POSTGRES_DSN}" diff --git a/Makefile b/Makefile index 220123a..2e8bfbd 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,16 @@ build: .PHONY: docker-build docker-build: - docker build -t houston -f Dockerfile . + docker build -t houston -f Dockerfile.houston . .PHONY: docker-run docker-run: docker-build docker run houston + +.PHONY: migration-up +migration-up: + migrate -path db/migration/ -database "${POSTGRES_DSN}?sslmode=disable" -verbose up + +.PHONY: migration-down +migration-down: + migrate -path db/migration/ -database "${POSTGRES_DSN}?sslmode=disable" -verbose down diff --git a/db/migration/000001_init_schema.down.sql b/db/migration/000001_init_schema.down.sql new file mode 100644 index 0000000..5546438 --- /dev/null +++ b/db/migration/000001_init_schema.down.sql @@ -0,0 +1,23 @@ +drop table incident; + +drop table team; + +drop table team_tag; + +drop table houston_user; + +drop table severity; + +drop table tag; + +drop table tag_value; + +drop table incident_status; + +drop table role; + +drop table incident_role; + +drop table incident_channel; + +drop table incident_tag; \ No newline at end of file diff --git a/db/migration/000001_init_schema.up.sql b/db/migration/000001_init_schema.up.sql new file mode 100644 index 0000000..101b089 --- /dev/null +++ b/db/migration/000001_init_schema.up.sql @@ -0,0 +1,148 @@ +CREATE TABLE incident +( + id SERIAL PRIMARY KEY, + title text, + description text, + status integer not null, + severity_id integer not null, + incident_name text, + slack_channel varchar(100), + detection_time timestamp without time zone, + start_time timestamp without time zone, + end_time timestamp without time zone, + team_id int not null, + jira_id varchar(100), + confluence_id varchar(100), + created_by varchar(100), + updated_by varchar(100), + severity_tat timestamp without time zone, + remind_me_at timestamp without time zone, + enable_reminder boolean DEFAULT false, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE team +( + id SERIAL PRIMARY KEY, + name varchar(50) unique not null, + slack_user_ids varchar[] default '{}', + active boolean DEFAULT false, + version bigint default 0, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +create table team_tag +( + id serial primary key, + team_id int not null, + tag_id int not null, + optional boolean default false, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE houston_user +( + id SERIAL PRIMARY KEY, + name varchar(50), + slack_user_id varchar(100), + active boolean DEFAULT true +); + +CREATE TABLE severity +( + id SERIAL PRIMARY KEY, + name varchar(50), + description text, + version bigint default 0, + sla int, + slack_user_ids varchar[] default '{}', + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE tag +( + id SERIAL PRIMARY KEY, + name varchar not null, + label text not null, + place_holder text, + action_id varchar(100) not null, + type varchar(100) not null, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +create table tag_value +( + id serial primary key, + tag_id int not null, + value varchar not null, + create_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE incident_status +( + id SERIAL PRIMARY KEY, + name varchar(50), + description text, + is_terminal_status boolean default false, + version bigint default 0, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +create table role +( + id serial primary key, + name varchar(100) not null, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE incident_role +( + id SERIAL PRIMARY KEY, + incident_id integer not null, + role varchar(100), + assigned_to varchar(100), + assigned_by varchar(100), + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +CREATE TABLE incident_channel +( + id SERIAL PRIMARY KEY, + slack_channel varchar(100), + incident_id int not null, + message_timestamp varchar(100), + version bigint default 0, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); + +create table incident_tag +( + id serial primary key, + incident_id int not null, + tag_id int not null, + tag_value_ids int[] default '{}', + free_text_value text, + created_at timestamp without time zone, + updated_at timestamp without time zone, + deleted_at timestamp without time zone +); \ No newline at end of file diff --git a/go.mod b/go.mod index aaf5384..f3f7568 100644 --- a/go.mod +++ b/go.mod @@ -51,6 +51,7 @@ require ( go.opentelemetry.io/otel v1.13.0 // indirect go.opentelemetry.io/otel/trace v1.13.0 // indirect go.uber.org/atomic v1.10.0 // indirect + go.uber.org/goleak v1.1.12 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/arch v0.2.0 // indirect golang.org/x/crypto v0.6.0 // indirect diff --git a/script/create-db-migration-file.sh b/script/create-db-migration-file.sh new file mode 100755 index 0000000..4603983 --- /dev/null +++ b/script/create-db-migration-file.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +migrate create -ext sql -dir db/migration -seq $1 \ No newline at end of file