lib/strong_migrations/migration.rb in strong_migrations-0.1.6 vs lib/strong_migrations/migration.rb in strong_migrations-0.1.7

- old
+ new

@@ -30,30 +30,51 @@ columns = args[1] if columns.is_a?(Array) && columns.size > 3 raise_error :add_index_columns end options = args[2] - if %w(PostgreSQL PostGIS).include?(connection.adapter_name) && !(options && options[:algorithm] == :concurrently) && !@new_tables.to_a.include?(args[0].to_s) + if postgresql? && !(options && options[:algorithm] == :concurrently) && !@new_tables.to_a.include?(args[0].to_s) raise_error :add_index end when :add_column type = args[2] options = args[3] raise_error :add_column_default if options && !options[:default].nil? raise_error :add_column_json if type.to_s == "json" when :change_column raise_error :change_column when :create_table - (@new_tables ||= []) << args[0].to_s + options = args[1] + raise_error :create_table if options[:force] + when :add_reference + options = args[2] || {} + index_value = options.fetch(:index, ActiveRecord::VERSION::MAJOR >= 5 ? true : false) + if postgresql? && index_value + raise_error :add_reference + end end end - super + if method == :create_table + (@new_tables ||= []) << args[0].to_s + end + + result = super + + if StrongMigrations.auto_analyze && postgresql? && method == :add_index + connection.execute "ANALYZE VERBOSE #{connection.quote_table_name(args[0])}" + end + + result end private + def postgresql? + %w(PostgreSQL PostGIS).include?(connection.adapter_name) + end + def raise_error(message_key) message = case message_key when :add_column_default "Adding a column with a non-null default requires @@ -109,10 +130,18 @@ 2. Write to both tables 3. Backfill data from the old table to new table 4. Move reads from the old table to the new table 5. Stop writing to the old table 6. Drop the old table" + when :add_reference +"Adding a non-concurrent index locks the table. Instead, use: + + def change + add_reference :users, :reference, index: false + commit_db_transaction + add_index :users, :reference_id, algorithm: :concurrently + end" when :add_index "Adding a non-concurrent index locks the table. Instead, use: def change commit_db_transaction @@ -124,9 +153,13 @@ If you're sure this is what you want, wrap it in a safety_assured { ... } block." when :change_table "The strong_migrations gem does not support inspecting what happens inside a change_table block, so cannot help you here. Please make really sure that what you're doing is safe before proceding, then wrap it in a safety_assured { ... } block." + when :create_table +"The force option will destroy existing tables. +If this is intended, drop the existing table first. +Otherwise, remove the option." end wait_message = ' __ __ _____ _______ _ \ \ / /\ |_ _|__ __| |