Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Updates and Maintenance

Audience: Lab Operator Prerequisites: Backup & Recovery Time: ~20 minutes

Overview

Keeping MADSci up to date ensures you have the latest features, bug fixes, and security patches. This guide covers upgrade procedures, database migrations, and routine maintenance tasks.

Checking for Updates

Current Version

# Check installed MADSci version
madsci version

# Check all installed package versions
pip list | grep madsci

Available Updates

Check the MADSci releases page for new versions and changelogs.

Upgrade Procedure

Step 1: Read the Changelog

Before upgrading, review the changelog for:

Step 2: Create Backups

Always back up before upgrading:

# Back up all databases
madsci-postgres-backup create \
  --db-url postgresql://postgres:postgres@localhost:5432/resources \
  --backup-dir ./backups/pre-upgrade

for db in events experiments data workcell locations; do
  madsci-mongodb-backup create \
    --mongo-url mongodb://localhost:27017 \
    --database $db \
    --backup-dir ./backups/pre-upgrade/mongodb
done

Step 3: Stop Services

# Stop all services
docker compose down

Step 4: Update Code

# If using git (development install)
git pull origin main
pdm install -G:all

# If using pip (released packages)
pip install --upgrade madsci_common madsci_client madsci_node_module \
  madsci_squid madsci_event_manager madsci_experiment_manager \
  madsci_resource_manager madsci_data_manager madsci_workcell_manager \
  madsci_location_manager madsci_experiment_application

Step 5: Rebuild Docker Images

# Rebuild all images
docker compose build

# Or rebuild specific services
docker compose build workcell_manager event_manager

Step 6: Run Database Migrations

# PostgreSQL migrations (Resource Manager)
python -m madsci.resource_manager.migration_tool \
  --db-url postgresql://postgres:postgres@localhost:5432/resources

# MongoDB migrations (per manager, if needed)
# Check release notes for specific migration commands

Step 7: Start Services

# Start all services
docker compose up -d

# Verify health
madsci status

# Check for migration warnings
madsci logs --level WARNING --tail 50

Step 8: Verify

# Run diagnostics
madsci doctor

# Test a simple workflow
# (Use your lab's standard smoke test)

Database Migrations

PostgreSQL Migrations

The Resource Manager uses Alembic for schema migrations:

# Check current migration status
python -m madsci.resource_manager.migration_tool --status \
  --db-url postgresql://postgres:postgres@localhost:5432/resources

# Run pending migrations
python -m madsci.resource_manager.migration_tool \
  --db-url postgresql://postgres:postgres@localhost:5432/resources

The migration tool automatically:

MongoDB Migrations

MongoDB managers handle index creation and schema validation on startup. Specific migrations are documented in release notes.

Definition File Migration

If upgrading from a version that uses definition files (pre-v0.7.0):

# Scan for files needing migration
madsci migrate scan

# Preview migration
madsci migrate convert --dry-run

# Apply migration
madsci migrate convert --apply

# Verify
madsci migrate status

# After confirming everything works, clean up
madsci migrate finalize

Routine Maintenance

Log Rotation

MADSci event logs grow over time. Configure retention:

# Environment variable for Event Manager
EVENT_RETENTION_DAYS=90
EVENT_ARCHIVE_ENABLED=true
EVENT_ARCHIVE_DIR=/data/event_archives

Database Maintenance

MongoDB

# Check database sizes
docker compose exec mongodb mongosh --eval "
  db.adminCommand('listDatabases').databases.forEach(function(d) {
    print(d.name + ': ' + (d.sizeOnDisk / 1024 / 1024).toFixed(2) + ' MB');
  });
"

# Compact a collection (reclaim disk space)
docker compose exec mongodb mongosh events --eval "
  db.runCommand({compact: 'events'})
"

PostgreSQL

# Check database size
docker compose exec postgres psql -U postgres -c "
  SELECT pg_database.datname,
         pg_size_pretty(pg_database_size(pg_database.datname))
  FROM pg_database
  ORDER BY pg_database_size(pg_database.datname) DESC;
"

# Run vacuum (reclaim space, update statistics)
docker compose exec postgres psql -U postgres -d resources -c "VACUUM ANALYZE;"

Docker Cleanup

# Remove unused images
docker image prune

# Remove unused volumes (careful - don't remove data volumes!)
docker volume prune --filter "label!=keep"

# Remove build cache
docker builder prune

# Full cleanup (stopped containers, unused images, unused networks)
docker system prune

Disk Space Monitoring

# Check overall disk usage
df -h

# Check Docker disk usage
docker system df

# Find large log files
find /var/lib/docker/containers -name "*.log" -size +100M

# Truncate a large container log (if needed)
truncate -s 0 /var/lib/docker/containers/<container_id>/<container_id>-json.log

Rollback Procedure

If an upgrade causes issues:

Step 1: Stop Services

docker compose down

Step 2: Restore Code

# If using git
git checkout <previous_version_tag>
pdm install -G:all

# If using pip
pip install madsci_common==<previous_version> ...

Step 3: Restore Database (if migrations were run)

# Restore PostgreSQL
madsci-postgres-backup restore \
  --db-url postgresql://postgres:postgres@localhost:5432/resources \
  --backup-path ./backups/pre-upgrade/resources_backup.sql

# Restore MongoDB
for db in events experiments data workcell locations; do
  madsci-mongodb-backup restore \
    --mongo-url mongodb://localhost:27017 \
    --database $db \
    --backup-path ./backups/pre-upgrade/mongodb/${db}_backup.bson
done

Step 4: Rebuild and Restart

docker compose build
docker compose up -d
madsci status

Maintenance Schedule

TaskFrequencyCommand
Health checkContinuousmadsci status --watch
Log reviewDailymadsci logs --level WARNING --since 24h
Database backupEvery 6 hoursAutomated via cron
Backup validationWeeklyRestore to test environment
Disk space checkWeeklydf -h && docker system df
Docker cleanupMonthlydocker system prune
MADSci updateAs releasedFollow upgrade procedure
Full recovery testQuarterlyRestore from backup to clean environment

What’s Next?