Skip to content

Commit

Permalink
renamed destroy_all to truncate. (#134)
Browse files Browse the repository at this point in the history
* renamed destroy_all to truncate. fixes #110

* make truncate a class method since it's not chainable like other instance methods
  • Loading branch information
jwoertink authored and paulcsmith committed Jul 3, 2019
1 parent b6cb187 commit 428158f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spec/query_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,13 @@ describe Avram::Query do
end
end
end

describe "truncate" do
it "truncates the table" do
10.times { UserBox.create }
UserQuery.new.select_count.should eq 10
UserQuery.truncate
UserQuery.new.select_count.should eq 0
end
end
end
3 changes: 2 additions & 1 deletion src/avram/query.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "./queryable"

abstract class Avram::Query
def destroy_all
# runs a SQL `TRUNCATE` on the current table
def self.truncate
Avram::Repo.run do |db|
db.exec "TRUNCATE TABLE #{@@table_name}"
end
Expand Down
1 change: 1 addition & 0 deletions src/avram/repo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Avram::Repo
transactions[Fiber.current.object_id]?
end

# runs a SQL `TRUNCATE` on all tables in the database
def self.truncate
DatabaseCleaner.new.truncate
end
Expand Down

0 comments on commit 428158f

Please sign in to comment.