Skip to content

Add Database Migration Tools for PostgreSQL and MongoDB - #112

Merged
RyanTheRobothead merged 167 commits into
mainfrom
db_migrations
Dec 4, 2025
Merged

Add Database Migration Tools for PostgreSQL and MongoDB#112
RyanTheRobothead merged 167 commits into
mainfrom
db_migrations

Conversation

@Dozgulbas

@Dozgulbas Dozgulbas commented Sep 15, 2025

Copy link
Copy Markdown
Contributor

PR Info

Implements database migration tools for MADSci's Resource Manager (PostgreSQL), Event Manager (MongoDB), and Data Manager (MongoDB) with automated version checking and safe schema evolution.

Breaking Changes

  • All the managers listed below will refuse to start unless the migration is completed.
  • Workcell Manager, Data Manager, Experiment Manager, Event Manager and Resource Manager

PostgreSQL Migration (Resource Manager)

  • Alembic-based migrations with automatic schema generation and type conversion safety
  • Version compatibility checking between MADSci package and database schema versions
  • Automated backup/restore using pg_dump/psql with rollback on migration failure
  • Server startup validation - Resource server refuses to start on version mismatch

MongoDB Migration (Event & Data Managers)

  • Schema-driven migrations for collection and index management
  • Version tracking using schema_versions collection in each database
  • Automated backup/restore using mongodump/mongorestore
  • Safe migration process with transactional rollback on failure

Server Integration
All servers now perform version validation on startup:

  • Resource Server: Validates PostgreSQL schema version using Alembic version table
  • Event Server: Validates MongoDB schema version using madsci_events.schema_versions
  • Data Server: Validates MongoDB schema version using madsci_data.schema_versions

Servers refuse to start on version mismatch and display clear migration instructions.

Migration Commands

  1. PostgreSQL (Resource Manager)
# Migrate to current MADSci version
python -m madsci.resource_manager.migration_tool --db-url postgresql://user:pass@host/db

# Create backup only  
python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --backup-only

# Restore from backup
python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --restore-from /path/to/backup.sql

# Generate new migration file
python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --generate-migration "Add new feature"

Docker Usage

# Run migration to current MADSci version in Docker
docker-compose run --rm -v $(pwd)/src:/home/madsci/MADSci/src resource-manager python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources'

# Migrate to specific version in Docker
docker-compose run --rm -v $(pwd)/src:/home/madsci/MADSci/src resource-manager python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --target-version 1.0.0

# Create backup only in Docker
docker-compose run --rm -v $(pwd)/src:/home/madsci/MADSci/src resource-manager python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --backup-only

# Restore form a backup file in Docker
docker-compose run --rm -v $(pwd)/src:/home/madsci/MADSci/src resource-manager python -m madsci.resource_manager.migration_tool --db-url 'postgresql://user:pass@postgres:5432/resources' --restore-from /path/to/backup.sql
  1. MongoDB (Event/Data Managers)
# Run migration for events database (auto-detects schema file)
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017  --database madsci_events

# Migrate with explicit database URL
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017 --database madsci_events

# Use custom schema file
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017  --database madsci_events --schema-file /path/to/schema.json

# Create backup only
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017  --database madsci_events --backup-only

# Restore from backup
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017  --database madsci_events --restore-from /path/to/backup

# Check version compatibility without migrating
python -m madsci.common.mongodb_migration_tool --db-url mongodb://localhost:27017  --database madsci_events --check-version

Docker Usage

# Run migration for events database in Docker
docker-compose run --rm event-manager python -m madsci.common.mongodb_migration_tool --db-url 'mongodb://mongodb:27017' --database 'madsci_events' --schema-file '/app/madsci/event_manager/schema.json'

# Create backup only in Docker
docker-compose run --rm event-manager python -m madsci.common.mongodb_migration_tool --db-url 'mongodb://mongodb:27017' --database 'madsci_events' --schema-file '/app/madsci/event_manager/schema.json' --backup-only

# Check version compatibility in Docker
docker-compose run --rm event-manager python -m madsci.common.mongodb_migration_tool --db-url 'mongodb://mongodb:27017' --database 'madsci_events' --schema-file '/app/madsci/event_manager/schema.json' --check-version

Schema Management

  • PostgreSQL: Uses Alembic auto-generation from SQLModel table definitions
  • MongoDB: Uses JSON schema files (schema.json) for collection/index definitions
  • Version source: MADSci package version for all databases (single source of truth)

Safety Features

  • Pre-migration database backups with automatic restore on failure
  • Version tracking prevents accidental downgrades
  • Background index creation to avoid blocking operations

Closes: #92

Developer Checklists

I have:

  • Run Pre-commit and Unit Tests, and ensured that they pass
  • Created or updated documentation relevant to your change
  • Created or updated unit tests relevant to your change

@Dozgulbas

Copy link
Copy Markdown
Contributor Author

Oh, one other thing: we should consider having a pydantic model for our mongo schema definition, so we can easily validate a schema file and the mongo db's existing schema definition

Added schema validators for this.

@Dozgulbas

Copy link
Copy Markdown
Contributor Author

@LuckierDodge Should be ready to review for the second time

@RyanTheRobothead
RyanTheRobothead self-requested a review November 17, 2025 18:12
@RyanTheRobothead
RyanTheRobothead merged commit 2663540 into main Dec 4, 2025
5 checks passed
@RyanTheRobothead
RyanTheRobothead deleted the db_migrations branch December 27, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Automatic Schema Migration

2 participants