-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequel adapter #152
Closed
Closed
Sequel adapter #152
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fb1315
Initial Sequel adapter
appleton 882e854
Failing shared specs for sequel adapter
appleton cb97407
Implement shared behaviour for Adapters::Sequel
appleton 096c7e4
Separate shared SQL adapter behaviour specs
appleton 8be167d
Align everything, autoload SequelTransition
appleton fb01c84
Use reflection to get a more accurate foreign key
appleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require_relative "../exceptions" | ||
|
||
module Statesman | ||
module Adapters | ||
class Sequel | ||
attr_reader :transition_class, :parent_model, :observer | ||
|
||
::Sequel.extension(:inflector) | ||
|
||
def initialize(transition_class, parent_model, observer) | ||
@transition_class = transition_class | ||
@parent_model = parent_model | ||
@observer = observer | ||
end | ||
|
||
def create(from, to, metadata = {}) | ||
create_transition(from.to_s, to.to_s, metadata) | ||
ensure | ||
@last_transition = nil | ||
end | ||
|
||
def history | ||
history_dataset.all | ||
end | ||
|
||
def last | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method now accepts a def last(force_reload: false)
@last_transition = nil if force_reload
@last_transition ||= history_dataset.last
end this code can be refactored to support Ruby < 2.0 |
||
@last_transition ||= history_dataset.last | ||
end | ||
|
||
private | ||
|
||
def history_dataset | ||
transitions_for_parent.order(:sort_key) | ||
end | ||
|
||
def transitions_for_parent | ||
@parent_model.send("#{transition_table_name}_dataset") | ||
end | ||
|
||
def next_sort_key | ||
(last && last.sort_key + 10) || 0 | ||
end | ||
|
||
def create_transition(from, to, metadata) | ||
transition = transition_class.new( | ||
to_state: to, | ||
sort_key: next_sort_key, | ||
metadata: metadata, | ||
parent_model_foreign_key => @parent_model.pk | ||
) | ||
|
||
parent_model_class.db.transaction do | ||
@observer.execute(:before, from, to, transition) | ||
transition.save | ||
@last_transition = transition | ||
@observer.execute(:after, from, to, transition) | ||
end | ||
|
||
@observer.execute(:after_commit, from, to, transition) | ||
|
||
transition | ||
end | ||
|
||
def transition_table_name | ||
@transition_table_name ||= @transition_class.table_name | ||
end | ||
|
||
def parent_model_class | ||
@parent_model_class ||= @parent_model.class | ||
end | ||
|
||
def parent_association_name | ||
parent_model_class.table_name.to_s.singularize.to_sym | ||
end | ||
|
||
def parent_model_foreign_key | ||
@transition_class.association_reflection(parent_association_name)[:key] | ||
end | ||
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,12 @@ | ||
require "json" | ||
require "sequel" | ||
|
||
module Statesman | ||
module Adapters | ||
module SequelTransition | ||
def self.included(base) | ||
base.send(:plugin, :serialization, :json, :metadata) | ||
end | ||
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
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,18 @@ | ||
require "spec_helper" | ||
require "statesman/adapters/adapter_behaviour" | ||
require "statesman/adapters/sql_adapter_behaviour" | ||
|
||
describe Statesman::Adapters::Sequel, sequel: true do | ||
before do | ||
MySequelModel.dataset = MySequelModel.dataset | ||
MySequelModelTransition.dataset = MySequelModelTransition.dataset | ||
end | ||
|
||
let(:observer) { double(Statesman::Machine, execute: nil) } | ||
let(:model) { MySequelModel.create(current_state: :pending) } | ||
|
||
it_behaves_like "an adapter", described_class, MySequelModelTransition | ||
it_behaves_like "a SQL adapter", described_class, MySequelModelTransition do | ||
let(:association_name) { :my_sequel_model_transitions_dataset } | ||
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,25 @@ | ||
require "spec_helper" | ||
|
||
shared_examples_for "a SQL adapter" do |adapter_class, transition_class| | ||
describe "#last" do | ||
let(:adapter) { adapter_class.new(transition_class, model, observer) } | ||
|
||
before { adapter.create(:x, :y) } | ||
|
||
context "with a previously looked up transition" do | ||
before { adapter.last } | ||
|
||
it "caches the transition" do | ||
expect_any_instance_of(model.class).to receive(association_name).never | ||
adapter.last | ||
end | ||
|
||
context "and a new transition" do | ||
before { adapter.create(:y, :z, []) } | ||
it "retrieves the new transition from the database" do | ||
expect(adapter.last.to_state).to eq("z") | ||
end | ||
end | ||
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,29 @@ | ||
require "json" | ||
require "sequel" | ||
require_relative "./sequel_migrations" | ||
require_relative "../../lib/statesman/adapters/sequel_transition" | ||
|
||
class SequelStateMachine | ||
include Statesman::Machine | ||
|
||
state :initial, initial: true | ||
state :succeeded | ||
state :failed | ||
|
||
transition from: :initial, to: [:succeeded, :failed] | ||
transition from: :failed, to: :initial | ||
end | ||
|
||
class MySequelModel < Sequel::Model | ||
one_to_many :my_sequel_model_transitions | ||
|
||
def state_machine | ||
@state_machine ||= SequelStateMachine.new( | ||
self, transition_class: MySequelModelTransition) | ||
end | ||
end | ||
|
||
class MySequelModelTransition < Sequel::Model | ||
include Statesman::Adapters::SequelTransition | ||
many_to_one :my_sequel_model | ||
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,41 @@ | ||
require "sequel" | ||
|
||
class SequelMigrator | ||
MODEL_TABLE = "my_sequel_models" | ||
TRANSITION_TABLE = "my_sequel_model_transitions" | ||
|
||
def initialize(db) | ||
@db = db | ||
end | ||
|
||
def up | ||
down | ||
create_model_table | ||
create_transition_table | ||
end | ||
|
||
def down | ||
[TRANSITION_TABLE, MODEL_TABLE].each do |table_name| | ||
@db.execute("DROP TABLE IF EXISTS #{table_name};") | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_model_table | ||
@db.create_table(:my_sequel_models) do | ||
primary_key :id, type: Bignum | ||
String :current_state | ||
end | ||
end | ||
|
||
def create_transition_table | ||
@db.create_table(:my_sequel_model_transitions) do | ||
primary_key :id, type: Bignum | ||
String :to_state | ||
foreign_key :my_sequel_model_id, :my_sequel_models | ||
Bignum :sort_key, index: true, unique: true | ||
String :metadata | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method now requires an additional
options={}
argument to work on the latest version.