Skip to content

Commit

Permalink
Add :ignore_index_errors option to Database#create_table and :ignore_…
Browse files Browse the repository at this point in the history
…errors option to Database#add_index

The schema_dumper extension is going to be using these options to
make it more likely that the migrations it generates will be
loadable.  Since indexes generally have no effect on behavior
(except for unique indexes), the inability to load one shouldn't
be a reason to cancel the migration.

I'm not happy with this way of doing things, but I can't really
think of a better idea that will allow dumping of PostgreSQL tables
with indexes on text columns and attempting to restore them on
MySQL.
  • Loading branch information
jeremyevans committed May 23, 2009
1 parent 8cf6702 commit 7c384cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== HEAD

* Add :ignore_index_errors option to Database#create_table and :ignore_errors option to Database#add_index (jeremyevans)

* Make graphing a complex dataset work correctly (jeremyevans)

* Fix MySQL command out of sync errors, disconnect from database if they occur (jeremyevans)
Expand Down
23 changes: 20 additions & 3 deletions lib/sequel/database/schema_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ def add_column(table, *args)
# DB.add_index :posts, :title
# DB.add_index :posts, [:author, :title], :unique => true
#
#
# Options:
# * :ignore_errors - Ignore any DatabaseErrors that are raised
#
# See alter_table.
def add_index(table, *args)
alter_table(table) {add_index(*args)}
def add_index(table, columns, options={})
e = options[:ignore_errors]
begin
alter_table(table){add_index(columns, options)}
rescue DatabaseError
raise unless e
end
end

# Alters the given table with the specified block. Example:
Expand Down Expand Up @@ -55,6 +64,7 @@ def alter_table(name, generator=nil, &block)
#
# Options:
# * :temp - Create the table as a temporary table.
# * :ignore_index_errors - Ignore any errors when creating indexes.
#
# See Schema::Generator.
def create_table(name, options={}, &block)
Expand Down Expand Up @@ -180,7 +190,14 @@ def create_table_from_generator(name, generator, options)

# Execute the create index statements using the generator.
def create_table_indexes_from_generator(name, generator, options)
index_sql_list(name, generator.indexes).each{|sql| execute_ddl(sql)}
e = options[:ignore_index_errors]
index_sql_list(name, generator.indexes).each do |sql|
begin
execute_ddl(sql)
rescue DatabaseError
raise unless e
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/core/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@
}.should raise_error(Sequel::Error)
end

specify "should ignore errors if the database raises an error on an index creation statement and the :ignore_index_errors option is used" do
@db.meta_def(:execute_ddl){|*a| raise Sequel::DatabaseError if /blah/.match(a.first); super(*a)}
lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.should raise_error(Sequel::DatabaseError)
@db.sqls.should == ['CREATE TABLE cats (id integer)']
@db.sqls.clear
lambda{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}.should_not raise_error(Sequel::DatabaseError)
@db.sqls.should == ['CREATE TABLE cats (id integer)', 'CREATE INDEX cats_id_index ON cats (id)']
end

specify "should accept multiple index definitions" do
@db.create_table(:cats) do
integer :id
Expand Down Expand Up @@ -642,6 +651,13 @@
@db.sqls.should == ["CREATE INDEX cats_name_index ON cats (name)"]
end

specify "should ignore errors if the database raises an error on an add_index call and the :ignore_errors option is used" do
@db.meta_def(:execute_ddl){|*a| raise Sequel::DatabaseError}
lambda{@db.add_index(:cats, :id)}.should raise_error(Sequel::DatabaseError)
lambda{@db.add_index(:cats, :id, :ignore_errors=>true)}.should_not raise_error(Sequel::DatabaseError)
@db.sqls.should == nil
end

specify "should support add_primary_key" do
@db.alter_table(:cats) do
add_primary_key :id
Expand Down

0 comments on commit 7c384cb

Please sign in to comment.