Skip to content

Commit fa3f088

Browse files
db fix
1 parent d09eee9 commit fa3f088

2 files changed

Lines changed: 42 additions & 35 deletions

File tree

entrypoint.sh

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,25 @@ PGID=${PGID:-1000}
88
CURRENT_UID=$(id -u appuser)
99
CURRENT_GID=$(id -g appuser)
1010

11-
# Only modify user/group if they're different from current
11+
# Adjust IDs if needed
1212
if [ "$PUID" != "$CURRENT_UID" ] || [ "$PGID" != "$CURRENT_GID" ]; then
13-
echo "Updating user appuser to UID:$PUID and GID:$PGID"
14-
15-
# Update group ID if needed
16-
if [ "$PGID" != "$CURRENT_GID" ]; then
17-
groupmod -g "$PGID" appuser
18-
fi
19-
20-
# Update user ID if needed
21-
if [ "$PUID" != "$CURRENT_UID" ]; then
22-
usermod -u "$PUID" appuser
23-
fi
24-
25-
# Fix ownership of app directory
26-
chown -R appuser:appuser /app
27-
28-
# Fix ownership of mounted volumes if they exist
29-
[ -d /app/data ] && chown -R appuser:appuser /app/data
30-
[ -d /app/backups ] && chown -R appuser:appuser /app/backups
31-
[ -d /app/logs ] && chown -R appuser:appuser /app/logs
13+
echo "Updating user appuser to UID:$PUID GID:$PGID"
14+
[ "$PGID" != "$CURRENT_GID" ] && groupmod -g "$PGID" appuser
15+
[ "$PUID" != "$CURRENT_UID" ] && usermod -u "$PUID" appuser
3216
fi
3317

34-
# Switch to appuser and execute the original command
18+
# Always ensure directories exist and are owned correctly (important for SQLite write access)
19+
for d in /app/data /app/backups /app/logs; do
20+
mkdir -p "$d"
21+
chown -R appuser:appuser "$d" 2>/dev/null || true
22+
chmod 775 "$d" 2>/dev/null || true
23+
done
24+
25+
# Ensure app code ownership (helps when mounting volumes)
26+
chown -R appuser:appuser /app 2>/dev/null || true
27+
28+
echo "Directory permissions:"
29+
ls -ld /app/data /app/backups /app/logs 2>/dev/null || true
30+
31+
echo "Switching to appuser..."
3532
exec gosu appuser "$@"

init_db.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22
from app import app
33
from models import db
44

5+
def ensure_sqlite_path():
6+
uri = app.config.get('SQLALCHEMY_DATABASE_URI')
7+
if uri and uri.startswith('sqlite:///'):
8+
db_file = uri.replace('sqlite:///', '')
9+
db_dir = os.path.dirname(db_file)
10+
if db_dir and not os.path.isdir(db_dir):
11+
os.makedirs(db_dir, exist_ok=True)
12+
print(f"Created database directory: {db_dir}")
13+
print(f"Target SQLite file: {db_file}")
14+
return db_file
15+
return None
16+
517
if __name__ == '__main__':
6-
# Ensure the data directory exists
7-
data_dir = '/app/data'
8-
os.makedirs(data_dir, exist_ok=True)
9-
print(f"Ensured data directory exists: {data_dir}")
10-
18+
db_file = ensure_sqlite_path()
1119
with app.app_context():
12-
db.create_all()
13-
print("Database initialized successfully!")
14-
15-
# Verify the database file was created
16-
db_path = '/app/data/github_backup.db'
17-
if os.path.exists(db_path):
18-
print(f"Database file created at: {db_path}")
19-
else:
20-
print("Warning: Database file not found after initialization")
20+
try:
21+
db.create_all()
22+
print("Database initialized successfully!")
23+
if db_file and os.path.exists(db_file):
24+
st = os.stat(db_file)
25+
print(f"Database file created: {db_file} (size {st.st_size} bytes, perms {oct(st.st_mode)[-3:]})")
26+
elif db_file:
27+
print(f"Warning: Database file not found yet at {db_file}")
28+
except Exception as e:
29+
print(f"Error creating database: {e}")
30+
raise

0 commit comments

Comments
 (0)