Data Migrate

repository·main·Indexed 23 days ago

https://github.com/ilyakatz/data-migrate

A tool for running versioned, repeatable data migrations alongside standard Rails schema migrations. It provides a dedicated `db/data` directory for data transformations and includes rake tasks for managing data-only migrations or combined schema and data updates. Supports Rails 6.1 through 8.0, with version 9.1.x for Rails 6.0. Features include Capistrano support, Rails Engine compatibility, and programmatic interfaces via DataMigrate::DataMigrator and DataMigrate::MigrationContext.

Tokens
2.8K
Snippets
5
Records
25
Agent score
76%

What's inside data-migrate

  1. Install Data Migrate

    main

    To use data_migrate in your Rails project, add the gem to your Gemfile and run bundle install.

    Rails Version Compatibility:

    • Supports Rails 6.1 through 8.0.
    • For Rails 6.0, use gem version 9.1.x:
    gem 'data_migrate', '~> 9.1.0'

    Note: When you run any provided rake tasks, a data_migrations table will be created in your database to track migration status, mirroring standard schema migrations.

    gem 'data_migrate'
  2. Enable Capistrano support

    main

    To use data_migrate with Capistrano deployments, replace capistrano/rails/migrations with the data_migrate task in your Capfile. This ensures rake db:migrate:with_data is executed during every deploy.

    require 'capistrano/data_migrate'
  3. Configure Rails Engines support

    main

    If your data migrations are located within Rails engines, update the data_migrations_path in your DataMigrate configuration to include the engine paths. For example, if your engines are in an engines folder:

    DataMigrate.configure do |config|
      config.data_migrations_path = ['db/data'] + Dir['engines/**/db/data']
    end
  4. Configure Data Migrate

    main

    You can customize the behavior of data_migrate in an initializer (e.g., config/initializers/data_migrate.rb).

    Important Notes:

    • data_migrate respects ActiveRecord::Base.dump_schema_after_migration. If this is false, the data_schema.rb file will not be generated.
    • To support Rails Engines, you can pass an array of paths to config.data_migrations_path to include engine-specific data directories.
    DataMigrate.configure do |config|
      config.data_migrations_table_name = 'my_migrations_database_name'
      config.data_migrations_path = 'db/awesomepath/'
      config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
      config.db_configuration = {
        'host' => '127.0.0.1',
        'database' => 'awesome_database',
        'adapter' => 'mysql2',
        'username' => 'root',
        'password' => nil,
      }
      config.spec_name = 'primary'
    end
  5. Understand Data vs Schema migrations

    main

    The project distinguishes between two types of migrations:

    1. Schema Migrations: Standard Rails migrations that modify the database structure. These are tracked in the standard schema_migrations table.
    2. Data Migrations: Migrations specifically designed to manipulate data. These are tracked in a separate data schema table and are managed by DataMigrate::DataMigrator.

    When running migrations, the project ensures that both schema and data migrations are sorted and executed in the correct order (typically schema migrations first, then data migrations).

  6. Use Rake tasks for data migrations

    main

    Data Migrate provides several rake tasks to manage data migrations. The data: namespace is used for data-only tasks, while the db: namespace (with :with_data suffixes) allows you to run both schema and data migrations together.

    Key Data Tasks

    • rake data:migrate: Migrate data migrations (options: VERSION=x, VERBOSE=false).
    • rake data:migrate:status: Display status of data migrations.
    • rake data:rollback: Rolls the schema back (specify steps with STEP=n).
    • rake data:version: Retrieves the current data migration version number.

    Combined Schema and Data Tasks

    Use these tasks to ensure both your database schema and data are updated in the correct order (data migrations will run before schema migrations if they share a version sequence).

    • rake db:migrate:with_data: Migrate both the database data and schema.
    • rake db:rollback:with_data: Rolls back the database, checking whether a schema or data migration was invoked last.
    • rake db:migrate:status:with_data: Provides a status view with an additional column indicating the migration type (data vs schema).
  7. Dump migration status using StatusService.dump

    main
    You can programmatically dump the current status of all data migrations to a stream (defaulting to $stdout) using DataMigrate::StatusService.dump. This method identifies the database name and lists the status, migration ID, and name for each migration recorded in the data schema migration table. If the migration table does not exist, it will output a warning message.
  8. Run schema migrations with DataMigrate::SchemaMigration.run

    main

    You can execute schema migrations directly using DataMigrate::SchemaMigration.run. This method wraps ActiveRecord::MigrationContext to allow running migrations in a specific direction (e.g., :up or :down) for a given set of paths and a specific version.

    Arguments:

    • direction: The direction to run the migration (:up or :down).
    • migration_paths: The paths to the migration files.
    • version: The specific version to run to (or from).
  9. Check the status of data migrations

    main

    Use the following methods to inspect the state of your data migrations:

    • needs_migration?: Returns true if there are pending data migrations.
    • migrations_status: Returns the status of all migrations (e.g., whether they are up or down).
    • current_version: Returns the version of the last migrated data migration.