-
Notifications
You must be signed in to change notification settings - Fork 1
/
justfile
339 lines (280 loc) · 12.5 KB
/
justfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
set shell := ["bash", "-c"]
# Allow scripts to read the arguments as shell arguments.
# This allows us to handle quoted arguments.
set positional-arguments
HGE_V3_DIR := env_var_or_default('HGE_V3_DIRECTORY', '../v3-engine')
CONNECTOR_IMAGE_NAME := "ghcr.io/hasura/ndc-postgres"
CONNECTOR_IMAGE_TAG := "dev"
CONNECTOR_IMAGE := CONNECTOR_IMAGE_NAME + ":" + CONNECTOR_IMAGE_TAG
POSTGRESQL_CONNECTION_URI := "postgresql://postgres:password@localhost:64002"
POSTGRESQL_EMPTY_CONNECTION_URI := "postgresql://postgres:password@localhost:64002/empty"
POSTGRES_LATEST_CHINOOK_NDC_METADATA := "static/postgres/v5-configuration"
POSTGRES_BROKEN_QUERIES_NDC_METADATA := "static/postgres/broken-queries-ndc-metadata"
COCKROACH_CONNECTION_URI := "postgresql://postgres:password@localhost:64003/defaultdb"
COCKROACH_LATEST_CHINOOK_NDC_METADATA := "static/cockroach/v5-configuration"
CITUS_CONNECTION_URI := "postgresql://postgres:password@localhost:64004?sslmode=disable"
CITUS_LATEST_CHINOOK_NDC_METADATA := "static/citus/v5-configuration"
YUGABYTE_CONNECTION_URI := "postgresql://yugabyte@localhost:64005"
YUGABYTE_LATEST_CHINOOK_NDC_METADATA := "static/yugabyte/v5-configuration"
# Notes:
# * Building Docker images will not work on macOS.
# You can use `main` instead, by running:
# just --set CONNECTOR_IMAGE_TAG dev-main <targets>
# check everything
check: audit format-check find-unused-dependencies build lint test
# run the connector
run: start-dependencies
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=ndc-postgres \
cargo run --bin ndc-postgres --release -- serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}} > /tmp/ndc-postgres.log
# watch the code, then test and re-run on changes
dev: start-dependencies
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=ndc-postgres \
cargo watch -i "**/snapshots/*" \
-c \
-x 'test -p ndc-postgres-configuration -p query-engine-metadata -p query-engine-sql -p query-engine-translation -p databases-tests --features postgres' \
-x clippy \
-x 'run --bin ndc-postgres -- serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}'
# watch the code, then test and re-run on changes
dev-cockroach: start-dependencies
CONNECTION_URI='{{ COCKROACH_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=cockroach-ndc \
cargo watch -i "**/snapshots/*" \
-c \
-x 'test -p query-engine-translation -p databases-tests --features cockroach' \
-x clippy \
-x 'run --bin ndc-postgres -- serve --otlp-endpoint http://localhost:4317 --configuration {{COCKROACH_LATEST_CHINOOK_NDC_METADATA}}'
# watch the code, then test and re-run on changes
dev-citus: start-dependencies
CONNECTION_URI='{{ CITUS_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=citus-ndc \
cargo watch -i "**/snapshots/*" \
-c \
-x 'test -p query-engine-translation -p databases-tests --features citus' \
-x clippy \
-x 'run --bin ndc-postgres -- serve --otlp-endpoint http://localhost:4317 --configuration {{CITUS_LATEST_CHINOOK_NDC_METADATA}}'
# Generate the OpenAPI Schema document for the configuration.
document-openapi:
RUST_LOG=INFO cargo run --bin openapi-generator
# watch the code, and re-run on changes
watch-run: start-dependencies
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=DEBUG \
cargo watch -i "tests/snapshots/*" \
-c \
-x 'run --bin ndc-postgres -- serve --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}'
# Run ndc-postgres with rust-gdb.
debug: start-dependencies
cargo build
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=DEBUG \
rust-gdb --args target/debug/ndc-postgres serve --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}
# Run the server and produce a flamegraph profile
flamegraph: start-dependencies
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
CARGO_PROFILE_RELEASE_DEBUG=true \
RUST_LOG=DEBUG \
OTEL_SERVICE_NAME=postgres-ndc \
cargo flamegraph --bin ndc-postgres -- \
serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}} > /tmp/ndc-postgres.log
# build everything
build:
cargo build --all-targets --all-features
# build and open docs
doc:
cargo doc --lib --no-deps --open
# run all tests
test *args: start-dependencies
#!/usr/bin/env bash
# choose a test runner
if command -v cargo-nextest > /dev/null; then
TEST_COMMAND=(cargo nextest run --no-fail-fast)
else
TEST_COMMAND=(cargo test --no-fail-fast)
fi
# enable the "yugabyte" feature if running Linux
if [[ "$(uname -m)" == 'x86_64' ]]; then
TEST_COMMAND+=(--features yugabyte)
else
echo "$(tput bold)$(tput setaf 3)WARNING:$(tput sgr0) Skipping the Yugabyte tests because we are running on a non-x86_64 architecture."
fi
# run citus tests
TEST_COMMAND+=(--features citus)
# run cockroach tests
TEST_COMMAND+=(--features cockroach)
# run postgres tests
TEST_COMMAND+=(--features postgres)
TEST_COMMAND+=($@)
echo "$(tput bold)${TEST_COMMAND[*]}$(tput sgr0)"
RUST_LOG=DEBUG "${TEST_COMMAND[@]}"
# Upgrade the the configuration format used in tests to the latest
# supported version. Use this as part of releasing a new configuration format
# version.
#
# Note that this requires the tool `sponge` from the `moreutils` package.
upgrade-test-configurations:
#!/usr/bin/env bash
for f in $(find crates/ -name 'configuration.json')
do
# Run the upgrade
cargo run --bin ndc-postgres-cli upgrade --dir-from $(dirname "$f") --dir-to $(dirname "$f")
# Clean up defaults and schema file, which we don't (yet) care about in tests.
rm $(dirname "$f")/schema.json
cat "$f" | jq 'if has("connectionSettings") then ( del(.connectionSettings) | . ) end | . ' | sponge "$f"
cat "$f" | jq 'if has("introspectionOptions") then ( del(.introspectionOptions) | . ) end | . ' | sponge "$f"
cat "$f" | jq 'if has("$schema") then ( del(."$schema") | . ) end | . ' | sponge "$f"
prettier --write $(dirname "$f")
done
# re-generate the NDC metadata configuration file
generate-configuration: build start-dependencies
# Generate the schema.json by initializing and then removing the configuration.
mkdir ./static/myschema
CONNECTION_URI='{{POSTGRESQL_EMPTY_CONNECTION_URI}}' HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH='./static/myschema' \
cargo run --bin ndc-postgres-cli -- initialize
mv ./static/myschema/schema.json ./static/schema.json
rm -r ./static/myschema
CONNECTION_URI='{{POSTGRESQL_EMPTY_CONNECTION_URI}}' HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH='{{POSTGRES_BROKEN_QUERIES_NDC_METADATA}}' \
cargo run --bin ndc-postgres-cli -- update
./scripts/archive-old-ndc-metadata.sh '{{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}'
CONNECTION_URI='{{POSTGRESQL_CONNECTION_URI}}' cargo run --bin ndc-postgres-cli -- --context='{{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}' update
CONNECTION_URI='{{CITUS_CONNECTION_URI}}' cargo run --bin ndc-postgres-cli -- --context='{{CITUS_LATEST_CHINOOK_NDC_METADATA}}' update
CONNECTION_URI='{{COCKROACH_CONNECTION_URI}}' cargo run --bin ndc-postgres-cli -- --context='{{COCKROACH_LATEST_CHINOOK_NDC_METADATA}}' update
@ if [[ "$(uname -m)" == 'x86_64' ]]; then \
echo "$(tput bold)CONNECTION_URI='{{YUGABYTE_CONNECTION_URI}}' cargo run --bin ndc-postgres-cli -- --context='{{YUGABYTE_LATEST_CHINOOK_NDC_METADATA}}' update$(tput sgr0)"; \
CONNECTION_URI='{{YUGABYTE_CONNECTION_URI}}' cargo run --bin ndc-postgres-cli -- --context='{{YUGABYTE_LATEST_CHINOOK_NDC_METADATA}}' update; \
else \
echo "$(tput bold)$(tput setaf 3)WARNING:$(tput sgr0) Not updating the Yugabyte configuration because we are running on a non-x86_64 architecture."; \
fi
prettier --log-level=warn --write static
# start all the databases and Jaeger
start-dependencies:
#!/usr/bin/env bash
COMMAND=(docker compose up --wait)
COMMAND+=(jaeger)
COMMAND+=(postgres)
COMMAND+=(citus)
COMMAND+=(cockroach)
# only start "yugabyte" if running Linux
if [[ "$(uname -m)" == 'x86_64' ]]; then
COMMAND+=(yugabyte)
fi
echo "$(tput bold)${COMMAND[*]}$(tput sgr0)"
"${COMMAND[@]}"
# run prometheus + grafana
start-metrics:
@echo "http://localhost:3001/ for grafana console"
docker compose up --wait prometheus grafana
# run the v3 engine binary, pointing it at our connector
run-engine: start-dependencies
docker compose up --wait auth-hook
@echo "http://localhost:3000/ for graphiql console"
@echo "http://localhost:4002/ for jaeger console"
# Run graphql-engine using static Chinook metadata
# Set `HGE_V3_DIRECTORY` to the location of your cloned v3 engine
RUST_LOG=DEBUG \
OTLP_ENDPOINT=http://localhost:4317 \
cargo run \
--manifest-path {{ HGE_V3_DIR }}/Cargo.toml \
--bin engine -- \
--metadata-path {{ HGE_V3_DIR }}/crates/engine/tests/schema.json \
--authn-config-path ./static/auth_config.json
# run a standard request to check the service correctly integrates with the engine.
# run `just dev` and `just run-engine` each in a new terminal, then run this command.
test-integrated:
curl -X POST \
-H 'Host: example.hasura.app' \
-H 'Content-Type: application/json' \
-H 'x-hasura-role: admin' \
http://localhost:3000/graphql \
-d '{ "query": "query { Album(limit: 3) { Title } } " }' | jq
# Navigate to the jaeger console
open-jaeger:
open http://localhost:4002/search?service=ndc-postgres
# Navigate to the grafana console
open-grafana: start-metrics
@echo "The login and password are admin:grafana"
open http://localhost:3001
# Navigate to the prometheus console
open-prometheus: start-metrics
open http://localhost:9090
# start a postgres docker image and connect to it using psql
repl-postgres *args:
@docker compose up --wait postgres
psql {{POSTGRESQL_CONNECTION_URI}} $@
# start a cockroach docker image and connect to it using psql
repl-cockroach:
@docker compose up --wait cockroach
psql {{COCKROACH_CONNECTION_URI}}
# start a citus docker image and connect to it using psql
repl-citus:
@docker compose up --wait citus
psql {{CITUS_CONNECTION_URI}}
# start a yugabyte docker image and connect to it using psql
repl-yugabyte:
@docker compose up --wait yugabyte
psql {{YUGABYTE_CONNECTION_URI}}
# run `clippy` linter
lint *FLAGS:
cargo clippy --all-targets --all-features {{FLAGS}}
lint-apply *FLAGS:
cargo clippy --all-targets --all-features --fix {{FLAGS}}
# run `cargo audit`
audit:
cargo audit
# run `cargo audit fix`
audit-fix:
cargo audit fix
# reformat everything
format:
cargo fmt --all
! command -v nixpkgs-fmt > /dev/null || nixpkgs-fmt .
prettier --write .
# is everything formatted?
format-check:
cargo fmt --all --check
! command -v nixpkgs-fmt > /dev/null || nixpkgs-fmt --check .
prettier --check .
# run `cargo machete`
find-unused-dependencies:
# note: you can install cargo-machete with `cargo install cargo-machete`, or use the Nix shell
cargo machete --with-metadata
# check the nix builds work
build-with-nix:
nix build --no-warn-dirty --print-build-logs
# run ndc-postgres-multitenant whilst outputting profile data for massif
massif-postgres: start-dependencies
cargo build --bin ndc-postgres --release
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=ndc-postgres \
valgrind --tool=massif \
target/release/ndc-postgres \
serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}} > /tmp/ndc-postgres.log
# run ndc-postgres-multitenant whilst outputting profile data for heaptrack
heaptrack-postgres: start-dependencies
cargo build --bin ndc-postgres --release
CONNECTION_URI='{{ POSTGRESQL_CONNECTION_URI }}' \
RUST_LOG=INFO \
OTEL_SERVICE_NAME=ndc-postgres \
heaptrack \
target/release/ndc-postgres \
serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}} > /tmp/ndc-postgres.log
# check the docker build works
build-docker-with-nix:
#!/usr/bin/env bash
if [[ '{{CONNECTOR_IMAGE_TAG}}' == 'dev' ]]; then
echo "$(tput bold)nix build .#docker | gunzip | docker load$(tput sgr0)"
gunzip < "$(nix build --no-warn-dirty --no-link --print-out-paths '.#docker')" | docker load
fi
# check the Postgres arm64 docker build works
build-aarch64-docker-with-nix:
#!/usr/bin/env bash
if [[ '{{CONNECTOR_IMAGE_TAG}}' == 'dev' ]]; then
echo "$(tput bold)nix build .#docker-aarch64-linux | gunzip | docker load$(tput sgr0)"
gunzip < "$(nix build --no-warn-dirty --no-link --print-out-paths --system aarch64-linux '.#docker-aarch64-linux')" | docker load
fi