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

Rails42 #1

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/strong_migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

module StrongMigrations
class << self
attr_accessor :auto_analyze, :start_after, :checks, :error_messages
attr_accessor :auto_analyze, :start_after, :checks, :error_messages, :ignored
end
self.auto_analyze = false
self.start_after = 0
self.checks = []
self.ignored = []
self.error_messages = {
add_column_default:
"Adding a column with a non-null default causes the entire table to be rewritten.
Expand All @@ -39,6 +40,15 @@ def change
end
end",

add_column_default_null:
"Adding a column with a null default causes the entire table to be rewritten.
Instead, add the column without a default value.
class %{migration_name} < ActiveRecord::Migration%{migration_suffix}
def change
%{command}
end
end",

add_column_json:
"There's no equality operator for the json column type, which
causes issues for SELECT DISTINCT queries. Use jsonb instead.",
Expand Down
11 changes: 9 additions & 2 deletions lib/strong_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def migrate(direction)
end

def method_missing(method, *args, &block)
unless @safe || ENV["SAFETY_ASSURED"] || is_a?(ActiveRecord::Schema) || @direction == :down || version_safe?
unless @safe || ENV["SAFETY_ASSURED"] || is_a?(ActiveRecord::Schema) || @direction == :down || version_safe? || name_safe?
ar5 = ActiveRecord::VERSION::MAJOR >= 5

case method
Expand Down Expand Up @@ -64,7 +64,10 @@ def method_missing(method, *args, &block)
options ||= {}
default = options[:default]

if !default.nil? && !(postgresql? && postgresql_version >= 110000)
if options.key?(:default) && default.nil? && postgresql?
raise_error :add_column_default_null,
command: command_str("add_column", [table, column, type, options.except(:default)])
elsif default && postgresql?
raise_error :add_column_default,
add_command: command_str("add_column", [table, column, type, options.except(:default)]),
change_command: command_str("change_column_default", [table, column, default]),
Expand Down Expand Up @@ -149,6 +152,10 @@ def version_safe?
version && version <= StrongMigrations.start_after
end

def name_safe?
version.nil? && name && StrongMigrations.ignored.include?(name)
end

def raise_error(message_key, header: nil, **vars)
message = StrongMigrations.error_messages[message_key] || "Missing message"

Expand Down
10 changes: 10 additions & 0 deletions test/strong_migrations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def change
end
end

class AddColumnDefaultNull < TestMigration
def change
add_column :users, :nice, :boolean, default: nil
end
end

class AddColumnDefaultSafe < TestMigration
def change
add_column :users, :nice, :boolean
Expand Down Expand Up @@ -251,6 +257,10 @@ def test_add_column_default
assert_unsafe AddColumnDefault
end

def test_add_column_default_null
assert_unsafe AddColumnDefaultNull
end

def test_add_column_default_safe
assert_safe AddColumnDefaultSafe
end
Expand Down