Sha256: 02425874fd964087e031903b0b74f5b9b9060c9d79e04b6c55e2ddbd2a79562c

Contents?: true

Size: 1.73 KB

Versions: 37

Compression:

Stored size: 1.73 KB

Contents

require 'rails/generators/active_record/migration'
module Qa::Local
  class TablesGenerator < Rails::Generators::Base
    source_root File.expand_path('../templates', __FILE__)
    include ActiveRecord::Generators::Migration

    def migrations # rubocop:disable Metrics/MethodLength
      if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
        message = "Use the mysql table based generator if you are using mysql 'rails generate qa:local:tables:mysql'"
        say_status("error", message, :red)
        return 0
      end
      generate "model qa/local_authority name:string:uniq"
      generate "model qa/local_authority_entry local_authority:references label:string uri:string:uniq"
      migration_file = Dir.entries(File.join(destination_root, 'db/migrate/'))
                          .reject { |name| !name.include?('create_qa_local_authority_entries') }.first
      migration_file = File.join('db/migrate', migration_file)
      gsub_file migration_file,
                /t\.references :local_authority.*/,
                't.references :local_authority, foreign_key: { to_table: :qa_local_authorities }, index: true'
      message = "Rails doesn't support functional indexes in migrations, so you'll have to add this manually:\n" \
                "CREATE INDEX \"index_qa_local_authority_entries_on_lower_label\" ON \"qa_local_authority_entries\" (local_authority_id, lower(label))\n" \
                "   OR on Sqlite: \n" \
                "CREATE INDEX \"index_qa_local_authority_entries_on_lower_label\" ON \"qa_local_authority_entries\" (local_authority_id, label collate nocase)\n"
      say_status("info", message, :yellow)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
qa-5.14.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.13.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.12.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.11.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.10.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.9.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.8.1 lib/generators/qa/local/tables/tables_generator.rb
qa-5.8.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.7.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.6.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.5.2 lib/generators/qa/local/tables/tables_generator.rb
qa-4.3.0 lib/generators/qa/local/tables/tables_generator.rb
qa-3.1.0 lib/generators/qa/local/tables/tables_generator.rb
qa-2.3.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.5.1 lib/generators/qa/local/tables/tables_generator.rb
qa-5.5.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.4.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.3.1 lib/generators/qa/local/tables/tables_generator.rb
qa-5.3.0 lib/generators/qa/local/tables/tables_generator.rb
qa-5.2.0 lib/generators/qa/local/tables/tables_generator.rb