diff --git a/lib/scenic/definition.rb b/lib/scenic/definition.rb index 58195107..0ce4a9f0 100644 --- a/lib/scenic/definition.rb +++ b/lib/scenic/definition.rb @@ -1,9 +1,10 @@ module Scenic # @api private class Definition - def initialize(name, version) + def initialize(name, version, root_path: Rails.root) @name = name.to_s @version = version.to_i + @root_path = root_path.is_a?(String) ? Pathname.new(root_path) : root_path end def to_sql @@ -15,7 +16,7 @@ def to_sql end def full_path - Rails.root.join(path) + @root_path.join(path) end def path diff --git a/lib/scenic/statements.rb b/lib/scenic/statements.rb index 84bf24b0..e2e23c61 100644 --- a/lib/scenic/statements.rb +++ b/lib/scenic/statements.rb @@ -22,7 +22,7 @@ module Statements # SELECT * FROM users WHERE users.active = 't' # SQL # - def create_view(name, version: nil, sql_definition: nil, materialized: false) + def create_view(name, version: nil, sql_definition: nil, materialized: false, root_path: Rails.root) if version.present? && sql_definition.present? raise( ArgumentError, @@ -34,7 +34,7 @@ def create_view(name, version: nil, sql_definition: nil, materialized: false) version = 1 end - sql_definition ||= definition(name, version) + sql_definition ||= definition(name, version, root_path: root_path) if materialized Scenic.database.create_materialized_view( @@ -88,7 +88,7 @@ def drop_view(name, revert_to_version: nil, materialized: false) # @example # update_view :engagement_reports, version: 3, revert_to_version: 2 # - def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, materialized: false) + def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, materialized: false, root_path: Rails.root) if version.blank? && sql_definition.blank? raise( ArgumentError, @@ -103,7 +103,7 @@ def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, ) end - sql_definition ||= definition(name, version) + sql_definition ||= definition(name, version, root_path: root_path) if materialized Scenic.database.update_materialized_view( @@ -132,7 +132,7 @@ def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, # @example # replace_view :engagement_reports, version: 3, revert_to_version: 2 # - def replace_view(name, version: nil, revert_to_version: nil, materialized: false) + def replace_view(name, version: nil, revert_to_version: nil, materialized: false, root_path: Rails.root) if version.blank? raise ArgumentError, "version is required" end @@ -141,15 +141,15 @@ def replace_view(name, version: nil, revert_to_version: nil, materialized: false raise ArgumentError, "Cannot replace materialized views" end - sql_definition = definition(name, version) + sql_definition = definition(name, version, root_path: root_path) Scenic.database.replace_view(name, sql_definition) end private - def definition(name, version) - Scenic::Definition.new(name, version).to_sql + def definition(name, version, root_path: Rails.root) + Scenic::Definition.new(name, version, root_path: root_path).to_sql end def no_data(materialized) diff --git a/spec/scenic/definition_spec.rb b/spec/scenic/definition_spec.rb index e840ac6b..8756108f 100644 --- a/spec/scenic/definition_spec.rb +++ b/spec/scenic/definition_spec.rb @@ -51,6 +51,13 @@ module Scenic expect(definition.full_path).to eq Rails.root.join(definition.path) end + + it "joins the path with whatever root you pass in" do + new_root = Pathname.new("component/engine/test/dummy") + definition = Definition.new("searches", 15, root_path: new_root) + + expect(definition.full_path).to eq new_root.join(definition.path) + end end describe "version" do diff --git a/spec/scenic/statements_spec.rb b/spec/scenic/statements_spec.rb index b0aa5120..e201d080 100644 --- a/spec/scenic/statements_spec.rb +++ b/spec/scenic/statements_spec.rb @@ -12,7 +12,7 @@ module Scenic version = 15 definition_stub = instance_double("Definition", to_sql: "foo") allow(Definition).to receive(:new) - .with(:views, version) + .with(:views, version, root_path: Rails.root) .and_return(definition_stub) connection.create_view :views, version: version @@ -21,6 +21,20 @@ module Scenic .with(:views, definition_stub.to_sql) end + it "creates a view from a file and alternate root_path" do + version = 15 + root_path = Pathname.new("component/engine/test/dummy") + definition_stub = instance_double("Definition", to_sql: "foo") + allow(Definition).to receive(:new) + .with(:views, version, root_path: root_path) + .and_return(definition_stub) + + connection.create_view :views, version: version, root_path: root_path + + expect(Scenic.database).to have_received(:create_view) + .with(:views, definition_stub.to_sql) + end + it "creates a view from a text definition" do sql_definition = "a defintion" @@ -34,7 +48,7 @@ module Scenic version = 1 definition_stub = instance_double("Definition", to_sql: "foo") allow(Definition).to receive(:new) - .with(:views, version) + .with(:views, version, root_path: Rails.root) .and_return(definition_stub) connection.create_view :views @@ -98,7 +112,7 @@ module Scenic it "updates the view in the database" do definition = instance_double("Definition", to_sql: "definition") allow(Definition).to receive(:new) - .with(:name, 3) + .with(:name, 3, root_path: Rails.root) .and_return(definition) connection.update_view(:name, version: 3) @@ -107,6 +121,19 @@ module Scenic .with(:name, definition.to_sql) end + it "updates the view in the database with an alternate path" do + root_path = Pathname.new("component/engine/test/dummy") + definition = instance_double("Definition", to_sql: "definition") + allow(Definition).to receive(:new) + .with(:name, 3, root_path: root_path) + .and_return(definition) + + connection.update_view(:name, version: 3, root_path: root_path) + + expect(Scenic.database).to have_received(:update_view) + .with(:name, definition.to_sql) + end + it "updates a view from a text definition" do sql_definition = "a defintion" @@ -119,7 +146,7 @@ module Scenic it "updates the materialized view in the database" do definition = instance_double("Definition", to_sql: "definition") allow(Definition).to receive(:new) - .with(:name, 3) + .with(:name, 3, root_path: Rails.root) .and_return(definition) connection.update_view(:name, version: 3, materialized: true) @@ -131,7 +158,7 @@ module Scenic it "updates the materialized view in the database with NO DATA" do definition = instance_double("Definition", to_sql: "definition") allow(Definition).to receive(:new) - .with(:name, 3) + .with(:name, 3, root_path: Rails.root) .and_return(definition) connection.update_view( @@ -166,7 +193,7 @@ module Scenic it "replaces the view in the database" do definition = instance_double("Definition", to_sql: "definition") allow(Definition).to receive(:new) - .with(:name, 3) + .with(:name, 3, root_path: Rails.root) .and_return(definition) connection.replace_view(:name, version: 3) @@ -175,6 +202,19 @@ module Scenic .with(:name, definition.to_sql) end + it "replaces the view in the database with an alternate path" do + root_path = Pathname.new("component/engine/test/dummy") + definition = instance_double("Definition", to_sql: "definition") + allow(Definition).to receive(:new) + .with(:name, 3, root_path: root_path) + .and_return(definition) + + connection.replace_view(:name, version: 3, root_path: root_path) + + expect(Scenic.database).to have_received(:replace_view) + .with(:name, definition.to_sql) + end + it "fails to replace the materialized view in the database" do definition = instance_double("Definition", to_sql: "definition") allow(Definition).to receive(:new)