Skip to content

Commit 0e5eff2

Browse files
authored
Use transactions for database dump restore (#112)
* Add development docker database * Use transactions for updating the database * Update instructions * Update action versions * Remove unused authentication method * Remove unused endpoint for authantication (FE handles it) * Remove test for deleted method * Remove app id from iac * Stop adding tags for default vpc and subnet
1 parent dc7004f commit 0e5eff2

File tree

8 files changed

+66
-55
lines changed

8 files changed

+66
-55
lines changed

.github/workflows/backtest.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,27 @@ jobs:
1818
build:
1919
runs-on: ubuntu-latest
2020
steps:
21-
- uses: actions/checkout@v2
22-
- name: Restore web server node dependencies
23-
uses: actions/cache@v2
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
- name: Restoreß dependencies
24+
uses: actions/cache@v3
2425
with:
2526
path: "**/server/test-web-server/node_modules"
26-
key: ${{ runner.os }}-modules-${{ hashFiles('**/server/test-web-server/yarn.lock') }}
27+
key: ${{ runner.os }}-v2-${{ hashFiles('**/server/test-web-server/package-lock.json') }}
2728
- name: Restore gradle dependencies
28-
uses: actions/cache@v2
29+
uses: actions/cache@v3
2930
with:
3031
path: "~/.gradle"
3132
key: ${{ runner.os }}-modules-${{ hashFiles('**/server/build.gradle') }}
3233
- name: Set up JDK 11
33-
uses: actions/setup-java@v2
34+
uses: actions/setup-java@v4
3435
with:
3536
java-version: "11"
3637
distribution: "adopt"
3738
- name: Validate Gradle wrapper
38-
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
39+
uses: gradle/wrapper-validation-action@v1
3940
- name: Run containers
40-
run: docker-compose -f server/docker-compose.yaml up -d
41+
run: docker-compose -f server/docker-compose-test.yaml up -d
4142
- name: Wait for database to start
4243
run: |
4344
for i in `seq 1 60`;

iac/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ provider "aws" {
2828
}
2929
}
3030
}
31+
32+
provider "aws" {
33+
region = var.aws_region
34+
alias = "no_tags"
35+
}

iac/subnet.tf

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
resource "aws_default_subnet" "default_az1" {
22
availability_zone = "${var.aws_region}a"
33

4-
tags = {
5-
(var.type) = var.type_subnet
6-
}
4+
provider = aws.no_tags
75
}
86

97
resource "aws_default_subnet" "default_az2" {
108
availability_zone = "${var.aws_region}b"
119

12-
tags = {
13-
(var.type) = var.type_subnet
14-
}
10+
provider = aws.no_tags
1511
}

iac/vpc.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
resource "aws_default_vpc" "default" {
2+
provider = aws.no_tags
23
}

scripts/get_db_export.sh

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
export_file_zip="wca-developer-database-dump.zip"
99
export_file_sql="wca-developer-database-dump.sql"
10-
temp_database="wca_development_temp"
11-
new_database="wca_development"
12-
13-
mysqlconn="mysql -h ${DB_HOST:-localhost} -u ${DB_USERNAME:-root} -P ${DB_PORT:-3306} --password=${DB_PASSWORD:-}"
10+
database_name="wca_development"
1411

1512
download=true
1613

@@ -50,18 +47,8 @@ fi
5047

5148
echo "Executing the .sql"
5249
echo "This can take a few hours"
53-
$mysqlconn -e "create database if not exists $temp_database; use $temp_database; source $export_file_sql;"
54-
55-
echo "Rename databases"
56-
57-
$mysqlconn -e "create database if not exists $new_database;"
58-
params=$($mysqlconn -N -e "select table_name from information_schema.tables where table_schema='$temp_database'")
59-
60-
for name in $params; do
61-
$mysqlconn -e "drop table if exists $new_database.$name; rename table $temp_database.$name to $new_database.$name;";
62-
echo "Renamed $temp_database.$name to $new_database.$name";
63-
done;
6450

65-
$mysqlconn -e "drop database $temp_database"
51+
mysql -h ${DB_HOST:-localhost} -u ${DB_USERNAME:-root} -P ${DB_PORT:-3306} --password=${DB_PASSWORD:-} \
52+
-e "start transaction; drop database if exists $database_name; create database $database_name; use $database_name; source $export_file_sql; commit;"
6653

6754
echo "Complete"

server/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
## Setup local database
1010

11-
You should use an internal database for handling WCA data.
11+
### Using docker
12+
13+
If you are in the `server` folder, you can just run `docker-compose up -d`. This will spin up an empty MySQL database running on port 3306.
14+
15+
### Your own local copy of WCA's database
16+
17+
You can also use an internal database for handling WCA data.
1218

