Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#224] Support custom data migration template #232

Closed
wants to merge 3 commits into from

Conversation

bazay
Copy link
Contributor

@bazay bazay commented Nov 21, 2022

Implements feature request in issue: #224

Todo

  • Test feature from Rails app

Testing Notes

The following code snippets are taken from a personal Rails application using this feature branch as gem source.

config/initializers/data_migrate.rb

DataMigrate.configure do |config|
  config.data_migrations_path = "db/maintenance/"
  config.data_template_path = Rails.root.join("lib/maintenance/maintenance_migration_template.rb")
end

lib/maintenance/maintenance_migration_template.rb

# frozen_string_literal: true

class <%= migration_class_name %> < <%= migration_base_class_name %>
  def up
    # Call you migration task from here
    #
    # For example:
    # params = {
    #   key: "value",
    # }
    #
    # Maintenance::MyTask.perform_later(**params)
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

Running the generator:

$ bin/rails g data_migration MaintenanceTest                                                                                                                                                     add-data-migrate
      create  db/maintenance/20221121162436_maintenance_test.rb

where
db/maintenance/20221121162436_maintenance_test.rb
contains

class MaintenanceTest < ActiveRecord::Migration[7.0]
  def up
    # Call you migration task from here
    #
    # For example:
    # params = {
    #   key: "value",
    # }
    #
    # Maintenance::MyTask.perform_later(**params)
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

I can also confirm that removing config.data_template_path from config/initializers/data_migrate.rb will use the default template.

This will help developers debug issues related to an incorrect file path as early as possible.

def data_template_path=(value)
@data_template_path = value.tap do |path|
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performing validation on the setter seems a bit unorthodox, alternatively this can be performed from within the data migration generator i.e. https://github.com/ilyakatz/data-migrate/pull/232/files#diff-a4575045875fb9b1c551bd2d20696bd405d5c84ed26c2c773a895ce660c392ceR30

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the idea of doing validation here so that the error is raised on startup and devs will know right away that something is wrong. so looks good to me

@ilyakatz ilyakatz mentioned this pull request Jan 2, 2023
@ilyakatz ilyakatz closed this in #242 Jan 2, 2023
@ilyakatz
Copy link
Owner

ilyakatz commented Jan 2, 2023

Thank you, merged and deployed!

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.

3 participants