-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[indexer]: Create script to autogenerate indexer schema #5012
[indexer]: Create script to autogenerate indexer schema #5012
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 4 Skipped Deployments
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm ⛱️
I would like to propose a change here. It would be nice to put the requirements like postgres and diesel into a docker container, so the developer don't need to install those dependencies on their machine just to execute that script, and we have a defined environment for generation of the schema. I would move the script to a subdirectory
Little build script #!/bin/bash
set -x
set -e
if ! command -v git &> /dev/null; then
echo "git not installed" >&2
exit 1
fi
REPO_ROOT=$(git rev-parse --show-toplevel)
echo "Parse the rust toolchain version from 'rust-toolchain.toml'..."
RUST_TOOLCHAIN_VERSION=$(grep -oE 'channel = "[^"]+' ${REPO_ROOT}/rust-toolchain.toml | sed 's/channel = "//')
if [ -z "$RUST_TOOLCHAIN_VERSION" ]; then
echo "Failed to parse the rust toolchain version"
exit 1
fi
docker build --build-arg RUST_TOOLCHAIN_VERSION=${RUST_TOOLCHAIN_VERSION} -t postgres-rust-diesel .
#!/bin/bash
# Copyright (c) Mysten Labs, Inc.
# Modifications Copyright (c) 2024 IOTA Stiftung
# SPDX-License-Identifier: Apache-2.0
#
# Update iota-indexer's generated src/schema.rs based on the schema after
# running all its migrations on a clean database.
set -x
set -e
if ! command -v git &> /dev/null; then
echo "git not installed" >&2
exit 1
fi
REPO=$(git rev-parse --show-toplevel)
# Set up the Docker container with PostgreSQL and Diesel CLI
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgrespw
CONTAINER_NAME=postgres-rust-diesel
docker run --rm -d \
--name ${CONTAINER_NAME} \
-e POSTGRES_USER=${POSTGRES_USER} \
-e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
-e RUST_TOOLCHAIN_VERSION=${RUST_TOOLCHAIN_VERSION} \
-v "${REPO}:/workspace" \
-w /workspace \
${CONTAINER_NAME}
# Wait for Postgres to be ready
RETRIES=0
while ! docker exec ${CONTAINER_NAME} pg_isready -p 5432 --username ${POSTGRES_USER}; do
if [ $RETRIES -gt 30 ]; then
echo "Postgres failed to start" >&2
docker stop ${CONTAINER_NAME}
exit 1
fi
sleep 1
RETRIES=$((RETRIES + 1))
done
# Run migrations and generate the schema.rs file
docker exec ${CONTAINER_NAME} diesel migration run \
--database-url "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432" \
--migration-dir "/workspace/crates/iota-indexer/migrations/pg"
docker exec ${CONTAINER_NAME} diesel print-schema \
--database-url "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432" \
--patch-file "/workspace/crates/iota-indexer/src/schema.patch" \
--except-tables "^objects_version_|_partition_" \
> "${REPO}/crates/iota-indexer/src/schema.rs"
# Cleanup: Stop and remove the container
docker stop ${CONTAINER_NAME}
# Applying the patch may destroy the formatting, fix it
rustfmt +nightly "${REPO}/crates/iota-indexer/src/schema.rs" |
@muXxer I like the idea. Pushed changes with dockerized script version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one! I added a few minor comments that would add some cherry on top of the improvement proposed by @muXxer
656f528
to
a5aa568
Compare
* [indexer]: Create script to autogenerate indexer schema * Convert script to dockerized version Co-authored-by: muXxer <git@muxxer.de> * Update script name in schema.patch * Allow build to be called from anywhere * Ensure the required image is built * Rename indexer-generate-schema to indexer-schema --------- Co-authored-by: muXxer <git@muxxer.de> Co-authored-by: Konstantinos Demartinos <konstantinos.demartinos@iota.org>
* [indexer]: Create script to autogenerate indexer schema * Convert script to dockerized version Co-authored-by: muXxer <git@muxxer.de> * Update script name in schema.patch * Allow build to be called from anywhere * Ensure the required image is built * Rename indexer-generate-schema to indexer-schema --------- Co-authored-by: muXxer <git@muxxer.de> Co-authored-by: Konstantinos Demartinos <konstantinos.demartinos@iota.org>
* [indexer]: Create script to autogenerate indexer schema * Convert script to dockerized version Co-authored-by: muXxer <git@muxxer.de> * Update script name in schema.patch * Allow build to be called from anywhere * Ensure the required image is built * Rename indexer-generate-schema to indexer-schema --------- Co-authored-by: muXxer <git@muxxer.de> Co-authored-by: Konstantinos Demartinos <konstantinos.demartinos@iota.org>
Description of change
Create script to autogenerate indexer schema.
Based on upstream patch MystenLabs/sui#19337
Links to any relevant issues
fixes #5008
Type of change
How the change has been tested
./scripts/generate_indexer_schema.sh
cargo ci-clippy
cargo +nightly ci-fmt
cargo test --profile simulator --package=iota-indexer --features shared_test_runtime
Change checklist
Tick the boxes that are relevant to your changes, and delete any items that are not.