LoginSignup
8
4

More than 5 years have passed since last update.

Laravel で DynamoDB の migration をやる方法の案

Last updated at Posted at 2018-01-25

前提

  • DynamoDB のテーブル管理のために特別なオペレーションを増やしたくない
  • baopham/dynamodb を利用している
  • テスト実行時は DynamoDB を使わない

やったこと

いつもの migration の中で DynamoDB のテーブル作成・削除も行うようにする。

<?php

use Illuminate\Database\Migrations\Migration;
use BaoPham\DynamoDb\DynamoDbClientService;

class CrateDynamoDbMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // テスト時には DynamoDB を利用しない
        if (app()->environment('testing')) {
            return;
        }

        $dynamoDbClientService = resolve(DynamoDbClientService::class);
        $client = $dynamoDbClientService->getClient();

        $params = [
            'TableName' => 'Movies',
            'KeySchema' => [
                [
                    'AttributeName' => 'year',
                    'KeyType' => 'HASH',
                ],
                [
                    'AttributeName' => 'title',
                    'KeyType' => 'RANGE',
                ],
            ],
            'AttributeDefinitions' => [
                [
                    'AttributeName' => 'year',
                    'AttributeType' => 'N'
                ],
                [
                    'AttributeName' => 'title',
                    'AttributeType' => 'S'
                ],
            ],
            'ProvisionedThroughput' => [
                'ReadCapacityUnits' => 10,
                'WriteCapacityUnits' => 10,
            ],
        ];

        $client->createTable($params);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // テスト時には DynamoDB を利用しない
        if (app()->environment('testing')) {
            return;
        }

        $dynamoDbClientService = resolve(DynamoDbClientService::class);
        $client = $dynamoDbClientService->getClient();

        $params = [
            'TableName' => 'Movies',
        ];

        $client->deleteTable($params);
    }
}

その他検討したこと

  • nordsoftware/lumen-dynamodb はテーブル作成のための仕組みを提供しているが、migration の機能は提供してなさそう
  • quankim/laravel-dynamodb-migrations というものも存在しているが、ドキュメントが少ないのと、利用実績があまりなさそう(Github の Star, packagist の Installs)なので今回は見送り
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4