Coder Social home page Coder Social logo

lgtm-cat-migration's Introduction

lgtm-cat-migration

LGTMeowで利用するDBのmigrationを行う

Getting Started

インストール

  • AWS CLI

環境変数の設定

環境変数の設定を行います。

direnv 等の利用を推奨します。

設定が必要な環境変数は下記の通りです。

# ローカル環境用
export DB_HOSTNAME=mysql
export DB_USERNAME=DBのユーザー名
export DB_PASSWORD=DBのパスワード
export DB_NAME=DB名

# ECS実行用
export SUBNET_ID=ECSタスクを実行するサブネットグループID
export STG_SECURITY_GROUP_ID=STG環境用 ECSタスクに設定するセキュリテイグループ
export PROD_SECURITY_GROUP_ID=PROD環境用 ECSタスクに設定するセキュリテイグループ
export ACCOUNT_ID=AWSのアカウントID

ローカルでの環境構築

docker-compose up -d でコンテナを起動します。

Migration の追加手順

1. Migration ファイルの追加

Migration をツールは migrate を利用します。

create_new_migration_file.sh を利用すると、空の Migration ファイルを作成出来ますので、そこに SQL を書いていきます。

create_new_migration_file.sh に渡す引数ですが、命名規則は以下のような形になっています。

  • create_[テーブル名]
  • add_column_[追加するカラム名]
  • add_index_[追加するカラム名]

実行例は以下の通りです。

./create_new_migration_file.sh create_users

# これらのファイルが作成される
created ./migrations/20210807151547_create_users.up.sql
created ./migrations/20210807151547_create_users.down.sql

2. Migration の実行を行う

./migrate_up.sh を実行して Migration を実行します。

Migration のロールバックを行う際は ./migrate_down.sh を実行します。

ローカル開発環境の場合、migrate コンテナ上で実行する必要があります。

AWS 上での Migration 実行方法

ECSタスクで migration を実行します。 ローカル環境から下記のスクリプトを実行してください。

下記のリソースは Terraform で管理しています。

  • ECS Cluster
  • ECR
  • IAM
  • SecurityGroup
  • CloudWatch Logs

前提条件

データベースとユーザーが作成されている必要があります。

DBへの接続

踏み台用のコンテナから DB に接続します。

下記のインストールが必要です。

ECS Service lgtm-cat-bastion-service の Desired count を1に変更し、タスクを起動します。

下記のコマンドを実行します。

aws ecs execute-command  \
    --profile lgtm-cat \
    --region ap-northeast-1 \
    --cluster lgtm-cat-bastion-cluster \
    --task <タスクID> \
    --container bastion \
    --command "/bin/sh" \
    --interactive

データベースとユーザーの作成

MySQL へ接続します。

mysql -h ホスト名 -P 3306 -u ユーザー名 -p

下記のコマンドを実行してデータベースとユーザーを作成してください。

CREATE DATABASE your_database;
CREATE USER your_user@'%' IDENTIFIED WITH mysql_native_password BY 'YourPassword';
GRANT ALL ON your_database.* TO 'your_user'@'%';

Docker イメージのビルドと ECR へのプッシュ

Docker の新規作成時、変更時に実行します。

./01_push_ecr_local.sh <stg|prod>

ECS タスクの定義の作成

ECS タスクの定義の新規作成時、変更時に実行します。

./02_create_ecs_task_local.sh <stg|prod>

ECS タスクの実行

migration の際に実行します。

./03_execute_ecs_task_local.sh <stg|prod>

lgtm-cat-migration's People

Contributors

kobayashi-m42 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.