Skip to content

Commit

Permalink
Explicitly specify the primary key for the metrics tables
Browse files Browse the repository at this point in the history
This is required as a view doesn't have a primary key, so
active record can't fetch the value by default.
  • Loading branch information
carbonin committed Feb 26, 2019
1 parent cb18388 commit 7ea5163
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# [PostgreSQL table inheritance]: https://www.postgresql.org/docs/9.6/static/tutorial-inheritance.html
# [PostgreSQL table partioning]: https://www.postgresql.org/docs/9.6/static/ddl-partitioning.html
class Metric < ApplicationRecord
# Specify the primary key for a model backed by a view
self.primary_key = "id"

BASE_COLS = ["id", "timestamp", "capture_interval_name", "resource_type", "resource_id", "resource_name", "tag_names", "parent_host_id", "parent_ems_cluster_id", "parent_ems_id", "parent_storage_id"]

include Metric::Common
Expand Down
3 changes: 3 additions & 0 deletions app/models/metric_rollup.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# @see Metric
class MetricRollup < ApplicationRecord
# Specify the primary key for a model backed by a view
self.primary_key = "id"

include Metric::Common
include_concern 'Metric::ChargebackHelper'

Expand Down
21 changes: 21 additions & 0 deletions spec/models/metric_rollup_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
describe MetricRollup do
describe "metric_rollups view" do
it "creates an object with an id" do
metric = described_class.create!(:timestamp => Time.now)
expect(metric.id).to be > 0
end

it "initializes an object's id after save" do
metric = described_class.new
metric.timestamp = Time.now
metric.save!
expect(metric.id).to be > 0
end

it "updates an existing object correctly" do
metric = described_class.create!(:timestamp => Time.now)
old_id = metric.id
metric.update_attributes!(:timestamp => Time.now - 1.day)
expect(metric.id).to eq(old_id)
end
end

context "test" do
it "should not raise an error when a polymorphic reflection is included and references are specified in a query" do
skip "until ActiveRecord is fixed"
Expand Down
21 changes: 21 additions & 0 deletions spec/models/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
_guid, _server, @zone = EvmSpecHelper.create_guid_miq_server_zone
end

describe "metrics view" do
it "creates an object with an id" do
metric = described_class.create!(:timestamp => Time.now)
expect(metric.id).to be > 0
end

it "initializes an object's id after save" do
metric = described_class.new
metric.timestamp = Time.now
metric.save!
expect(metric.id).to be > 0
end

it "updates an existing object correctly" do
metric = described_class.create!(:timestamp => Time.now)
old_id = metric.id
metric.update_attributes!(:timestamp => Time.now - 1.day)
expect(metric.id).to eq(old_id)
end
end

context "as vmware" do
before do
@ems_vmware = FactoryBot.create(:ems_vmware, :zone => @zone)
Expand Down

0 comments on commit 7ea5163

Please sign in to comment.