Files
gitea-mirror/scripts
ARUNAVO RAY d697cb2bc9 fix: prevent starred repo name collisions during concurrent mirroring (#236)
* fix: prevent starred repo name collisions during concurrent mirroring (#95)

When multiple starred repos share the same short name (e.g. alice/dotfiles
and bob/dotfiles), concurrent batch mirroring could cause 409 Conflict
errors because generateUniqueRepoName only checked Gitea via HTTP, missing
repos that were claimed in the local DB but not yet created remotely.

Three fixes:
- Add DB-level check in generateUniqueRepoName so it queries the local
  repositories table for existing mirroredLocation claims, preventing two
  concurrent jobs from picking the same target name.
- Clear mirroredLocation on failed mirror so a failed repo doesn't falsely
  hold a location that was never successfully created, which would block
  retries and confuse the uniqueness check.
- Extract isMirroredLocationClaimedInDb helper for the DB lookup, using
  ne() to exclude the current repo's own record from the collision check.

* fix: address review findings for starred repo name collision fix

- Make generateUniqueRepoName immediately claim name by writing
  mirroredLocation to DB, closing the TOCTOU race window between
  name selection and the later status="mirroring" DB update
- Add fullName validation guard (must contain "/")
- Make isMirroredLocationClaimedInDb fail-closed (return true on
  DB error) to be conservative about preventing collisions
- Scope mirroredLocation clear on failure to starred repos only,
  preserving it for non-starred repos that may have partially
  created in Gitea and need the location for recovery

* fix: address P1/P2 review findings for starred repo name collision

P1a: Remove early name claiming from generateUniqueRepoName to prevent
stale claims on early return paths. The function now only checks
availability — the actual claim happens at the status="mirroring" DB
write (after both idempotency checks), which is protected by a new
unique partial index.

P1b: Add unique partial index on (userId, mirroredLocation) WHERE
mirroredLocation != '' via migration 0010. This enforces atomicity at
the DB level: if two concurrent workers try to claim the same name,
the second gets a constraint violation rather than silently colliding.

P2: Only clear mirroredLocation on failure if the Gitea migrate call
itself failed (migrateSucceeded flag). If migrate succeeded but
metadata mirroring failed, preserve the location since the repo
physically exists in Gitea and we need it for recovery/retry.
2026-03-18 15:27:20 +05:30
..
2025-05-18 09:31:23 +05:30
2025-07-11 01:17:54 +05:30
2025-07-11 01:17:54 +05:30
2025-06-17 10:30:33 +05:30
2025-06-17 10:30:33 +05:30
2025-07-11 01:17:54 +05:30
2026-02-24 09:45:06 +05:30
2025-06-17 10:30:33 +05:30
2025-08-29 17:04:48 +05:30
2025-07-11 01:04:50 +05:30
2025-08-28 08:34:27 +05:30

Scripts Directory

This folder contains utility scripts for database management, event management, Docker builds, and LXC container deployment.

Database Management

Database Management Tool (manage-db.ts)

This is a consolidated database management tool that handles all database-related operations. It combines the functionality of the previous separate scripts into a single, more intelligent script that can check, fix, and initialize the database as needed.

Features

  • Check Mode: Validates the existence and integrity of the database
  • Init Mode: Creates the database only if it doesn't already exist
  • Fix Mode: Corrects database file location issues
  • Reset Users Mode: Removes all users and their data
  • Auto Mode: Automatically checks, fixes, and initializes the database if needed

Running the Database Management Tool

You can execute the database management tool using your package manager with various commands:

# Checks database status (default action if no command is specified)
bun run manage-db

# Check database status
bun run check-db

# Initialize the database (only if it doesn't exist)
bun run init-db

# Fix database location issues
bun run fix-db

# Automatic check, fix, and initialize if needed
bun run db-auto

# Reset all users (for testing signup flow)
bun run reset-users

# Remove database files completely
bun run cleanup-db

# Complete setup (install dependencies and initialize database)
bun run setup

# Start development server with a fresh database
bun run dev:clean

# Start production server with a fresh database
bun run start:fresh

Database File Location

The database file should be located in the ./data/gitea-mirror.db directory. If the file is found in the root directory, the fix mode will move it to the correct location.

Event Management

The following scripts help manage events in the SQLite database:

Note

: For a more user-friendly approach, you can use the cleanup button in the Activity Log page of the web interface to delete all activities with a single click.

Remove Duplicate Events (remove-duplicate-events.ts)

Specifically removes duplicate events based on deduplication keys without affecting old events.

# Remove duplicate events for all users
bun scripts/remove-duplicate-events.ts

# Remove duplicate events for a specific user
bun scripts/remove-duplicate-events.ts <userId>

Fix Interrupted Jobs (fix-interrupted-jobs.ts)

Fixes interrupted jobs that might be preventing cleanup by marking them as failed.

# Fix all interrupted jobs
bun scripts/fix-interrupted-jobs.ts

# Fix interrupted jobs for a specific user
bun scripts/fix-interrupted-jobs.ts <userId>

Use this script if you're having trouble cleaning up activities due to "interrupted" jobs that won't delete.

Startup Recovery (startup-recovery.ts)

Runs job recovery during application startup to handle any interrupted jobs from previous runs.

# Run startup recovery (normal mode)
bun scripts/startup-recovery.ts

# Force recovery even if recent attempt was made
bun scripts/startup-recovery.ts --force

# Set custom timeout (default: 30000ms)
bun scripts/startup-recovery.ts --timeout=60000

# Using npm scripts
bun run startup-recovery
bun run startup-recovery-force

This script is automatically run by the Docker entrypoint during container startup. It ensures that any jobs interrupted by container restarts or application crashes are properly recovered or marked as failed.

Deployment Scripts

Docker Deployment

  • build-docker.sh: Builds the Docker image for the application
  • docker-diagnostics.sh: Provides diagnostic information for Docker deployments

LXC Container Deployment

Two deployment options are available for LXC containers:

  1. Proxmox VE (online): Using the community-maintained script by Tobias (CrazyWolf13)

  2. gitea-mirror-lxc-local.sh: For offline/LAN-only deployment on a developer laptop

    • Pushes your local checkout + Bun ZIP to the container
    • Useful for testing without internet access

For detailed instructions on LXC deployment, see README-lxc.md.