Skip to content

Commit a31cf89

Browse files
authored
Merge pull request #265 from plural/add_printings_card_subtypes
Explicitly connect printings and card subtypes.
2 parents ef9e972 + 9a3cf6a commit a31cf89

File tree

6 files changed

+91
-12
lines changed

6 files changed

+91
-12
lines changed

app/models/printing.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Printing < ApplicationRecord
77
has_one :faction, :through => :card
88
has_one :card_cycle, :through => :card_set
99
has_one :card_type, :through => :card
10+
has_many :printing_card_subtypes
11+
has_many :card_subtypes, :through => :printing_card_subtypes
1012
has_one :side, :through => :card
1113
has_many :illustrator_printings
1214
has_many :illustrators, :through => :illustrator_printings

app/models/printing_card_subtype.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class PrintingCardSubtype < ApplicationRecord
4+
self.table_name = 'printings_card_subtypes'
5+
6+
belongs_to :printing
7+
belongs_to :card_subtype
8+
belongs_to :unified_printing,
9+
foreign_key: :printing_id
10+
end

app/models/unified_printing.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ class UnifiedPrinting < ApplicationRecord
55

66
belongs_to :side
77
belongs_to :unified_card,
8-
:primary_key => :id,
9-
:foreign_key => :card_id
8+
primary_key: :id,
9+
foreign_key: :card_id
1010
belongs_to :card
1111
belongs_to :card_set
12-
has_one :faction, :through => :card
13-
has_one :card_cycle, :through => :card_set
14-
has_one :card_type, :through => :card
15-
has_one :side, :through => :card
16-
has_many :illustrator_printings, primary_key: :id, foreign_key: :printing_id
17-
has_many :illustrators, :through => :illustrator_printings
12+
has_one :faction, through: :card
13+
has_one :card_cycle, through: :card_set
14+
has_one :card_type, through: :card
1815

19-
has_many :unified_restrictions, primary_key: :card_id, foreign_key: :card_id
20-
has_many :card_pool_cards, primary_key: :card_id, foreign_key: :card_id
21-
has_many :card_pools, :through => :card_pool_cards
16+
has_many :printing_card_subtypes,
17+
primary_key: :id,
18+
foreign_key: :printing_id
19+
has_many :card_subtypes, through: :printing_card_subtypes
20+
21+
has_one :side, through: :card
22+
has_many :illustrator_printings,
23+
primary_key: :id,
24+
foreign_key: :printing_id
25+
has_many :illustrators, through: :illustrator_printings
26+
27+
has_many :unified_restrictions,
28+
primary_key: :card_id,
29+
foreign_key: :card_id
30+
has_many :card_pool_cards,
31+
primary_key: :card_id,
32+
foreign_key: :card_id
33+
has_many :card_pools, through: :card_pool_cards
2234
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreatePrintingsCardSubtypes < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :printings_card_subtypes, id: false, force: :cascade do |t|
4+
t.text :printing_id, null: false
5+
t.text :card_subtype_id, null: false
6+
t.index [:printing_id, :card_subtype_id], name: "index_printings_card_subtypes_on_card_id_and_subtype_id"
7+
end
8+
end
9+
end

db/schema.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2024_05_12_220325) do
13+
ActiveRecord::Schema[7.1].define(version: 2024_06_22_192959) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pgcrypto"
1616
enable_extension "plpgsql"
@@ -222,6 +222,12 @@
222222
t.string "released_by"
223223
end
224224

225+
create_table "printings_card_subtypes", id: false, force: :cascade do |t|
226+
t.text "printing_id", null: false
227+
t.text "card_subtype_id", null: false
228+
t.index ["printing_id", "card_subtype_id"], name: "index_printings_card_subtypes_on_card_id_and_subtype_id"
229+
end
230+
225231
create_table "restrictions", id: :string, force: :cascade do |t|
226232
t.text "name", null: false
227233
t.text "date_start", null: false

lib/tasks/cards.rake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,43 @@ namespace :cards do
214214
}
215215
end
216216

217+
# This assumes that cards and card subtypes have already been loaded.
218+
def import_printing_subtypes()
219+
printing_id_to_card_subtype_id = []
220+
Card.all.each { |c|
221+
c.printing_ids.each { |p|
222+
c.card_subtype_ids.each { |s|
223+
printing_id_to_card_subtype_id << [p, s]
224+
}
225+
}
226+
}
227+
228+
# Use a transaction since we are completely replacing the mapping table.
229+
ActiveRecord::Base.transaction do
230+
puts ' Clear out existing printing -> subtype mappings'
231+
unless ActiveRecord::Base.connection.delete("DELETE FROM printings_card_subtypes")
232+
puts 'Hit an error while deleting card -> subtype mappings. rolling back.'
233+
raise ActiveRecord::Rollback
234+
end
235+
236+
num_assoc = 0
237+
printing_id_to_card_subtype_id.each_slice(250) { |m|
238+
num_assoc += m.length
239+
puts ' %d printing -> subtype associations' % num_assoc
240+
sql = 'INSERT INTO printings_card_subtypes (printing_id, card_subtype_id) VALUES '
241+
vals = []
242+
m.each { |m|
243+
vals << "('%s', '%s')" % [m[0], m[1]]
244+
}
245+
sql << vals.join(', ')
246+
unless ActiveRecord::Base.connection.execute(sql)
247+
puts 'Hit an error while inserting card -> subtype mappings. rolling back.'
248+
raise ActiveRecord::Rollback
249+
end
250+
}
251+
end
252+
end
253+
217254
# We don't reload JSON files in here because we have already saved all the cards
218255
# with their subtypes fields and can parse from there.
219256
def import_card_subtypes(cards)
@@ -773,6 +810,9 @@ namespace :cards do
773810
puts 'Importing Printings...'
774811
import_printings(printings_json)
775812

813+
puts 'Importing Subtypes for Printings...'
814+
import_printing_subtypes()
815+
776816
puts 'Importing Illustrators...'
777817
import_illustrators()
778818

0 commit comments

Comments
 (0)