README
¶
MongoDB to PostgreSQL Migration Tool
This tool migrates data from MongoDB to PostgreSQL for the Alita Robot project.
Features
- Batch processing for efficient migration of large datasets
- Progress tracking and statistics
- Dry-run mode for testing without writing data
- Upsert support to handle existing data
- Comprehensive error handling and logging
- Support for all Alita Robot collections
Prerequisites
- MongoDB instance with existing data
- PostgreSQL database with schema already created (run the migration SQL first)
- Go 1.21 or higher
Installation
cd cmd/migrate
go build -o migrate
Configuration
Create a .env file based on .env.example:
# MongoDB Configuration
MONGO_URI=mongodb://localhost:27017
MONGO_DATABASE=alita
# PostgreSQL Configuration
DATABASE_URL=postgres://user:password@localhost:5432/alita_db?sslmode=disable
Usage
Basic Migration
./migrate
With Command-Line Flags
./migrate \
-mongo-uri="mongodb://localhost:27017" \
-mongo-db="alita" \
-postgres-dsn="postgres://user:pass@localhost/alita_db" \
-batch-size=500 \
-verbose
Dry Run (Test Mode)
./migrate -dry-run
This will simulate the migration without writing any data to PostgreSQL.
Command-Line Options
-mongo-uri: MongoDB connection URI-mongo-db: MongoDB database name (default: "alita")-postgres-dsn: PostgreSQL connection DSN-batch-size: Number of records to process in each batch (default: 1000)-dry-run: Perform a dry run without writing to PostgreSQL-verbose: Enable verbose logging
Migration Process
The tool migrates the following collections:
- users - User information and preferences
- chats - Chat groups and their metadata
- admin - Admin settings per chat
- notes_settings - Note configuration per chat
- notes - Saved notes and messages
- filters - Keyword filters and auto-responses
- greetings - Welcome and goodbye messages
- locks - Permission locks and restrictions
- pins - Pin settings per chat
- rules - Chat rules
- warns_settings - Warning system configuration
- warns_users - User warnings
- antiflood_settings - Anti-flood configuration
- blacklists - Blacklisted words
- channels - Linked channels
- connection - User-chat connections
- connection_settings - Connection configuration
- disable - Disabled commands per chat
- report_user_settings - User report settings
- report_chat_settings - Chat report settings
Data Transformations
The migration handles several data transformations:
- Nested documents are flattened (e.g., greetings.welcome_settings)
- Arrays are converted to JSONB (e.g., warns, chat users) or expanded to individual rows (e.g., blacklist triggers)
- MongoDB Long types are converted to PostgreSQL bigint
- Missing fields are handled with appropriate defaults
- Permissions/Restrictions in locks are expanded to individual rows
- Blacklist triggers are expanded from a single array to individual word entries
Special Considerations
- Chat Users: The
chats.usersarray is migrated to both a JSONB column and a separatechat_usersjunction table - Locks: Permissions and restrictions are expanded from nested objects to individual lock_type rows
- Disable: Commands array is expanded to individual command rows
- Blacklists: The
triggersarray is expanded so each blacklisted word becomes a separate row in PostgreSQL
Error Handling
- Failed records are logged but don't stop the migration
- Each collection is migrated independently
- Statistics show success/failure counts
- Detailed error messages are provided for debugging
Post-Migration
After migration:
- Verify row counts match between MongoDB and PostgreSQL
- Test the application with the migrated data
- Update PostgreSQL sequences if needed
- Consider creating additional indexes for performance
Troubleshooting
Connection Issues
- Ensure MongoDB is accessible and running
- Verify PostgreSQL credentials and database exists
- Check network connectivity between the tool and databases
Data Issues
- Run with
-verboseflag for detailed logging - Use
-dry-runto test without writing data - Check the error logs for specific record failures
Performance
- Adjust
-batch-sizebased on your system resources - Smaller batches use less memory but take longer
- Larger batches are faster but require more memory
Development
To modify the migration:
- Update models in
models.gofor data structures - Modify migration functions in
migrations.gofor transformation logic - Adjust batch processing in
main.gofor performance tuning
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.