diff --git a/.github/workflows/scheduled.yml b/.github/workflows/scheduled.yml index efb3e32..b1fbc36 100644 --- a/.github/workflows/scheduled.yml +++ b/.github/workflows/scheduled.yml @@ -42,8 +42,37 @@ jobs: - name: cargo update if: hashFiles('server/Cargo.lock') != '' run: cargo update + - name: Download DynamoDB Local + if: hashFiles('server/Cargo.lock') != '' + run: | + wget https://d1ni2b6xgvw0s0.cloudfront.net/v2.x/dynamodb_local_latest.tar.gz + wget https://d1ni2b6xgvw0s0.cloudfront.net/v2.x/dynamodb_local_latest.tar.gz.sha256 + sha256sum --check dynamodb_local_latest.tar.gz.sha256 + mkdir dynamodb-local + tar xfv dynamodb_local_latest.tar.gz --directory=dynamodb-local + ls -lah dynamodb-local + - name: Install Java SDK + if: hashFiles('server/Cargo.lock') != '' + uses: actions/setup-java@v4 + with: + distribution: 'oracle' + java-version: '21' + - name: Launch DynamoDB Local + if: hashFiles('server/Cargo.lock') != '' + run: | + cd dynamodb-local + java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb & + - name: Create tables and indexes in DynamoDb Local + if: hashFiles('server/Cargo.lock') != '' + run: ./run-migrations.sh http://localhost:8000 + env: + AWS_ACCESS_KEY_ID: carpe + AWS_SECRET_ACCESS_KEY: diem + AWS_DEFAULT_REGION: dead-poets-society-eu-east-1 + # https://twitter.com/jonhoo/status/1571290371124260865 - name: cargo test if: hashFiles('server/Cargo.lock') != '' - run: cargo test --locked --all-features --all-targets + run: cargo test --locked --all-features --all-targets -- --include-ignored env: RUSTFLAGS: -D deprecated + USE_DYNAMODB: local diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e7beccb..4880c9e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -43,6 +43,31 @@ jobs: # enable this ci template to run regardless of whether the lockfile is checked in or not if: hashFiles('server/Cargo.lock') == '' run: cargo generate-lockfile + - name: Download DynamoDB Local + run: | + wget https://d1ni2b6xgvw0s0.cloudfront.net/v2.x/dynamodb_local_latest.tar.gz + wget https://d1ni2b6xgvw0s0.cloudfront.net/v2.x/dynamodb_local_latest.tar.gz.sha256 + sha256sum --check dynamodb_local_latest.tar.gz.sha256 + mkdir dynamodb-local + tar xfv dynamodb_local_latest.tar.gz --directory=dynamodb-local + ls -lah dynamodb-local + - name: Install Java SDK + uses: actions/setup-java@v4 + with: + distribution: 'oracle' + java-version: '21' + - name: Launch DynamoDB Local + run: | + cd dynamodb-local + java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb & + - name: Create tables and indexes in DynamoDb Local + run: ./run-migrations.sh http://localhost:8000 + env: + AWS_ACCESS_KEY_ID: carpe + AWS_SECRET_ACCESS_KEY: diem + AWS_DEFAULT_REGION: dead-poets-society-eu-east-1 # https://twitter.com/jonhoo/status/1571290371124260865 - name: cargo test --locked - run: cargo test --locked --all-features --all-targets + run: cargo test --locked --all-features --all-targets -- --include-ignored + env: + USE_DYNAMODB: local diff --git a/server/.gitignore b/server/.gitignore index d7e95f5..a0bcd69 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,3 +1,5 @@ /target /dynamodb-data +/dynamodb-local +dynamodb_local_latest.tar.gz* Makefile diff --git a/server/run-dynamodb-local.sh b/server/run-dynamodb-local.sh index 23a156c..feeca1d 100755 --- a/server/run-dynamodb-local.sh +++ b/server/run-dynamodb-local.sh @@ -45,34 +45,7 @@ while ! (aws dynamodb list-tables --endpoint-url ${ENDPOINT_URL} >/dev/null); do echo "⏳ Waiting for the database to start accepting connections..." done -echo "🗒️ Creating 'events' table..." -aws dynamodb create-table \ - --table-name events \ - --attribute-definitions AttributeName=id,AttributeType=S \ - --key-schema AttributeName=id,KeyType=HASH \ - --billing-mode PAY_PER_REQUEST \ - --endpoint-url ${ENDPOINT_URL} >/dev/null - -aws dynamodb update-time-to-live \ - --table-name events \ - --time-to-live-specification Enabled=true,AttributeName=expire \ - --endpoint-url ${ENDPOINT_URL} >/dev/null - -echo "🗒️ Creating 'questions' table and 🚄 GSI..." -aws dynamodb create-table \ - --table-name questions \ - --attribute-definitions AttributeName=id,AttributeType=S \ - AttributeName=eid,AttributeType=S \ - AttributeName=votes,AttributeType=N \ - --key-schema AttributeName=id,KeyType=HASH \ - --global-secondary-indexes 'IndexName=top,KeySchema=[{AttributeName=eid,KeyType=HASH},{AttributeName=votes,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[answered,hidden]}' \ - --billing-mode PAY_PER_REQUEST \ - --endpoint-url ${ENDPOINT_URL} >/dev/null - -aws dynamodb update-time-to-live \ - --table-name questions \ - --time-to-live-specification Enabled=true,AttributeName=expire \ - --endpoint-url ${ENDPOINT_URL} >/dev/null +./run-migrations.sh "${ENDPOINT_URL}" echo "✅ Container \"${DYNAMODB_CONTAINER_NAME}\" with DynamoDB Local is ready!" diff --git a/server/run-migrations.sh b/server/run-migrations.sh new file mode 100755 index 0000000..b35f5e4 --- /dev/null +++ b/server/run-migrations.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +if ! [ -x "$(command -v aws)" ]; then + echo '❌ Error: please make sure AWS CLI is installed and is in the PATH' >&2 + exit 1 +fi + +if [ "$1" = "" ]; then + echo " + ❌ Please provide DynamoDB endpoint url. + + Usage: $0 + e.g.: $0 http://localhost:8000 + + Also make sure 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', and 'AWS_DEFAULT_REGION' + are available in the environment or your '~/.aws' configuration files. + " + exit 1 +fi + +ENDPOINT_URL="$1" + +echo "🗒️ Creating 'events' table..." +aws dynamodb create-table \ + --table-name events \ + --attribute-definitions AttributeName=id,AttributeType=S \ + --key-schema AttributeName=id,KeyType=HASH \ + --billing-mode PAY_PER_REQUEST \ + --endpoint-url ${ENDPOINT_URL} >/dev/null + +aws dynamodb update-time-to-live \ + --table-name events \ + --time-to-live-specification Enabled=true,AttributeName=expire \ + --endpoint-url ${ENDPOINT_URL} >/dev/null + +echo "🗒️ Creating 'questions' table and 🚄 GSI..." +aws dynamodb create-table \ + --table-name questions \ + --attribute-definitions AttributeName=id,AttributeType=S \ + AttributeName=eid,AttributeType=S \ + AttributeName=votes,AttributeType=N \ + --key-schema AttributeName=id,KeyType=HASH \ + --global-secondary-indexes 'IndexName=top,KeySchema=[{AttributeName=eid,KeyType=HASH},{AttributeName=votes,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[answered,hidden]}' \ + --billing-mode PAY_PER_REQUEST \ + --endpoint-url ${ENDPOINT_URL} >/dev/null + +aws dynamodb update-time-to-live \ + --table-name questions \ + --time-to-live-specification Enabled=true,AttributeName=expire \ + --endpoint-url ${ENDPOINT_URL} >/dev/null