Skip to content

Commit

Permalink
Wrap each seed file load in a transaction
Browse files Browse the repository at this point in the history
Prevents partially applied data from being stuck in the database.

I'm still not sure what changes we'd need for multi-db setups.
For now I've added a `transaction` delegation, so users can do:

```ruby
users.create name: "Something"

table_on_secondary_database.transaction do
  table_on_secondary_database.create name: "Something"
end
```
  • Loading branch information
kaspth committed Nov 1, 2023
1 parent 00f85b1 commit 5fbb19e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def load_onto(seeds)
end
end

def self.transaction(&block)
ActiveRecord::Base.transaction(&block)
end

def self.prepare(&block)
store_path.rmtree if ENV["OAKEN_RESET"]
Seeds.instance_eval(&block)
Expand Down
6 changes: 6 additions & 0 deletions lib/oaken/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def load_onto(seeds)
end
end

def transaction(&block)
super do
Oaken.transaction(&block)
end
end

def replay?
checksum == @computed_checksum
end
Expand Down
1 change: 1 addition & 0 deletions lib/oaken/stored/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Oaken::Stored::ActiveRecord < Struct.new(:type, :key)
def initialize(type, key = nil)
super(type, key || Oaken.inflector.tableize(type.name))
end
delegate :transaction, to: :type # For multi-db setups to help open a transaction on secondary connections.
delegate :find, :insert_all, to: :type

def create(reader = nil, **attributes)
Expand Down

0 comments on commit 5fbb19e

Please sign in to comment.