Skip to content

Instantly share code, notes, and snippets.

@Patrick-Lehner-Woven-by-Toyota
Created March 20, 2024 08:01
Show Gist options
  • Save Patrick-Lehner-Woven-by-Toyota/811668880cf78a1bc78c679993ce137d to your computer and use it in GitHub Desktop.
Save Patrick-Lehner-Woven-by-Toyota/811668880cf78a1bc78c679993ce137d to your computer and use it in GitHub Desktop.
#!/bin/bash
# This is a minimal reproducing example for https://github.com/SeaQL/sea-schema/issues/130.
###############################################################################
### Set things up ###
###############################################################################
# These `set` lines make the script fail in certain conditions (see http://go/set-euxo for more details)
set -e # immediately exit if any command has a non-zero exit status
set -u # immediately exit if referencing any variable not previously defined
# if a command in a pipeline fails, use that return code for the pipeline, instead of the pipline's last command
set -o pipefail
# Check that command dependencies are available
for cmd in {docker,sea-orm-cli}; do
if ! command -v ${cmd} > /dev/null; then
>&2 echo "This script requires '${cmd}' to be installed."
exit 1
fi
done
# Check that commands are available in the right version
if ! sea-orm-cli --version | grep -qE ' 0\.12\.15+$'; then
>&2 echo "This script requires sea-orm-cli v0.12.15 or newer to be installed, but 'sea-orm-cli --version' returned:"
>&2 echo " " $(sea-orm-cli --version)
exit 1
fi
container_name="seaorm-fk-issue-mre"
db_user="postgres"
db_password="pg1234"
echo
echo "###############################################################################"
echo "### Set up DB and tables ###"
echo "###############################################################################"
echo
set -x # turn on command trace output to show what's happening (see http://go/set-euxo for more details)
docker run --name "$container_name" \
-e POSTGRES_PASSWORD="$db_password" \
-e POSTGRES_USER="$db_user" \
-e POSTGRES_DB="$db_user" \
-p 5432:5432 \
-d postgres:14.5
# wait for postgres to start and be ready
while ! docker exec "$container_name" psql -U "$db_user" -d "$db_user" -c "" > /dev/null 2> /dev/null ; do
sleep 1
done
docker exec "$container_name" psql -U "$db_user" -d "$db_user" -c "
CREATE TABLE "table1" (
"id" SERIAL NOT NULL PRIMARY KEY,
"u" INT NOT NULL
);
CREATE UNIQUE INDEX "u_idx" ON "table1" ("u");
CREATE TABLE "table2" (
"fk_u" INT NOT NULL PRIMARY KEY REFERENCES "table1" ("u")
);
"
docker exec "$container_name" psql -U "$db_user" -d "$db_user" -c "\d table1"
docker exec "$container_name" psql -U "$db_user" -d "$db_user" -c "\d table2"
set +x # turn off command trace output
echo
echo "###############################################################################"
echo "### Run sea-orm-cli to show the issue ###"
echo "###############################################################################"
echo
sea-orm-cli generate entity -v \
-u "postgres://$db_user:$db_password@localhost:5432/$db_user" || (
echo
echo "The sea-orm-cli invocation failed, see error above."
echo "The postgres docker container '$container_name' is left running for your inspection."
exit 1
)
echo
echo "###############################################################################"
echo "### If you can see this message printed, the issue did not occur ###"
echo "###############################################################################"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment