From e0f4f600cbe0f8a4de7cf735ab6332de69bdcff3 Mon Sep 17 00:00:00 2001 From: tigattack <10629864+tigattack@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:00:45 +0000 Subject: [PATCH] Only create sqlite DB file if DB_CONNECTION undefined or eq sqlite --- scripts/adonisjs-4-to-5.sh | 70 ++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/scripts/adonisjs-4-to-5.sh b/scripts/adonisjs-4-to-5.sh index ccb5c9f9..0b2cfef2 100644 --- a/scripts/adonisjs-4-to-5.sh +++ b/scripts/adonisjs-4-to-5.sh @@ -13,38 +13,42 @@ if [ -z "$DATA_DIR" ]; then DATA_DIR="/data" fi -# Define the path to your SQLite database file -db_file="$DATA_DIR/$DB_DATABASE.sqlite" - -# Check if the database file exists -if [ ! -f "$db_file" ]; then - echo "Database file '$db_file' not found. An empty database will be created." - exit 0 -fi - -# Check if the "adonis_schema_versions" table exists and if the version is less than 2 -version=$(sqlite3 "$db_file" "SELECT version FROM adonis_schema_versions LIMIT 1;" 2>/dev/null) -if [ -z "$version" ] || [ "$version" -lt 2 ]; then - # Table not found or version less than 2, proceed - echo "-- Starting database migration from AdonisJS v4 to v5 --" - - # Check if the "adonis_schema" table exists - schema_exists=$(sqlite3 "$db_file" "SELECT name FROM sqlite_master WHERE type='table' AND name='adonis_schema';" 2>/dev/null) - if [ -n "$schema_exists" ]; then - # "adonis_schema" table exists, proceed - - # Iterate through rows in the "name" column of "adonis_schema" and append "database/migrations" to each value - sqlite3 -batch "$db_file" "SELECT name FROM adonis_schema;" 2>/dev/null | while read -r old_value; do - new_value="database/migrations/$old_value" - echo "Updating value from '$old_value' to '$new_value'" - - # Update the value in the database - sqlite3 "$db_file" "UPDATE adonis_schema SET name='$new_value' WHERE name='$old_value';" 2>/dev/null - done - else - echo "ERROR: Table 'adonis_schema' not found." - exit 1 +# Check if $DB_CONNECTION is undefined or equals 'sqlite' +if [ -z "$DB_CONNECTION" ] || [ "$DB_CONNECTION" = "sqlite" ]; then + + # Define the path to your SQLite database file + db_file="$DATA_DIR/$DB_DATABASE.sqlite" + + # Check if the database file exists + if [ ! -f "$db_file" ]; then + echo "Database file '$db_file' not found. An empty database will be created." + exit 0 + fi + + # Check if the "adonis_schema_versions" table exists and if the version is less than 2 + version=$(sqlite3 "$db_file" "SELECT version FROM adonis_schema_versions LIMIT 1;" 2>/dev/null) + if [ -z "$version" ] || [ "$version" -lt 2 ]; then + # Table not found or version less than 2, proceed + echo "-- Starting database migration from AdonisJS v4 to v5 --" + + # Check if the "adonis_schema" table exists + schema_exists=$(sqlite3 "$db_file" "SELECT name FROM sqlite_master WHERE type='table' AND name='adonis_schema';" 2>/dev/null) + if [ -n "$schema_exists" ]; then + # "adonis_schema" table exists, proceed + + # Iterate through rows in the "name" column of "adonis_schema" and append "database/migrations" to each value + sqlite3 -batch "$db_file" "SELECT name FROM adonis_schema;" 2>/dev/null | while read -r old_value; do + new_value="database/migrations/$old_value" + echo "Updating value from '$old_value' to '$new_value'" + + # Update the value in the database + sqlite3 "$db_file" "UPDATE adonis_schema SET name='$new_value' WHERE name='$old_value';" 2>/dev/null + done + else + echo "ERROR: Table 'adonis_schema' not found." + exit 1 + fi + # else + # echo "Version is greater than or equal to 2. Exiting script." fi -# else - # echo "Version is greater than or equal to 2. Exiting script." fi