Locked video

Please purchase the course to watch this video.

Buy Now

Migrations

Auto-migrations

Summary

Sails provides auto-migrations to streamline database schema updates during development. However, this feature should not be used in production as it may result in data loss.

Three Migration Strategies in Sails:

  1. Safe (Default in Production)

    • No automatic schema updates.

    • You must handle migrations manually.

  2. Alter (Default in Development)

    • Updates schema while preserving data.

    • Ideal for development when making frequent changes.

  3. Drop (Best for Testing)

    • Deletes and recreates the database on each lift.

    • Ensures a clean slate for tests.

Key Takeaway: Use alter for development, drop for testing, and safe for production to avoid unintended data loss.

Transcript

In the last lesson, we covered manual migrations—updating your database outside Sails. Now, we'll explore Sails' built-in auto-migrations, a convenient feature for iterating over your database schema during development.

Sails recognizes that your data model evolves as you build, adding or removing fields based on new requirements. Auto-migrations help by automatically updating your database schema without requiring manual intervention. However, Sails does not recommend using this in production, as it can result in data loss. Auto-migrations are intended for development and testing when working with temporary or non-critical data.

Auto-Migration Strategies in Sails

Sails provides three migration strategies:

  1. Safe (Default in Production)

    • Prevents Sails from modifying your database schema.

    • You manage migrations manually.

    • Used to protect sensitive production data.

  2. Alter (Default in Development)

    • Updates the schema while preserving existing data.

    • Sails temporarily stores data in memory, recreates tables/collections, then restores data.

    • Ideal for development when schema changes frequently.

  3. Drop (Recommended for Testing)

    • Completely resets the database on each lift.

    • Deletes all data and recreates tables/collections.

    • Useful for test environments where fresh data is needed for each run.

In a Sails app, migration strategies are set in config/models.js under the migrate property.

  • Safe Mode: If set to safe, new attributes in models won’t be reflected in the database until you manually update it.

  • Alter Mode: When set to alter, Sails updates the schema and retains data. Adding a new field (e.g., age) will update the schema without losing existing records.

  • Drop Mode: Setting migrate to drop erases all data on each lift. This ensures test cases run with a clean database every time.

Recap

  • Safe: No automatic schema changes (default in production).

  • Alter: Modifies schema while preserving data (default in development).

  • Drop: Deletes and recreates schema on every lift (ideal for tests).

Use auto-migrations carefully. They're great for development and testing but should be avoided in production.

Full Course

$
34.99

USD

plus local taxes
Buy Now