Skip to content

Commit

Permalink
Merge pull request #265 from plural/add_printings_card_subtypes
Browse files Browse the repository at this point in the history
Explicitly connect printings and card subtypes.
  • Loading branch information
plural authored Jun 22, 2024
2 parents ef9e972 + 9a3cf6a commit a31cf89
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/models/printing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Printing < ApplicationRecord
has_one :faction, :through => :card
has_one :card_cycle, :through => :card_set
has_one :card_type, :through => :card
has_many :printing_card_subtypes
has_many :card_subtypes, :through => :printing_card_subtypes
has_one :side, :through => :card
has_many :illustrator_printings
has_many :illustrators, :through => :illustrator_printings
Expand Down
10 changes: 10 additions & 0 deletions app/models/printing_card_subtype.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class PrintingCardSubtype < ApplicationRecord
self.table_name = 'printings_card_subtypes'

belongs_to :printing
belongs_to :card_subtype
belongs_to :unified_printing,
foreign_key: :printing_id
end
34 changes: 23 additions & 11 deletions app/models/unified_printing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ class UnifiedPrinting < ApplicationRecord

belongs_to :side
belongs_to :unified_card,
:primary_key => :id,
:foreign_key => :card_id
primary_key: :id,
foreign_key: :card_id
belongs_to :card
belongs_to :card_set
has_one :faction, :through => :card
has_one :card_cycle, :through => :card_set
has_one :card_type, :through => :card
has_one :side, :through => :card
has_many :illustrator_printings, primary_key: :id, foreign_key: :printing_id
has_many :illustrators, :through => :illustrator_printings
has_one :faction, through: :card
has_one :card_cycle, through: :card_set
has_one :card_type, through: :card

has_many :unified_restrictions, primary_key: :card_id, foreign_key: :card_id
has_many :card_pool_cards, primary_key: :card_id, foreign_key: :card_id
has_many :card_pools, :through => :card_pool_cards
has_many :printing_card_subtypes,
primary_key: :id,
foreign_key: :printing_id
has_many :card_subtypes, through: :printing_card_subtypes

has_one :side, through: :card
has_many :illustrator_printings,
primary_key: :id,
foreign_key: :printing_id
has_many :illustrators, through: :illustrator_printings

has_many :unified_restrictions,
primary_key: :card_id,
foreign_key: :card_id
has_many :card_pool_cards,
primary_key: :card_id,
foreign_key: :card_id
has_many :card_pools, through: :card_pool_cards
end
9 changes: 9 additions & 0 deletions db/migrate/20240622192959_create_printings_card_subtypes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreatePrintingsCardSubtypes < ActiveRecord::Migration[7.1]
def change
create_table :printings_card_subtypes, id: false, force: :cascade do |t|
t.text :printing_id, null: false
t.text :card_subtype_id, null: false
t.index [:printing_id, :card_subtype_id], name: "index_printings_card_subtypes_on_card_id_and_subtype_id"
end
end
end
8 changes: 7 additions & 1 deletion 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.1].define(version: 2024_05_12_220325) do
ActiveRecord::Schema[7.1].define(version: 2024_06_22_192959) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -222,6 +222,12 @@
t.string "released_by"
end

create_table "printings_card_subtypes", id: false, force: :cascade do |t|
t.text "printing_id", null: false
t.text "card_subtype_id", null: false
t.index ["printing_id", "card_subtype_id"], name: "index_printings_card_subtypes_on_card_id_and_subtype_id"
end

create_table "restrictions", id: :string, force: :cascade do |t|
t.text "name", null: false
t.text "date_start", null: false
Expand Down
40 changes: 40 additions & 0 deletions lib/tasks/cards.rake
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,43 @@ namespace :cards do
}
end

# This assumes that cards and card subtypes have already been loaded.
def import_printing_subtypes()
printing_id_to_card_subtype_id = []
Card.all.each { |c|
c.printing_ids.each { |p|
c.card_subtype_ids.each { |s|
printing_id_to_card_subtype_id << [p, s]
}
}
}

# Use a transaction since we are completely replacing the mapping table.
ActiveRecord::Base.transaction do
puts ' Clear out existing printing -> subtype mappings'
unless ActiveRecord::Base.connection.delete("DELETE FROM printings_card_subtypes")
puts 'Hit an error while deleting card -> subtype mappings. rolling back.'
raise ActiveRecord::Rollback
end

num_assoc = 0
printing_id_to_card_subtype_id.each_slice(250) { |m|
num_assoc += m.length
puts ' %d printing -> subtype associations' % num_assoc
sql = 'INSERT INTO printings_card_subtypes (printing_id, card_subtype_id) VALUES '
vals = []
m.each { |m|
vals << "('%s', '%s')" % [m[0], m[1]]
}
sql << vals.join(', ')
unless ActiveRecord::Base.connection.execute(sql)
puts 'Hit an error while inserting card -> subtype mappings. rolling back.'
raise ActiveRecord::Rollback
end
}
end
end

# We don't reload JSON files in here because we have already saved all the cards
# with their subtypes fields and can parse from there.
def import_card_subtypes(cards)
Expand Down Expand Up @@ -773,6 +810,9 @@ namespace :cards do
puts 'Importing Printings...'
import_printings(printings_json)

puts 'Importing Subtypes for Printings...'
import_printing_subtypes()

puts 'Importing Illustrators...'
import_illustrators()

Expand Down

0 comments on commit a31cf89

Please sign in to comment.