Skip to content

Commit 6a25f1e

Browse files
committed
Improvements and cleanup
1 parent cce7259 commit 6a25f1e

File tree

10 files changed

+114
-437
lines changed

10 files changed

+114
-437
lines changed

.github/workflows/pr.yaml

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
fail-fast: false
99
steps:
1010
- name: Checkout
11-
uses: actions/checkout@v2
11+
uses: actions/checkout@v4
1212
with:
1313
fetch-depth: 2
1414

@@ -23,10 +23,10 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
php-versions: [ '8.2' ]
26+
php-versions: [ '8.1', '8.2', '8.3' ]
2727
dependency-version: [ prefer-stable ]
2828
steps:
29-
- uses: actions/checkout@v2
29+
- uses: actions/checkout@v4
3030
- name: Setup PHP, with composer and extensions
3131
uses: shivammathur/setup-php@v2
3232
with:
@@ -55,10 +55,10 @@ jobs:
5555
runs-on: ubuntu-latest
5656
strategy:
5757
matrix:
58-
php-versions: [ '8.2' ]
58+
php-versions: [ '8.1', '8.2', '8.3' ]
5959
dependency-version: [ prefer-stable ]
6060
steps:
61-
- uses: actions/checkout@v2
61+
- uses: actions/checkout@v4
6262
- name: Setup PHP, with composer and extensions
6363
uses: shivammathur/setup-php@v2
6464
with:
@@ -83,40 +83,26 @@ jobs:
8383
run: |
8484
composer coding-standards-check
8585
86-
markdownlint:
86+
markdown-coding-standards:
87+
name: Markdown coding standards
8788
runs-on: ubuntu-latest
88-
name: markdownlint
8989
steps:
9090
- name: Checkout
91-
uses: actions/checkout@v2
92-
- name: Get yarn cache directory path
93-
id: yarn-cache-dir-path
94-
run: echo "::set-output name=dir::$(yarn cache dir)"
95-
- name: Cache yarn packages
96-
uses: actions/cache@v2
97-
id: yarn-cache
98-
with:
99-
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
100-
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
101-
restore-keys: |
102-
${{ runner.os }}-yarn-
103-
- name: Yarn install
104-
uses: actions/setup-node@v2
105-
with:
106-
node-version: '18'
107-
- run: yarn install
108-
- name: markdownlint
109-
run: yarn coding-standards-check/markdownlint
91+
uses: actions/checkout@v4
92+
93+
- name: Coding standards
94+
run: |
95+
docker run --rm --volume "$PWD":/md peterdavehello/markdownlint markdownlint --ignore vendor --ignore LICENSE.md '**/*.md'
11096
11197
code-analysis:
11298
name: PHP - Code analysis
11399
runs-on: ubuntu-latest
114100
strategy:
115101
matrix:
116-
php-versions: [ '8.2' ]
102+
php-versions: [ '8.1', '8.2', '8.3' ]
117103
dependency-version: [ prefer-stable ]
118104
steps:
119-
- uses: actions/checkout@v2
105+
- uses: actions/checkout@v4
120106
- name: Setup PHP, with composer and extensions
121107
uses: shivammathur/setup-php@v2
122108
with:
@@ -140,4 +126,4 @@ jobs:
140126
composer install --no-interaction --no-progress --no-scripts
141127
- name: code-analysis
142128
run: |
143-
composer code-analysis
129+
composer code-analysis

.markdownlint.jsonc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"default": true,
3+
// https://github.com/DavidAnson/markdownlint/blob/main/doc/md013.md
4+
"line-length": {
5+
"line_length": 120,
6+
"code_blocks": false,
7+
"tables": false
8+
},
9+
// https://github.com/DavidAnson/markdownlint/blob/main/doc/md024.md
10+
"no-duplicate-heading": {
11+
"siblings_only": true
12+
}
13+
}

.markdownlintrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
* [PR-19](https://github.com/itk-dev/serviceplatformen_organisation_api/pull/19)
11+
Multiple databases
12+
1013
## [1.0.3] 2024-02-29
1114

1215
* Restore to default monolog config.

README.md

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## About the project
44

5-
Sets up an API with data from [Serviceplatformen Organisation](https://digitaliseringskataloget.dk/integration/sf1500).
5+
Sets up an API with data from [Serviceplatformen Organisation][sf1500].
66

77
### Built with
88

@@ -119,14 +119,68 @@ for help run
119119
docker compose exec phpfpm bin/console organisation:fetch:data --help
120120
```
121121

122-
**To avoid issues with memory leaks during development add the
123-
`--no-debug` flag to the fetch data command.** You may also want to
122+
__To avoid issues with memory leaks during development add the
123+
`--no-debug` flag to the fetch data command.__ You may also want to
124124
add the verbose flag to see progress.
125125

126126
```sh
127127
docker compose exec phpfpm bin/console --no-debug organisation:fetch:data -vvv
128128
```
129129

130+
## Multi-database setup
131+
132+
Fetching all data from [SF1500] can be unstable, so for production we use a multi-database setup; one database is
133+
*current* and the other is used to fetch data. If all data is successfully fetched into ‘the other“ database, it will
134+
become ‘the current“ database.
135+
136+
Data is fetched by running `bin/fetch-data`, but first a little configuration must be made:
137+
138+
```sh
139+
# Copy the example config
140+
cp bin/fetch-data.config.example bin/fetch-data.config
141+
```
142+
143+
Edit `bin/fetch-data.config` to match your; the most important (and only required) config variable is `database_urls`.
144+
145+
When the configuration is in place, run
146+
147+
``` sh
148+
bin/fetch-data
149+
```
150+
151+
to fetch all data into the first database, i.e. the first entry in `database_urls`. If the command succeeds, the first
152+
database will become the current database. If you run the command again, data will be fetched into the second database,
153+
and if the fetching succeeds, the second database will become the current database.
154+
155+
For testing that the switching of databases actually work, set
156+
157+
```sh
158+
# bin/fetch-data.config
159+
max=10
160+
```
161+
162+
in `bin/fetch-data.config` to fetch at most 10 items of each type.
163+
164+
To test that switching databases does not accur in case of an error during fetching, you can define a bogus database URL
165+
before running `bin/fetch-data`:
166+
167+
```sh
168+
# bin/fetch-data.config
169+
database_urls=(
170+
'mysql://db:[email protected]:3306/db'
171+
)
172+
```
173+
174+
If nedd be, you can define which [compose command](https://docs.docker.com/compose/compose-application-model/#cli) (the
175+
default being `docker compose`) is actually used to run the compose setup:
176+
177+
```sh
178+
# bin/fetch-data.config
179+
compose=my-custom-compose
180+
```
181+
182+
See [`bin/fetch-data.config.example`](bin/fetch-data.config.example) for details on configuration variables.
183+
130184
## API
131185

132186
See
@@ -209,8 +263,8 @@ we decided to adhere to in this project.
209263
* Markdown files (markdownlint standard rules)
210264

211265
```sh
212-
docker compose run --rm node yarn install
213-
docker compose run --rm node yarn coding-standards-check
266+
docker run --rm --volume "$PWD:/md" peterdavehello/markdownlint markdownlint --ignore LICENSE.md --ignore vendor/ '**/*.md' --fix
267+
docker run --rm --volume "$PWD:/md" peterdavehello/markdownlint markdownlint --ignore LICENSE.md --ignore vendor/ '**/*.md'
214268
```
215269

216270
* Shell scripts
@@ -244,3 +298,5 @@ psalm
244298
```sh
245299
docker compose exec phpfpm composer code-analysis
246300
```
301+
302+
[sf1500]: https://digitaliseringskataloget.dk/integration/sf1500

bin/fetch-data

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ source "$config_file"
2020

2121
if [ -z "${database_urls:-}" ]; then
2222
cat <<EOF >&2
23-
Missing
23+
Missing or empty
2424
2525
database_urls=(…)
2626
@@ -31,13 +31,6 @@ EOF
3131
exit 1
3232
fi
3333

34-
# https://www.php.net/manual/en/reserved.constants.php#constant.php-int-max
35-
max=${max:-9223372036854775807}
36-
page_size=${page_size:-1000}
37-
38-
echo max: "$max"
39-
echo page_size: "$page_size"
40-
4134
function compose() {
4235
if [[ -n "${compose_script:-}" ]]; then
4336
"$compose_script" "$@"
@@ -64,12 +57,22 @@ echo current_index: "$current_index"
6457
echo next_database_url: "$next_database_url"
6558
echo next_index: "$next_index"
6659

60+
# https://www.php.net/manual/en/reserved.constants.php#constant.php-int-max
61+
max=${max:-9223372036854775807}
62+
page_size=${page_size:-1000}
63+
64+
echo max: "$max"
65+
echo page_size: "$page_size"
66+
67+
# Fetch data into "next" database
68+
# Cf.
69+
compose exec --env DATABASE_URL="$next_database_url" phpfpm bin/console doctrine:migrations:migrate --no-interaction first
6770
compose exec --env DATABASE_URL="$next_database_url" phpfpm bin/console doctrine:migrations:migrate --no-interaction
6871
compose exec --env DATABASE_URL="$next_database_url" phpfpm bin/console --no-debug organisation:fetch:data --max="$max" --page-size="$page_size" -vvv
6972

70-
echo "DATABASE_URL='$next_database_url'" | tee >| .env.local.database
73+
echo "Data fetched into $next_database_url"
7174

75+
# Switch to next database.
76+
echo "DATABASE_URL='$next_database_url'" | tee >| .env.local.database
7277
# Re-up to read in new database URL.
7378
compose up --detach
74-
75-
echo 'SUCCESS!'

bin/fetch-data.config.example

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Database URLs
2-
# database_urls=(
3-
# )
2+
database_urls=(
3+
# Databases defined in ../docker-compose.override.yml
4+
'mysql://db:db@database-0:3306/db?serverVersion=10.11.2-MariaDB&charset=utf8mb4'
5+
'mysql://db:db@database-1:3306/db?serverVersion=10.11.2-MariaDB&charset=utf8mb4'
6+
)
47

5-
# Optional compose script path
6-
# compose_script=
8+
# Optional path to compose script. The path is relative to the directory of this file.
9+
# compose_script=docker compose
710

11+
# Options for bin/console organisation:fetch:data
812
# max=10
913
# page_size=5

docker-compose.override.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ services:
44
# which may already have a nginx container.
55
container_name: organisation_api
66

7-
node:
8-
image: node:18
9-
volumes:
10-
- .:/app:delegated
11-
working_dir: /app
12-
137
phpfpm:
148
# https://docs.docker.com/compose/compose-file/05-services/#env_file
159
env_file:

package.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)