Skip to content

Commit

Permalink
Switch SemanticLink to use new association
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Oct 30, 2024
1 parent f4606f6 commit f6c53dc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/models/semantic_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

# Link a Spree::Variant to an external DFC SuppliedProduct.
class SemanticLink < ApplicationRecord
self.ignored_columns += [:variant_id]

belongs_to :subject, polymorphic: true
belongs_to :variant, class_name: "Spree::Variant"

validates :semantic_id, presence: true
end
8 changes: 8 additions & 0 deletions db/migrate/20241030033956_change_null_on_semantic_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ChangeNullOnSemanticLinks < ActiveRecord::Migration[7.0]
def change
change_column_null :semantic_links, :subject_id, false
change_column_null :semantic_links, :subject_type, false

change_column_null :semantic_links, :variant_id, true
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_10_30_023153) do
ActiveRecord::Schema[7.0].define(version: 2024_10_30_033956) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand Down Expand Up @@ -401,12 +401,12 @@
end

create_table "semantic_links", force: :cascade do |t|
t.bigint "variant_id", null: false
t.bigint "variant_id"
t.string "semantic_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "subject_type"
t.bigint "subject_id"
t.string "subject_type", null: false
t.bigint "subject_id", null: false
t.index ["subject_type", "subject_id"], name: "index_semantic_links_on_subject"
t.index ["variant_id"], name: "index_semantic_links_on_variant_id"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

it "copies the original data" do
link = SemanticLink.create!(
variant: original_variant,
subject: dummy_variant, # This would be NULL when migration runs.
semantic_id: "some-url",
)
SemanticLink.update_all("variant_id = #{original_variant.id}")

expect { subject.up }.to change {
link.reload.subject
Expand Down

0 comments on commit f6c53dc

Please sign in to comment.