Skip to content
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

Merged
merged 6 commits into from
Jan 29, 2025

Conversation

tomxey
Copy link
Contributor

@tomxey tomxey commented Jan 24, 2025

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

  • Enhancement (a non-breaking change which adds functionality)

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.

  • I have followed the contribution guidelines for this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that new and existing unit tests pass locally with my changes

@tomxey tomxey self-assigned this Jan 24, 2025
Copy link

vercel bot commented Jan 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
apps-backend ⬜️ Ignored (Inspect) Visit Preview Jan 28, 2025 4:40pm
apps-ui-kit ⬜️ Ignored (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 4:40pm
rebased-explorer ⬜️ Ignored (Inspect) Visit Preview Jan 28, 2025 4:40pm
wallet-dashboard ⬜️ Ignored (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 4:40pm

@iota-ci iota-ci added infrastructure Issues related to the Infrastructure Team sc-platform Issues related to the Smart Contract Platform group. labels Jan 24, 2025
@tomxey tomxey marked this pull request as ready for review January 24, 2025 11:20
@tomxey tomxey requested review from a team as code owners January 24, 2025 11:20
Copy link
Contributor

@kodemartin kodemartin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ⛱️

@muXxer
Copy link
Contributor

muXxer commented Jan 28, 2025

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 indexer-generate-schema.
Content of the Dockerfile:

# Use the official PostgreSQL image as a base
FROM postgres:15

# Install dependencies
RUN apt-get update && apt-get install -y \
    curl \
    build-essential \
    libpq-dev && \
    rm -rf /var/lib/apt/lists/*

ARG RUST_TOOLCHAIN_VERSION

# Install Rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain ${RUST_TOOLCHAIN_VERSION}

# Install Diesel CLI
RUN /root/.cargo/bin/cargo install diesel_cli --no-default-features --features postgres

# Add the Rust and Diesel CLI binaries to PATH
ENV PATH="/root/.cargo/bin:${PATH}"

Little build script build.sh

#!/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 .

generate.sh

#!/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"

@tomxey
Copy link
Contributor Author

tomxey commented Jan 28, 2025

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.
...

@muXxer I like the idea. Pushed changes with dockerized script version.

@tomxey tomxey requested a review from kodemartin January 28, 2025 12:01
Copy link
Contributor

@kodemartin kodemartin left a 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

@tomxey tomxey force-pushed the sc-platform/autogenerate-indexer-schema-using-diesel branch from 656f528 to a5aa568 Compare January 28, 2025 16:39
@tomxey tomxey merged commit 919a9e8 into develop Jan 29, 2025
40 checks passed
@tomxey tomxey deleted the sc-platform/autogenerate-indexer-schema-using-diesel branch January 29, 2025 07:47
lzpap pushed a commit that referenced this pull request Jan 30, 2025
* [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>
lzpap pushed a commit that referenced this pull request Jan 30, 2025
* [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>
lzpap pushed a commit that referenced this pull request Jan 30, 2025
* [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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infrastructure Issues related to the Infrastructure Team sc-platform Issues related to the Smart Contract Platform group.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

indexer: ensure schema always matches what diesel generated
4 participants