Also patches for the specs...
http://groups.google.com/group/sequel-talk/web/contraint_spec_patch.diff
Index: spec/schema_generator_spec.rb
===================================================================
--- spec/schema_generator_spec.rb (revision 929)
+++ spec/schema_generator_spec.rb (working copy)
@@ -82,6 +82,8 @@
add_index [:fff, :ggg]
drop_index :hhh
add_full_text_index :blah
+ add_constraint :con1, ':fred > 100'
+ drop_constraint :con2
end
end
@@ -94,7 +96,9 @@
{:op => :set_column_default, :name => :eee, :default => 1},
{:op => :add_index, :columns => [:fff, :ggg]},
{:op => :drop_index, :columns => [:hhh]},
- {:op => :add_index, :columns => [:blah], :full_text => true}
+ {:op => :add_index, :columns => [:blah], :full_text => true},
+ {:op => :add_constraint, :type => :check, :name
=> :con1, :check => [':fred > 100']},
+ {:op => :drop_constraint, :name => :con2}
]
end
-end
\ No newline at end of file
+end
Index: spec/schema_spec.rb
===================================================================
--- spec/schema_spec.rb (revision 929)
+++ spec/schema_spec.rb (working copy)
@@ -279,3 +279,30 @@
@db.sqls.should == ['DROP TABLE cats']
end
end
+
+context "DB#alter_table" do
+ setup do
+ @db = SchemaDummyDatabase.new
+ end
+
+ specify "should accept add constraint definitions" do
+ @db.alter_table(:cats) do
+ add_constraint :valid_score, 'score <= 100'
+ end
+ @db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT valid_score
CHECK (score <= 100)"]
+ @db.sqls.clear
+
+ @db.alter_table(:cats) do
+ add_constraint(:blah_blah) {:x > 0 && :y < 1}
+ end
+ @db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT blah_blah
CHECK (((x > 0) AND (y < 1)))"]
+ end
+
+ specify "should accept drop constraint definitions" do
+ @db.alter_table(:cats) do
+ drop_constraint :valid_score
+ end
+ @db.sqls.should == ["ALTER TABLE cats DROP CONSTRAINT
valid_score"]
+ end
+
+end
On Feb 23, 11:39 pm, Jim Morris <[EMAIL PROTECTED]> wrote:
> Ok I believe the attached diff will add contraints to alter_table, but
> I have not been able to build it to test.
>
> http://groups.google.com/group/sequel-talk/web/constraint_patch.diff
>
> Index: lib/sequel_core/schema/schema_sql.rb
> ===================================================================
> --- lib/sequel_core/schema/schema_sql.rb (revision 929)
> +++ lib/sequel_core/schema/schema_sql.rb (working copy)
> @@ -143,6 +143,10 @@
> index_definition_sql(table, op)
> when :drop_index
> "DROP INDEX #{default_index_name(table, op[:columns])}"
> + when :add_constraint
> + "ALTER TABLE #{table} ADD
> #{constraint_definition_sql(op)}"
> + when :drop_constraint
> + "ALTER TABLE #{table} DROP CONSTRAINT
> #{literal(op[:name])}"
> else
> raise Error, "Unsupported ALTER TABLE operation"
> end
> Index: lib/sequel_core/schema/schema_generator.rb
> ===================================================================
> --- lib/sequel_core/schema/schema_generator.rb (revision 929)
> +++ lib/sequel_core/schema/schema_generator.rb (working copy)
> @@ -147,7 +147,24 @@
> :columns => columns \
> }
> end
> +
> + def add_constraint(name, *args, &block)
> + @operations << { \
> + :op => :add_constraint, \
> + :name => name, \
> + :type => :check, \
> + :check => block || args \
> + }
> end
> +
> + def drop_constraint(name)
> + @operations << { \
> + :op => :drop_constraint, \
> + :name => name \
> + }
> end
> +
> end
> + end
> +end
>
> On Feb 23, 7:08 pm, Jim Morris <[EMAIL PROTECTED]> wrote:
>
> > Ok, but what about the alter_table for constraints?
>
> > I'll take a look and see if I can submit a patch to add this to
> > alter_table, it seems adding (and removing) constraints in an
> > alter_table would be pretty handy.
>
> > I could use it right now as I am doing migrations and having to add a
> > check manually with execute.
>
> > On Feb 23, 12:49 pm, Aman Gupta <[EMAIL PROTECTED]> wrote:
>
> > > On Feb 22, 12:25 pm, Jim Morris <[EMAIL PROTECTED]> wrote:
>
> > > > This is excellent!
>
> > > > However is there a way to have this work in an alter_table block? or
> > > > is there another way to add constraints to an existing table?
>
> > > > Also is there any formal documentation for check? or a wiki page?
>
> > > Nothing yet, there should be some rdocs.. If someone more familiar
> > > with the subject wants to write up a wiki page on the topic, I'll get
> > > it up on the site.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---