if sqlite3 /rails/storage/production.sqlite3 "PRAGMA user_version;"> /dev/null 2>&1;then
echo"✅ Database file is writable"
else
echo"❌ ERROR: Cannot write to database file. Check volume mount and permissions."
# Try to fix permissions again with more aggressive approach
echo"Attempting more aggressive permission fix..."
chown-R rails:rails /rails/storage
chmod-R 777 /rails/storage
chmod 666 /rails/storage/*.sqlite3
# Try to create an empty database structure
echo"Attempting to initialize empty database..."
sqlite3 /rails/storage/production.sqlite3 "CREATE TABLE IF NOT EXISTS schema_migrations (version varchar(255) NOT NULL); CREATE UNIQUE INDEX unique_schema_migrations ON schema_migrations (version);"||true
fi
# Prepare the database with retry logic
echo"Preparing database..."
# First try to manually create schema_migrations table to avoid common issues
echo"Attempting to create schema_migrations table manually..."
for db_file in /rails/storage/production.sqlite3 /rails/storage/production_cache.sqlite3 /rails/storage/production_queue.sqlite3 /rails/storage/production_cable.sqlite3;do
echo"Initializing $db_file..."
sqlite3 "$db_file""CREATE TABLE IF NOT EXISTS schema_migrations (version varchar(255) NOT NULL); CREATE UNIQUE INDEX IF NOT EXISTS unique_schema_migrations ON schema_migrations (version);"||true
done
# Create empty schema.rb file to help Rails recognize the database state
mkdir-p /rails/db
if[!-f"/rails/db/schema.rb"];then
echo"Creating empty schema.rb file..."
cat> /rails/db/schema.rb <<'EOL'
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 0) do
# These are extensions that must be enabled in order to support this database
end
EOL
fi
# Check if we should skip database preparation
if["${SKIP_DB_PREPARATION}"="true"];then
echo"⚠️ SKIP_DB_PREPARATION is set to true, skipping database preparation"
echo"✅ Using existing database files without migrations"
SUCCESS=true
else
# Now try to run db:prepare with multiple attempts
for i in{1..3};do
echo"Database preparation attempt $i..."
# Print current environment for debugging
echo"Current environment:"
echo"- Working directory: $(pwd)"
echo"- User: $(whoami)"
echo"- Storage directory contents:"
ls-la /rails/storage
echo"- Database config:"
cat /rails/config/database.yml | grep-A 10 production
# Try different database commands with increasing aggressiveness
# Initialize the database files with schema_migrations tables
echo"Initializing database files with schema_migrations tables..."
for db_file in /root/img_manager/storage/production.sqlite3 /root/img_manager/storage/production_cache.sqlite3 /root/img_manager/storage/production_queue.sqlite3 /root/img_manager/storage/production_cable.sqlite3;do
sqlite3 "$db_file""CREATE TABLE IF NOT EXISTS schema_migrations (version varchar(255) NOT NULL); CREATE UNIQUE INDEX IF NOT EXISTS unique_schema_migrations ON schema_migrations (version);"||echo"Failed to initialize $db_file"
done
echo"Checking SELinux status..."
if command-v getenforce &> /dev/null;then
selinux_status=$(getenforce)
echo"SELinux status: $selinux_status"
if["$selinux_status"=="Enforcing"];then
echo"SELinux is enforcing. Setting proper contexts..."
# Set SELinux context for the directories
if command-vchcon &> /dev/null;then
chcon-Rt svirt_sandbox_file_t /root/img_manager
else
echo"chcon command not found. Unable to set SELinux context."
fi
fi
else
echo"getenforce command not found. Unable to check SELinux status."