Skip to content

Commit

Permalink
Add set_column_accept_null method to AlterTableGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
divoxx committed Sep 14, 2008
1 parent bedb708 commit ec2db8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/sequel_core/schema/generator.rb
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/sequel_core/schema/sql.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/sequel_core/schema_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit ec2db8a

Please sign in to comment.