-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces truncating the database before every spec. In avram we're seeing specs run ~20x faster but Lucky apps might expect to see speed ups of around 4-5x.
- Loading branch information
1 parent
9b184d4
commit 36e2f87
Showing
14 changed files
with
157 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Avram::SpecHelper | ||
TRUNCATE = "truncate" | ||
|
||
macro use_transactional_specs(*databases) | ||
Spec.around_each do |spec| | ||
Avram::SpecHelper.wrap_spec_in_transaction(spec, {{ databases.splat }}) | ||
end | ||
end | ||
|
||
def self.wrap_spec_in_transaction(spec : Spec::Example::Procsy, *databases) | ||
if use_truncation?(spec) | ||
spec.run | ||
databases.each(&.truncate) | ||
return | ||
end | ||
|
||
tracked_transactions = [] of DB::Transaction | ||
|
||
databases.each do |database| | ||
database.lock_id = Fiber.current.object_id | ||
database.connections.values.each do |conn| | ||
tracked_transactions << conn.begin_transaction.tap(&._avram_joinable=(false)) | ||
end | ||
|
||
database.setup_connection do |conn| | ||
tracked_transactions << conn.begin_transaction.tap(&._avram_joinable=(false)) | ||
end | ||
end | ||
|
||
spec.run | ||
|
||
tracked_transactions.each do |transaction| | ||
next if transaction.closed? || transaction.connection.closed? | ||
|
||
transaction.rollback | ||
transaction.connection.release | ||
end | ||
tracked_transactions.clear | ||
databases.each do |database| | ||
database.connections.clear | ||
database.setup_connection { } | ||
end | ||
end | ||
|
||
private def self.use_truncation?(spec : Spec::Example::Procsy) : Bool | ||
current = spec.example | ||
while !current.is_a?(Spec::RootContext) | ||
temp = current.as(Spec::Item) | ||
return true if temp.tags.try(&.includes?(TRUNCATE)) | ||
current = temp.parent | ||
end | ||
|
||
false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module DB | ||
abstract class Connection | ||
# :nodoc: | ||
getter _avram_stack = [] of DB::Transaction | ||
|
||
# :nodoc: | ||
def _avram_in_transaction? : Bool | ||
!_avram_stack.empty? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module DB | ||
abstract class Transaction | ||
# :nodoc: | ||
property? _avram_joinable = true | ||
end | ||
|
||
class TopLevelTransaction < Transaction | ||
def initialize(@connection : Connection) | ||
@nested_transaction = false | ||
@connection.perform_begin_transaction | ||
@connection._avram_stack.push(self) | ||
end | ||
|
||
protected def do_close | ||
@connection.release_from_transaction | ||
@connection._avram_stack.pop | ||
end | ||
end | ||
|
||
class SavePointTransaction < Transaction | ||
def initialize(@parent : Transaction, @savepoint_name : String) | ||
@nested_transaction = false | ||
@connection = @parent.connection | ||
@connection.perform_create_savepoint(@savepoint_name) | ||
@connection._avram_stack.push(self) | ||
end | ||
|
||
protected def do_close | ||
@parent.release_from_nested_transaction | ||
@connection._avram_stack.pop | ||
end | ||
end | ||
end |