Permalink
Browse files

Merge pull request #72 from nulldb/support_limit_and_null

Support limit and null
  • Loading branch information...
2 parents 9ab7570 + 661bd3d commit 722212b2c935d564701a3545824f54e283620f40 @mnoack mnoack committed on GitHub Dec 27, 2016
Showing with 37 additions and 10 deletions.
  1. +23 −8 lib/active_record/connection_adapters/nulldb_adapter/core.rb
  2. +14 −2 spec/nulldb_spec.rb
@@ -280,20 +280,35 @@ def new_column_arguments(col_def)
def args_with_optional_cast_type(col_def)
default_column_arguments(col_def).tap do |args|
if respond_to? :fetch_type_metadata
- args.insert(2, fetch_type_metadata(col_def.type))
+ args.insert(2, meta_with_limit!(fetch_type_metadata(col_def.type), col_def))
elsif initialize_column_with_cast_type?
- args.insert(2, lookup_cast_type(col_def.type))
+ args.insert(2, meta_with_limit!(lookup_cast_type(col_def.type), col_def))
+ else
+ args[2] = args[2].to_s + "(#{col_def.limit})" if col_def.limit
end
end
end
+ def meta_with_limit!(meta, col_def)
+ meta.instance_variable_set('@limit', col_def.limit)
+ meta
+ end
+
def default_column_arguments(col_def)
- [
- col_def.name.to_s,
- col_def.default,
- col_def.type,
- col_def.null
- ]
+ if ActiveRecord::VERSION::MAJOR == 5
+ [
+ col_def.name.to_s,
+ col_def.default,
+ col_def.null.nil? || col_def.null # cast [false, nil, true] => [false, true, true], other adapters default to null=true
+ ]
+ else
+ [
+ col_def.name.to_s,
+ col_def.default,
+ col_def.type,
+ col_def.null.nil? || col_def.null # cast [false, nil, true] => [false, true, true], other adapters default to null=true
+ ]
+ end
end
def initialize_column_with_cast_type?
View
@@ -71,7 +71,7 @@ class TablelessModel < ActiveRecord::Base
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table(:employees) do |t|
- t.string :name
+ t.string :name, null: false, limit: 50
t.date :hire_date
t.integer :employee_number
t.decimal :salary
@@ -113,6 +113,18 @@ class TablelessModel < ActiveRecord::Base
should_have_column(Employee, :salary, :decimal)
end
+ it 'should have limit on name' do
+ expect(Employee.columns_hash['name'].limit).to eq 50
+ end
+
+ it "should return true on nullable field" do
+ expect(Employee.columns_hash['salary'].null).to be true
+ end
+
+ it "should return false on non-nullable field" do
+ expect(Employee.columns_hash['name'].null).to be false
+ end
+
it "should return the appropriate primary key" do
expect( ActiveRecord::Base.connection.primary_key('employees') ).to eq 'id'
end
@@ -249,7 +261,7 @@ def should_not_contain_statement(cxn, entry_point)
def should_have_column(klass, col_name, col_type)
col = klass.columns_hash[col_name.to_s]
- expect(col.sql_type.to_sym).to eq col_type
+ expect(col.sql_type.to_s.gsub(/\([0-9]+\)/, "").to_sym).to eq col_type
end

0 comments on commit 722212b

Please sign in to comment.