Skip to content

Commit 7b748b5

Browse files
committed
Emulate dropping a unique column on SQLite 3.35.0+
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).
1 parent 72f08cd commit 7b748b5

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Emulate dropping a unique column or a column that is part of an index on SQLite 3.35.0+ (jeremyevans) (#2176)
4+
35
* Add :min_wait_timeout option to sharded_threaded connection pool and default to 0.1, fixes stalling observed on JRuby (jeremyevans)
46

57
* Support MERGE RETURNING on PostgreSQL 17+ (jeremyevans)

lib/sequel/adapters/shared/sqlite.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def alter_table_sql(table, op)
245245
super
246246
end
247247
when :drop_column
248-
if sqlite_version >= 33500
248+
if sqlite_version >= 33500 && !indexes(table).any?{|_, h| h[:columns].include?(op[:name])}
249249
super
250250
else
251251
ocp = lambda{|oc| oc.delete_if{|c| c.to_s == op[:name].to_s}}

spec/adapters/sqlite_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@
159159
@db[:fk].select_order_map([:a, :b, :c, :d, :e]).must_equal [[100, 10, 211, 212, 213]]
160160
@db.schema(:fk).map{|_,v| v[:generated]}.must_equal [false, false, true, true, true]
161161
end if DB.sqlite_version >= 33100
162+
163+
it "should support dropping a unique column" do
164+
@db.create_table!(:fk){Integer :a; Integer :b, :unique=>true}
165+
@db[:fk].insert(:a=>1, :b=>2)
166+
@db.alter_table(:fk){drop_column :b}
167+
@db[:fk].all.must_equal [{:a=>1}]
168+
end
169+
170+
it "should support dropping a column with scalar index" do
171+
@db.create_table!(:fk){Integer :a; Integer :b, index: true}
172+
@db[:fk].insert(:a=>1, :b=>2)
173+
@db.alter_table(:fk){drop_column :b}
174+
@db[:fk].all.must_equal [{:a=>1}]
175+
end
176+
177+
it "should support dropping a column that is part of a composite index" do
178+
@db.create_table!(:fk){Integer :a; Integer :b; index [:a, :b]}
179+
@db[:fk].insert(:a=>1, :b=>2)
180+
@db.alter_table(:fk){drop_column :b}
181+
@db[:fk].all.must_equal [{:a=>1}]
182+
end
183+
184+
it "should support dropping a column that is not part of an index" do
185+
@db.create_table!(:fk){Integer :a, index: true; Integer :b}
186+
@db[:fk].insert(:a=>1, :b=>2)
187+
@db.alter_table(:fk){drop_column :b}
188+
@db[:fk].all.must_equal [{:a=>1}]
189+
end
190+
191+
it "should support dropping a column for a table without an index" do
192+
@db.create_table!(:fk){Integer :a; Integer :b}
193+
@db[:fk].insert(:a=>1, :b=>2)
194+
@db.alter_table(:fk){drop_column :b}
195+
@db[:fk].all.must_equal [{:a=>1}]
196+
end
162197
end
163198

164199
describe "SQLite temporary views" do

0 commit comments

Comments
 (0)