Skip to content

Commit

Permalink
Add truncate_tables method to SQLiteAdapter.
Browse files Browse the repository at this point in the history
Without this, the PostgreSQLAdapter truncate_tables method would be
called and go kerblooie cuz t'ain't no PostgreSQL database in play.
  • Loading branch information
chrismo committed Sep 26, 2013
1 parent df55cf7 commit 3cf6d82
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ vendor/
examples/db/*.db
examples/config/database.yml
db/config.yml
db/test.sqlite3
.vagrant
6 changes: 6 additions & 0 deletions db/sample.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ postgres:
host: 127.0.0.1
encoding: unicode
template: template0

sqlite3:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
4 changes: 4 additions & 0 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def delete_table(table_name)
end
alias truncate_table delete_table

def truncate_tables(tables)
tables.each { |t| truncate_table(t) }
end

private

# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
Expand Down
42 changes: 42 additions & 0 deletions spec/database_cleaner/active_record/truncation/sqlite3_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'
require 'active_record'
require 'support/active_record/sqlite3_setup'
require 'database_cleaner/active_record/truncation'

module ActiveRecord
module ConnectionAdapters
describe do
before(:all) { active_record_sqlite3_setup }

let(:adapter) { SQLite3Adapter }

let(:connection) do
active_record_sqlite3_connection
end

before(:each) do
connection.truncate_tables connection.tables
end

describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }

connection.truncate_table('users')
User.count.should eq 0
end

it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.delete_all

connection.truncate_table('users')

User.create.id.should eq 1
end
end

end
end
end

40 changes: 40 additions & 0 deletions spec/support/active_record/sqlite3_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'

module SQLite3Helper
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"

# ActiveRecord::Base.logger = Logger.new(STDERR)

def config
db_config['sqlite3']
end

def create_db
@encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{config.inspect}"
end
end

def establish_connection config = config
ActiveRecord::Base.establish_connection(config)
end

def active_record_sqlite3_setup
create_db
establish_connection
load_schema
end

def active_record_sqlite3_connection
ActiveRecord::Base.connection
end
end

RSpec.configure do |c|
c.include SQLite3Helper
end

0 comments on commit 3cf6d82

Please sign in to comment.