diff --git a/spec/avram/schema_enforcer/ensure_matching_columns_spec.cr b/spec/avram/schema_enforcer/ensure_matching_columns_spec.cr index 46f6f4eb8..f5b5c4c57 100644 --- a/spec/avram/schema_enforcer/ensure_matching_columns_spec.cr +++ b/spec/avram/schema_enforcer/ensure_matching_columns_spec.cr @@ -6,6 +6,18 @@ private class ModelWithMissingButSimilarlyNamedColumn < BaseModel end end +private class ModelWithMissingColumn < BaseModel + table :users do + column first_name : String + end +end + +private class ModelWithMissingEnumColumn < BaseModel + table :users do + column gender : Enum + end +end + private class ModelWithOptionalAttributeOnRequiredColumn < BaseModel table :users do column name : String? @@ -25,6 +37,16 @@ describe Avram::SchemaEnforcer::EnsureMatchingColumns do expect_schema_mismatch "wants to use the column 'mickname' but it does not exist. Did you mean 'nickname'?" do ModelWithMissingButSimilarlyNamedColumn.ensure_correct_column_mappings! end + + # Partial message assertion for brevity + expect_schema_mismatch "wants to use the column 'first_name' but it does not exist." do + ModelWithMissingColumn.ensure_correct_column_mappings! + end + + # Partial message assertion for brevity + expect_schema_mismatch "add gender : Int32" do + ModelWithMissingEnumColumn.ensure_correct_column_mappings! + end end it "raises on nilable column with required columns" do diff --git a/src/avram/schema_enforcer/ensure_matching_columns.cr b/src/avram/schema_enforcer/ensure_matching_columns.cr index 6f22f45c7..0b54a0a54 100644 --- a/src/avram/schema_enforcer/ensure_matching_columns.cr +++ b/src/avram/schema_enforcer/ensure_matching_columns.cr @@ -49,6 +49,8 @@ class Avram::SchemaEnforcer::EnsureMatchingColumns < Avram::SchemaEnforcer::Vali if best_match message += " Did you mean '#{best_match.colorize.yellow.bold}'?\n\n" else + # Enum column are mapped to Int32 in the database layer + missing_attribute_type_hint = "#{missing_attribute[:name]} : #{missing_attribute[:type].sub("Enum", "Int32")}" message += <<-TEXT @@ -62,10 +64,10 @@ class Avram::SchemaEnforcer::EnsureMatchingColumns < Avram::SchemaEnforcer::Vali alter :#{table_name} do #{"# Add the column:".colorize.dim} - add #{missing_attribute[:name]} : #{missing_attribute[:type]} + add #{missing_attribute_type_hint} #{"# Or if this is a column for a belongs_to relationship:".colorize.dim} - add_belongs_to #{missing_attribute[:name]} : #{missing_attribute[:type]} + add_belongs_to #{missing_attribute_type_hint} end Or, you can skip schema checks for this model: