Class: Qa::Local::Tables::MysqlGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Qa::Local::Tables::MysqlGenerator
- Includes:
- ActiveRecord::Generators::Migration
- Defined in:
- lib/generators/qa/local/tables/mysql/mysql_generator.rb
Instance Method Summary collapse
-
#migrations ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Details
#migrations ⇒ Object
rubocop:disable Metrics/MethodLength
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/generators/qa/local/tables/mysql/mysql_generator.rb', line 8 def migrations # rubocop:disable Metrics/MethodLength unless defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) = "Use the table based generator if you are not using mysql 'rails generate qa:local:tables'" say_status("error", , :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 lower_label:string" 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' insert_into_file migration_file, after: "add_index :qa_local_authority_entries, :uri, unique: true" do "\n add_foreign_key :qa_local_authority_entries, :qa_local_authorities, column: :local_authority_id\n" \ " if ActiveRecord::Base.connection.instance_of? ActiveRecord::ConnectionAdapters::Mysql2Adapter\n" \ " remove_column :qa_local_authority_entries, :lower_label, :string\n" \ " execute(\"alter table qa_local_authority_entries add lower_label varchar(256) GENERATED ALWAYS AS (lower(label)) VIRTUAL\")\n" \ " end\n" \ " add_index :qa_local_authority_entries, [:lower_label, :local_authority_id], name: 'index_qa_local_authority_entries_on_lower_label_and_authority'" end = "Rails doesn't support functional indexes in migrations, so we inserted an exec into the migration since you are specifying you are running with mysql.\n" \ "The excute will not be run for another database. The commands being run are: "\ " ALTER TABLE qa_local_authority_entries ADD lower_label VARCHAR(256) GENERATED ALWAYS AS (lower(label)) VIRTUAL" \ " CREATE INDEX index_qa_local_authority_entries_on_lower_label_and_authority ON qa_local_authority_entries (local_authority_id, lower_label)" say_status("info", , :yellow) end |