Permalink
Browse files

Support drop column operations inside a transaction on sqlite

  • Loading branch information...
jeremyevans committed Dec 3, 2008
1 parent 9239416 commit 2d4084ebee3293fe799ed125cb5f46fa4ddf94d4
Showing with 17 additions and 4 deletions.
  1. +2 −0 CHANGELOG
  2. +8 −4 lib/sequel_core/adapters/shared/sqlite.rb
  3. +7 −0 spec/adapters/sqlite_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Support drop column operations inside a transaction on sqlite (jeremyevans)
+
* Support literal strings with placeholders and subselects in prepared statements (jeremyevans)
* Have the connection pool remove disconnected connections when the adapter supports it (jeremyevans)
@@ -6,6 +6,12 @@ module DatabaseMethods
TABLES_FILTER = "type = 'table' AND NOT name = 'sqlite_sequence'"
TEMP_STORE = {'0' => :default, '1' => :file, '2' => :memory}.freeze
+ # Run all alter_table commands in a transaction. This is technically only
+ # needed for drop column.
+ def alter_table(name, generator=nil, &block)
+ transaction{super}
+ end
+
# SQLite supports limited table modification. You can add a column
# or an index. Dropping columns is supported by copying the table into
# a temporary table, dropping the table, and creating a new table without
@@ -18,14 +24,12 @@ def alter_table_sql(table, op)
index_definition_sql(table, op)
when :drop_column
columns_str = (schema_parse_table(table, {}).map{|c| c[0]} - Array(op[:name])).join(",")
- ["BEGIN TRANSACTION",
- "CREATE TEMPORARY TABLE #{table}_backup(#{columns_str})",
+ ["CREATE TEMPORARY TABLE #{table}_backup(#{columns_str})",
"INSERT INTO #{table}_backup SELECT #{columns_str} FROM #{table}",
"DROP TABLE #{table}",
"CREATE TABLE #{table}(#{columns_str})",
"INSERT INTO #{table} SELECT #{columns_str} FROM #{table}_backup",
- "DROP TABLE #{table}_backup",
- "COMMIT"]
+ "DROP TABLE #{table}_backup"]
else
raise Error, "Unsupported ALTER TABLE operation"
end
@@ -415,6 +415,13 @@ def @db.ret_commit
@db[:test2].first.should == {:name => 'mmm'}
end
+ specify "should support drop_column operations in a transaction" do
+ @db.transaction{@db.drop_column :test2, :value}
+ @db[:test2].columns.should == [:name]
+ @db[:test2] << {:name => 'mmm'}
+ @db[:test2].first.should == {:name => 'mmm'}
+ end
+
specify "should not support rename_column operations" do
proc {@db.rename_column :test2, :value, :zyx}.should raise_error(Sequel::Error)
end

0 comments on commit 2d4084e

Please sign in to comment.