1319
In case you do not have it installed yet, you will need to get MySQL.
1420

@@ -23,8 +29,6 @@ create database wca_development;
2329

2430
The database `wca_development` will be populated with WCA data. If you want to change password, username or others, make sure to also change on `application-local.properties`.
2531

26-
You should also create the best ever ranks table using the file `server/db/migration/V20220122.1600__best_ever_ranks.sql`.
27-
2832
## Before you run this
2933

3034
You need your copy of the database from WCA. If you already have it (with a user 'root' with no password), you can skip this.
@@ -37,13 +41,19 @@ From the root folder, use
3741

3842
## How to run it
3943

40-
The commands listed here should work in Unix systems or in Windows (using GitBash commandline) with maven set.
41-
4244
- Clone this repository
4345

4446
`git clone https://github.com/thewca/statistics.git`
4547

46-
- Navigate to the server's folder
48+
### With your IDE
49+
50+
- Open the `server` folder in your favorite IDE (not the root folder)
51+
- The project uses gradle and your IDE should recognize that.
52+
- You can hit the run (or debug) button to run it.
53+
54+
### With CLI
55+
56+
Navigate to the server's folder (Unix systems)
4757

4858
`cd statistics/server`
4959

@@ -53,6 +63,8 @@ The commands listed here should work in Unix systems or in Windows (using GitBas
5363

5464
An address should be logged. Probably http://localhost:8080/swagger-ui.html#/, if you did not change port. Visit it to read the documentation. You can run in another port (let's say 8001) by using `./gradlew bootRun --args='--spring.profiles.active=local --server.port=8001'`.
5565

66+
For Windows, I think you need to use `.\gradlew.bat bootRun`.
67+
5668
## Run with docker
5769

5870
- Build an image
@@ -71,22 +83,22 @@ This backend project uses integration tests so we need to actually connect to a
7183

7284
- Preparing the database for tests (from the repository root)
7385

74-
`docker-compose -f server/docker-compose.yaml up -d`
86+
`docker-compose -f server/docker-compose-test.yaml up -d`
7587

7688
This will start the database (port 3307) and also a mocked version of the WCA's api (for getting user info) on port 3500.
7789

7890
- Run the tests
7991

80-
In a new terminal, from the repository root, run
92+
In a new terminal, from the repository root, run
8193

8294
./server/gradlew clean build -p server --info
8395

8496
- If you need to change migrations, run
8597

86-
```
87-
docker-compose -f server/docker-compose.yaml down --volumes
88-
docker-compose -f server/docker-compose.yaml up -d
89-
```
98+
```
99+
docker-compose -f server/docker-compose-test.yaml down --volumes
100+
docker-compose -f server/docker-compose-test.yaml up -d
101+
```
90102

91103
## Code format
92104

@@ -95,4 +107,4 @@ In a new terminal, from the repository root, run
95107
- `File/Settings`
96108
- `Editor/Code style`
97109
- `Scheme actions/Import schemes/IntelliJ IDEA...`
98-
- Import file `server/codequality-config/checkstyle/intellij-java-google-style.xml`
110+
- Import file `server/codequality-config/checkstyle/intellij-java-google-style.xml`

server/docker-compose-test.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3.1"
2+
services:
3+
wca-test-db:
4+
image: "mysql:8.0.26"
5+
environment:
6+
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
7+
MYSQL_DATABASE: wca_development
8+
ports:
9+
- "3307:3306"
10+
volumes:
11+
- "./db/migration:/docker-entrypoint-initdb.d/:ro"
12+
wca-mock-server:
13+
image: "node:16-bullseye-slim"
14+
volumes:
15+
- "./test-web-server/data.js/:/data.js"
16+
- "./test-web-server/package.json/:/package.json"
17+
- "./test-web-server/server.js/:/server.js"
18+
entrypoint: sh -c "npm install;npm start"
19+
network_mode: host

server/docker-compose.yaml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
version: "3.1"
22
services:
3-
wca-test-db:
3+
wca-db:
44
image: "mysql:8.0.26"
55
environment:
66
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
77
MYSQL_DATABASE: wca_development
88
ports:
9-
- "3307:3306"
10-
volumes:
11-
- "./db/migration:/docker-entrypoint-initdb.d/:ro"
12-
wca-mock-server:
13-
image: "node:16-bullseye-slim"
14-
volumes:
15-
- "./test-web-server/data.js/:/data.js"
16-
- "./test-web-server/package.json/:/package.json"
17-
- "./test-web-server/server.js/:/server.js"
18-
entrypoint: sh -c "npm install;npm start"
19-
network_mode: host
9+
- "3306:3306"

0 commit comments

Comments
 (0)