From ec2db8acc34c988b7ef5a32e48d95ceafb15e8fc Mon Sep 17 00:00:00 2001 From: Rodrigo Kochenburger Date: Sun, 14 Sep 2008 15:03:56 -0300 Subject: [PATCH] Add set_column_accept_null method to AlterTableGenerator --- lib/sequel_core/schema/generator.rb | 5 +++++ lib/sequel_core/schema/sql.rb | 2 ++ spec/sequel_core/schema_spec.rb | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/lib/sequel_core/schema/generator.rb b/lib/sequel_core/schema/generator.rb index b1d74f091b..21ebd95a3c 100644 --- a/lib/sequel_core/schema/generator.rb +++ b/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 diff --git a/lib/sequel_core/schema/sql.rb b/lib/sequel_core/schema/sql.rb index e19be0925d..ebcdb598fc 100644 --- a/lib/sequel_core/schema/sql.rb +++ b/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 diff --git a/spec/sequel_core/schema_spec.rb b/spec/sequel_core/schema_spec.rb index 6f2c763d66..e34d94fa57 100644 --- a/spec/sequel_core/schema_spec.rb +++ b/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