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

Improve alter migrations error msg #524

Merged
merged 5 commits into from
Nov 12, 2020
Merged
Changes from 2 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
23 changes: 18 additions & 5 deletions src/avram/migrator/alter_table_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,25 @@ class Avram::Migrator::AlterTableStatement
@fill_existing_with_statements << "ALTER TABLE #{@table_name} ALTER COLUMN #{column} SET NOT NULL;" unless nilable
end

{% symbol_expected_message = "%s expected a symbol like ':user', instead got: '%s'" %}
macro symbol_expected_error(action, name)

{% raise <<-ERROR

#{action} expected a symbol like ':user', instead got: '#{name}'.

in: #{name.filename}:#{name.line_number}:#{name.column_number}

Try replacing...

▸ '#{name}' with '#{name.var}'
ERROR
%}
end

macro rename(old_name, new_name)
{% for name in {old_name, new_name} %}
{% unless name.is_a?(SymbolLiteral) %}
{% raise symbol_expected_message % {"rename", name} %}
symbol_expected_error("rename", {{ name }})
{% end %}
{% end %}
renamed_rows << "RENAME COLUMN #{{{old_name}}} TO #{{{new_name}}}"
Expand All @@ -189,22 +202,22 @@ class Avram::Migrator::AlterTableStatement
macro rename_belongs_to(old_association_name, new_association_name)
{% for association_name in {old_association_name, new_association_name} %}
{% unless association_name.is_a?(SymbolLiteral) %}
{% raise symbol_expected_message % {"rename_belongs_to", association_name} %}
symbol_expected_error("rename_belongs_to", {{ name }})
{% end %}
{% end %}
rename {{old_association_name}}_id, {{new_association_name}}_id
end

macro remove(name)
{% unless name.is_a?(SymbolLiteral) %}
{% raise symbol_expected_message % {"remove", name} %}
symbol_expected_error("remove", {{ name }})
{% end %}
dropped_rows << " DROP #{{{name}}}"
end

macro remove_belongs_to(association_name)
{% unless association_name.is_a?(SymbolLiteral) %}
{% raise symbol_expected_message % {"remove_belongs_to", association_name} %}
symbol_expected_error("remove_belongs_to", {{ name }})
{% end %}
remove {{ association_name }}_id
end
Expand Down