Permalink
Browse files

Add set_column_accept_null method to AlterTableGenerator

  • Loading branch information...
1 parent bedb708 commit ec2db8acc34c988b7ef5a32e48d95ceafb15e8fc @divoxx divoxx committed Sep 14, 2008
Showing with 21 additions and 0 deletions.
  1. +5 −0 lib/sequel_core/schema/generator.rb
  2. +2 −0 lib/sequel_core/schema/sql.rb
  3. +14 −0 spec/sequel_core/schema_spec.rb
View
5 lib/sequel_core/schema/generator.rb
@@ -260,6 +260,11 @@ def set_column_default(name, default)
def set_column_type(name, type)
@operations << {:op => :set_column_type, :name => name, :type => type}
end
+
+ # Modify a column's NOT NULL constraint.
+ def set_column_accept_null(name, accept_null)
+ @operations << {:op => :set_column_null, :name => name, :null => accept_null}
+ end
private
View
2 lib/sequel_core/schema/sql.rb
@@ -33,6 +33,8 @@ def alter_table_sql(table, op)
"ALTER COLUMN #{quoted_name} TYPE #{op[:type]}"
when :set_column_default
"ALTER COLUMN #{quoted_name} SET DEFAULT #{literal(op[:default])}"
+ when :set_column_null
+ "ALTER COLUMN #{quoted_name} #{op[:null] ? 'DROP' : 'SET'} NOT NULL"
when :add_index
return index_definition_sql(table, op)
when :drop_index
View
14 spec/sequel_core/schema_spec.rb
@@ -459,6 +459,20 @@
setup do
@db = SchemaDummyDatabase.new
end
+
+ specify "should allow adding not null constraint" do
+ @db.alter_table(:cats) do
+ set_column_accept_null :score, false
+ end
+ @db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
+ end
+
+ specify "should allow droping not null constraint" do
+ @db.alter_table(:cats) do
+ set_column_accept_null :score, true
+ end
+ @db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
+ end
specify "should support add_column" do
@db.alter_table(:cats) do

0 comments on commit ec2db8a

Please sign in to comment.