6.6 KiB
Multi-User Data Migration Guide
This document explains how to migrate existing data to the multi-user system.
Overview
When upgrading to the multi-user version of Qinglong, all existing data (cron tasks, environment variables, subscriptions, and dependencies) will be treated as "legacy data" that is accessible to all users.
To properly isolate data between users, you need to migrate existing data to specific user accounts.
Migration Script
The migrate-to-multiuser.js script helps you assign existing legacy data to specific users.
Prerequisites
- Node.js installed
- Sequelize and dotenv packages (already included in the project)
- At least one user created in the User Management interface
Usage
1. List All Users
First, see all available users in your system:
node migrate-to-multiuser.js --list-users
Output example:
Users in the system:
ID Username Role Status
-- -------- ---- ------
1 admin Admin Enabled
2 user1 User Enabled
3 user2 User Enabled
2. Preview Migration (Dry Run)
Before making changes, preview what will be migrated:
node migrate-to-multiuser.js --userId=1 --dry-run
Or by username:
node migrate-to-multiuser.js --username=admin --dry-run
Output example:
Legacy Data Statistics:
Cron tasks: 15
Environment variables: 8
Subscriptions: 3
Dependencies: 5
DRY RUN: No changes will be made.
Would assign all legacy data to user ID 1
3. Perform Migration
Once you're ready, run the migration without --dry-run:
By User ID:
node migrate-to-multiuser.js --userId=1
By Username:
node migrate-to-multiuser.js --username=admin
Output example:
Found user 'admin' with ID 1
Legacy Data Statistics:
Cron tasks: 15
Environment variables: 8
Subscriptions: 3
Dependencies: 5
Migrating data to user ID 1...
✓ Migrated 15 cron tasks
✓ Migrated 8 environment variables
✓ Migrated 3 subscriptions
✓ Migrated 5 dependencies
✓ Migration completed successfully!
Command Line Options
| Option | Description |
|---|---|
--userId=<id> |
Assign all legacy data to user with this ID |
--username=<name> |
Assign all legacy data to user with this username |
--list-users |
List all users in the system |
--dry-run |
Show what would be changed without making changes |
--help |
Show help message |
Migration Strategy
Scenario 1: Single User to Multi-User
If you're upgrading from single-user to multi-user and want to keep all existing data under one admin account:
- Create an admin user in the User Management interface
- Run migration:
node migrate-to-multiuser.js --username=admin
Scenario 2: Distribute Data to Multiple Users
If you want to distribute existing data to different users:
- Create all necessary user accounts first
- Identify which data belongs to which user (you may need to do this manually by checking the database)
- For each user, manually update the
userIdfield in the database tables (Crontabs,Envs,Subscriptions,Dependences)
SQL Example:
-- Assign specific cron tasks to user ID 2
UPDATE Crontabs
SET userId = 2
WHERE name LIKE '%user2%' AND userId IS NULL;
-- Assign specific environment variables to user ID 2
UPDATE Envs
SET userId = 2
WHERE name LIKE '%USER2%' AND userId IS NULL;
Scenario 3: Keep as Shared Data
If you want certain data to remain accessible to all users:
- Simply don't run the migration script
- Data with
userId = NULLremains as "legacy data" accessible to everyone - This is useful for shared cron tasks or environment variables
Important Notes
-
Backup First: Always backup your database before running migration scripts
cp data/database.sqlite data/database.sqlite.backup -
Test in Dry Run: Always use
--dry-runfirst to see what will change -
One-Time Operation: The script only migrates data where
userIdis NULL- Already migrated data won't be changed
- You can run it multiple times safely
-
Transaction Safety: The migration uses database transactions
- If any error occurs, all changes are rolled back
- Your data remains safe
-
User Must Exist: The target user must exist before migration
- Create users in the User Management interface first
- Use
--list-usersto verify users exist
Troubleshooting
Error: "User not found"
Problem: The specified user doesn't exist in the database.
Solution:
- Run
node migrate-to-multiuser.js --list-usersto see available users - Create the user in User Management interface if needed
- Use correct user ID or username
Error: "Database connection failed"
Problem: Cannot connect to the database.
Solution:
- Check that
data/database.sqliteexists - Verify database file permissions
- Check
QL_DATA_DIRenvironment variable if using custom path
Error: "Migration failed"
Problem: An error occurred during migration.
Solution:
- Check the error message for details
- Verify database is not corrupted
- Restore from backup if needed
- Check database file permissions
Manual Migration
If you prefer to migrate data manually using SQL:
Connect to Database
sqlite3 data/database.sqlite
Check Legacy Data
-- Count legacy cron tasks
SELECT COUNT(*) FROM Crontabs WHERE userId IS NULL;
-- View legacy cron tasks
SELECT id, name, command FROM Crontabs WHERE userId IS NULL;
Migrate Data
-- Migrate all legacy data to user ID 1
UPDATE Crontabs SET userId = 1 WHERE userId IS NULL;
UPDATE Envs SET userId = 1 WHERE userId IS NULL;
UPDATE Subscriptions SET userId = 1 WHERE userId IS NULL;
UPDATE Dependences SET userId = 1 WHERE userId IS NULL;
Verify Migration
-- Check if any legacy data remains
SELECT
(SELECT COUNT(*) FROM Crontabs WHERE userId IS NULL) as legacy_crons,
(SELECT COUNT(*) FROM Envs WHERE userId IS NULL) as legacy_envs,
(SELECT COUNT(*) FROM Subscriptions WHERE userId IS NULL) as legacy_subs,
(SELECT COUNT(*) FROM Dependences WHERE userId IS NULL) as legacy_deps;
Support
If you encounter issues with data migration:
- Check this guide for solutions
- Review the error messages carefully
- Ensure you have a recent backup
- Open an issue on GitHub with:
- Error messages
- Migration command used
- Database statistics (from dry run)
Related Documentation
- MULTI_USER_GUIDE.md - Complete multi-user feature guide
- README.md - Main project documentation