Skip to content

Commit

Permalink
Emulate dropping a unique column on SQLite 3.35.0+
Browse files Browse the repository at this point in the history
SQLite 3.35.0 added support for dropping columns, but not for
dropping unique columns, or columns that are part of an index
Fallback to the emulated support if the column being dropped is
part of an index (unique columns will have an index created for
them).
  • Loading branch information
jeremyevans committed Jun 14, 2024
1 parent 72f08cd commit 7b748b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Emulate dropping a unique column or a column that is part of an index on SQLite 3.35.0+ (jeremyevans) (#2176)

* Add :min_wait_timeout option to sharded_threaded connection pool and default to 0.1, fixes stalling observed on JRuby (jeremyevans)

* Support MERGE RETURNING on PostgreSQL 17+ (jeremyevans)
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/adapters/shared/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def alter_table_sql(table, op)
super
end
when :drop_column
if sqlite_version >= 33500
if sqlite_version >= 33500 && !indexes(table).any?{|_, h| h[:columns].include?(op[:name])}
super
else
ocp = lambda{|oc| oc.delete_if{|c| c.to_s == op[:name].to_s}}
Expand Down
35 changes: 35 additions & 0 deletions spec/adapters/sqlite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,41 @@
@db[:fk].select_order_map([:a, :b, :c, :d, :e]).must_equal [[100, 10, 211, 212, 213]]
@db.schema(:fk).map{|_,v| v[:generated]}.must_equal [false, false, true, true, true]
end if DB.sqlite_version >= 33100

it "should support dropping a unique column" do
@db.create_table!(:fk){Integer :a; Integer :b, :unique=>true}
@db[:fk].insert(:a=>1, :b=>2)
@db.alter_table(:fk){drop_column :b}
@db[:fk].all.must_equal [{:a=>1}]
end

it "should support dropping a column with scalar index" do
@db.create_table!(:fk){Integer :a; Integer :b, index: true}
@db[:fk].insert(:a=>1, :b=>2)
@db.alter_table(:fk){drop_column :b}
@db[:fk].all.must_equal [{:a=>1}]
end

it "should support dropping a column that is part of a composite index" do
@db.create_table!(:fk){Integer :a; Integer :b; index [:a, :b]}
@db[:fk].insert(:a=>1, :b=>2)
@db.alter_table(:fk){drop_column :b}
@db[:fk].all.must_equal [{:a=>1}]
end

it "should support dropping a column that is not part of an index" do
@db.create_table!(:fk){Integer :a, index: true; Integer :b}
@db[:fk].insert(:a=>1, :b=>2)
@db.alter_table(:fk){drop_column :b}
@db[:fk].all.must_equal [{:a=>1}]
end

it "should support dropping a column for a table without an index" do
@db.create_table!(:fk){Integer :a; Integer :b}
@db[:fk].insert(:a=>1, :b=>2)
@db.alter_table(:fk){drop_column :b}
@db[:fk].all.must_equal [{:a=>1}]
end
end

describe "SQLite temporary views" do
Expand Down

0 comments on commit 7b748b5

Please sign in to comment.