Enhance database initialization and management in entrypoint script; add missing tables and improve error handling

This commit is contained in:
Arunavo Ray
2025-05-19 08:50:45 +05:30
parent 7e2f11e231
commit 8a9acd4bf7
3 changed files with 148 additions and 29 deletions

View File

@@ -31,6 +31,11 @@ COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN pnpm build
# Compile TypeScript scripts to JavaScript
RUN mkdir -p dist/scripts && \
for script in scripts/*.ts; do \
node_modules/.bin/tsc --outDir dist/scripts --module commonjs --target es2020 --esModuleInterop $script || true; \
done
# -----------------------------------
FROM deps AS pruner
@@ -55,14 +60,14 @@ COPY --from=builder /app/data ./data
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=4321
ENV DATABASE_URL=sqlite://data/gitea-mirror.db
ENV DATABASE_URL=file:data/gitea-mirror.db
# Make entrypoint executable
RUN chmod +x /app/docker-entrypoint.sh
ENTRYPOINT ["/app/docker-entrypoint.sh"]
RUN apk add --no-cache wget && \
RUN apk add --no-cache wget sqlite && \
mkdir -p /app/data && \
addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 gitea-mirror && \

View File

@@ -71,18 +71,6 @@ This will create the necessary tables. On first launch, you'll be guided through
Gitea Mirror provides multi-architecture Docker images that work on both ARM64 (e.g., Apple Silicon, Raspberry Pi) and x86_64 (Intel/AMD) platforms.
##### Using Pre-built Images from GitHub Container Registry
```bash
# Pull the latest multi-architecture image
docker pull ghcr.io/arunavo4/gitea-mirror:latest
# Run the application
docker run -d \\
-p 4321:4321 \\
ghcr.io/arunavo4/gitea-mirror:latest
```
##### Using Docker Compose (Recommended)
```bash
@@ -94,6 +82,25 @@ docker-compose --profile production up -d
docker-compose -f docker-compose.dev.yml up -d
```
> **Important**: Docker Compose is the recommended method for running Gitea Mirror as it automatically sets up the required Redis sidecar service that the application depends on.
##### Using Pre-built Images from GitHub Container Registry
If you want to run the container directly without Docker Compose, you'll need to set up a Redis instance separately:
```bash
# First, start a Redis container
docker run -d --name gitea-mirror-redis redis:alpine
# Pull the latest multi-architecture image
docker pull ghcr.io/arunavo4/gitea-mirror:latest
# Run the application with a link to the Redis container
docker run -d -p 4321:4321 --link gitea-mirror-redis:redis \
-e REDIS_URL=redis://redis:6379 \
ghcr.io/arunavo4/gitea-mirror:latest
```
##### Building Docker Images Manually
The project includes a build script to create and manage multi-architecture Docker images:
@@ -145,10 +152,11 @@ docker volume create gitea-mirror-data
The Docker container can be configured with the following environment variables:
- `DATABASE_URL`: SQLite database URL (default: `sqlite://data/gitea-mirror.db`)
- `DATABASE_URL`: SQLite database URL (default: `file:data/gitea-mirror.db`)
- `HOST`: Host to bind to (default: `0.0.0.0`)
- `PORT`: Port to listen on (default: `4321`)
- `JWT_SECRET`: Secret key for JWT token generation (important for security)
- `REDIS_URL`: URL for Redis connection (required, default: none)
#### Manual Installation
@@ -227,8 +235,14 @@ Gitea Mirror follows a modular architecture with clear separation of concerns. S
│ │ │ │ │ │ │ │
│ │ Frontend │◄───►│ Backend │◄───►│ Database │ │
│ │ (Astro) │ │ (Node.js) │ │ (SQLite) │ │
│ │ │ │ │ │ │ │
│ └─────────────┘ └────────────┘ └─────────────────┘ │
│ │ │ │ │ │ │ │
│ └─────────────┘ └────────────┘ └─────────────────┘ │
│ │ │
│ ┌──────┴──────┐ │
│ │ │ │
│ │ Redis │ │
│ │ │ │
│ └──────┬──────┘ │
│ │ │
└─────────────────────────────┼───────────────────────────────────┘
@@ -246,6 +260,8 @@ Gitea Mirror follows a modular architecture with clear separation of concerns. S
└─────────────────────────────────────────┘
```
> **Note**: Redis is a required component for Gitea Mirror as it's used for job queuing and caching.
## Development
### Local Development Setup
@@ -344,6 +360,7 @@ GITEA_USERNAME=your-local-gitea-username
- **Frontend**: Astro, React, Shadcn UI, Tailwind CSS v4
- **Backend**: Node.js
- **Database**: SQLite (default) or PostgreSQL
- **Caching/Queue**: Redis
- **API Integration**: GitHub API (Octokit), Gitea API
## Contributing

View File

@@ -14,29 +14,126 @@ fi
# Initialize the database if it doesn't exist
if [ ! -f "/app/data/gitea-mirror.db" ]; then
echo "Initializing database..."
if [ -f "scripts/init-db.ts" ]; then
node -r tsx/cjs scripts/init-db.ts
elif [ -f "scripts/manage-db.ts" ]; then
node -r tsx/cjs scripts/manage-db.ts init
if [ -f "dist/scripts/init-db.js" ]; then
node dist/scripts/init-db.js
elif [ -f "dist/scripts/manage-db.js" ]; then
node dist/scripts/manage-db.js init
else
echo "Warning: Could not find database initialization scripts in dist/scripts."
echo "Creating and initializing database manually..."
# Create the database file
touch /app/data/gitea-mirror.db
# Initialize the database with required tables
sqlite3 /app/data/gitea-mirror.db <<EOF
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS configs (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
is_active INTEGER NOT NULL DEFAULT 1,
github_config TEXT NOT NULL,
gitea_config TEXT NOT NULL,
include TEXT NOT NULL DEFAULT '["*"]',
exclude TEXT NOT NULL DEFAULT '[]',
schedule_config TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
updated_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS repositories (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
config_id TEXT NOT NULL,
name TEXT NOT NULL,
full_name TEXT NOT NULL,
url TEXT NOT NULL,
clone_url TEXT NOT NULL,
owner TEXT NOT NULL,
organization TEXT,
mirrored_location TEXT DEFAULT '',
is_private INTEGER NOT NULL DEFAULT 0,
is_fork INTEGER NOT NULL DEFAULT 0,
forked_from TEXT,
has_issues INTEGER NOT NULL DEFAULT 0,
is_starred INTEGER NOT NULL DEFAULT 0,
is_archived INTEGER NOT NULL DEFAULT 0,
size INTEGER NOT NULL DEFAULT 0,
has_lfs INTEGER NOT NULL DEFAULT 0,
has_submodules INTEGER NOT NULL DEFAULT 0,
default_branch TEXT NOT NULL,
visibility TEXT NOT NULL DEFAULT 'public',
status TEXT NOT NULL DEFAULT 'imported',
last_mirrored INTEGER,
error_message TEXT,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
updated_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (config_id) REFERENCES configs(id)
);
CREATE TABLE IF NOT EXISTS organizations (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
config_id TEXT NOT NULL,
name TEXT NOT NULL,
avatar_url TEXT NOT NULL,
membership_role TEXT NOT NULL DEFAULT 'member',
is_included INTEGER NOT NULL DEFAULT 1,
status TEXT NOT NULL DEFAULT 'imported',
last_mirrored INTEGER,
error_message TEXT,
repository_count INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
updated_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (config_id) REFERENCES configs(id)
);
CREATE TABLE IF NOT EXISTS mirror_jobs (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
repository_id TEXT,
repository_name TEXT,
organization_id TEXT,
organization_name TEXT,
details TEXT,
status TEXT NOT NULL DEFAULT 'imported',
message TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
EOF
echo "Database initialized with required tables."
fi
else
echo "Database already exists, checking for issues..."
if [ -f "scripts/fix-db-issues.ts" ]; then
node -r tsx/cjs scripts/fix-db-issues.ts
elif [ -f "scripts/manage-db.ts" ]; then
node -r tsx/cjs scripts/manage-db.ts fix
if [ -f "dist/scripts/fix-db-issues.js" ]; then
node dist/scripts/fix-db-issues.js
elif [ -f "dist/scripts/manage-db.js" ]; then
node dist/scripts/manage-db.js fix
fi
# Update the database schema
echo "Updating database schema..."
if [ -f "scripts/manage-db.ts" ]; then
node -r tsx/cjs scripts/manage-db.ts update-schema
if [ -f "dist/scripts/manage-db.js" ]; then
node dist/scripts/manage-db.js update-schema
fi
# Run migrations
echo "Running database migrations..."
if [ -f "scripts/run-migrations.ts" ]; then
node -r tsx/cjs scripts/run-migrations.ts
if [ -f "dist/scripts/run-migrations.js" ]; then
node dist/scripts/run-migrations.js
fi
fi