Sha256: b79f7cfcfd46067d85406b0152851ee4604d4e304cb304cc628ff821486c3db3

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

require 'rails/generators/active_record'

module ForeignKeyChecker
  module Generators
    class MigrationGenerator < ActiveRecord::Generators::Base
      desc "generates the necessary migrations to fix foreign_key problems"
      argument :name, type: :string, default: 'FixForeignKeys'

      source_root File.expand_path('templates', __dir__)

      def create_migrations
        @class_name = name.camelcase.tr(':', '')
        file_name = name.underscore.tr('/', '_')
        @done = Dir.glob(Rails.root.join('db', 'migrate', '*.rb')).map do |path|
          File.open(path).readlines.find { |line| line.include?('ActiveRecord::Migration') && line.include?(@class_name) }
        end.compact.size
        @checks = ForeignKeyChecker.check(zombies: false) unless @behavior == :revoke
        if @behavior == :revoke
          @file_suffix = "_v#{@done}" if @done > 1
          @class_suffix = "V#{@done}" if @done > 1
        else
          @file_suffix = "_v#{@done + 1}" if @done > 0
          @class_suffix = "V#{@done + 1}" if @done > 0
        end
        migration_template 'migrations/fix_foreign_keys.rb.erb', "db/migrate/#{file_name}#{@file_suffix}.rb"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
foreign_key_checker-0.5.1 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.5.0 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.4.1 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.4.0 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.3.0 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.2.2 lib/generators/foreign_key_checker/migration/migration_generator.rb
foreign_key_checker-0.2.1 lib/generators/foreign_key_checker/migration/migration_generator.rb