Skip to content

Commit

Permalink
Only create sqlite DB file if DB_CONNECTION undefined or eq sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
tigattack authored and vraravam committed Oct 26, 2024
1 parent b86d3a6 commit e0f4f60
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions scripts/adonisjs-4-to-5.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e0f4f60

Please sign in to comment.