Class: Cardio::Migration

Inherits:
Object
  • Object
show all
Includes:
Card::Model::SaveHelper
Defined in:
lib/cardio/migration.rb,
lib/cardio/migration/core.rb,
lib/cardio/migration/import.rb,
lib/cardio/migration/import/merger.rb,
lib/cardio/migration/deck_structure.rb,
lib/cardio/migration/import/import_data.rb,
lib/cardio/migration/import/import_data/card_content.rb,
lib/cardio/migration/import/import_data/card_attributes.rb

Direct Known Subclasses

Core, DeckStructure

Defined Under Namespace

Classes: Core, DeckStructure, Import

Constant Summary

Constants included from Card::Model::SaveHelper::SaveHelperHelper

Card::Model::SaveHelper::SaveHelperHelper::CARDTYPE_METHOD_REGEXP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Model::SaveHelper

#add_script, #create_card, #create_card!, #create_or_update_card, #create_or_update_card!, #delete_card, #delete_code_card, #ensure_card, #ensure_card!, #ensure_code_card, #ensure_trait, #ensure_trait_rule, #update_card, #update_card!, #with_user

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Card::Model::SaveHelper::SaveHelperHelper

Class Method Details

.assume_currentObject



46
47
48
49
50
51
52
# File 'lib/cardio/migration.rb', line 46

def assume_current
  migration_context do |mc|
    versions = mc.migrations.map(&:version)
    migrated = mc.get_all_versions
    mark_as_migrated versions - migrated
  end
end

.assume_migrated_upto_versionObject



42
43
44
# File 'lib/cardio/migration.rb', line 42

def assume_migrated_upto_version
  Schema.assume_migrated_upto_version type
end

.data_path(filename = nil) ⇒ Object



54
55
56
# File 'lib/cardio/migration.rb', line 54

def data_path filename=nil
  File.join([migration_paths.first, "data", filename].compact)
end

.find_unused_name(base_name) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/cardio/migration.rb', line 20

def find_unused_name base_name
  test_name = base_name
  add = 1
  while Card.exists?(test_name)
    test_name = "#{base_name}#{add}"
    add += 1
  end
  test_name
end

.migration_paths(mig_type = type) ⇒ Object



30
31
32
# File 'lib/cardio/migration.rb', line 30

def migration_paths mig_type=type
  Schema.migration_paths mig_type
end

.schema_mode(mig_type = type, &block) ⇒ Object



38
39
40
# File 'lib/cardio/migration.rb', line 38

def schema_mode mig_type=type, &block
  Schema.mode mig_type, &block
end

.schema_suffix(mig_type = type) ⇒ Object



34
35
36
# File 'lib/cardio/migration.rb', line 34

def schema_suffix mig_type=type
  Schema.suffix mig_type
end

.typeObject

Rake tasks use class methods, migrations use instance methods. To avoid repetition a lot of instance methods here just call class methods. The subclass Card::CoreMigration needs a different @type so we can’t use a class variable @@type. It has to be a class instance variable. Migrations are subclasses of Cardio::Migration or Card::CoreMigration but they don’t inherit the @type. The method below solves this problem.



16
17
18
# File 'lib/cardio/migration.rb', line 16

def type
  @type || ancestors[1]&.type
end

Instance Method Details

#contentedlyObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cardio/migration.rb', line 74

def contentedly
  return yield if ENV["NO_CARD_LOAD"]
  Card::Cache.reset_all
  Schema.mode "" do
    Card::Auth.as_bot do
      yield
    ensure
      ::Card::Cache.reset_all
    end
  end
end

#data_path(filename = nil) ⇒ Object



133
134
135
# File 'lib/cardio/migration.rb', line 133

def data_path filename=nil
  self.class.data_path filename
end

#downObject

Raises:

  • (ActiveRecord::IrreversibleMigration)


154
155
156
# File 'lib/cardio/migration.rb', line 154

def down
  raise ActiveRecord::IrreversibleMigration
end

#exec_migration(conn, direction) ⇒ Object

Execute this migration in the named direction copied from ActiveRecord to wrap ‘up’ in ‘contentedly’



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cardio/migration.rb', line 139

def exec_migration conn, direction
  @connection = conn
  if respond_to?(:change)
    if direction == :down
      revert { change }
    else
      change
    end
  else
    contentedly { send(direction) }
  end
ensure
  @connection = nil
end

#import_cards(filename, merge_opts = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cardio/migration.rb', line 97

def import_cards filename, merge_opts={}
  Card::Mailer.perform_deliveries = false
  output_file = File.join data_path, "unmerged_#{filename}"
  merge_opts[:output_file] ||= output_file
   = JSON.parse(File.read(data_path(filename)))
  full_data =
    .map do |hash|
      hash["content"] =
        File.read data_path(File.join("cards", hash["name"].to_name.key))
      hash
    end
  Card.merge_list full_data, merge_opts
end

#import_json(filename, merge_opts = {}) ⇒ Object

def disable_ddl_transaction #:nodoc:

true

end



90
91
92
93
94
95
# File 'lib/cardio/migration.rb', line 90

def import_json filename, merge_opts={}
  Card::Mailer.perform_deliveries = false
  output_file = File.join data_path, "unmerged_#{filename}"
  merge_opts[:output_file] ||= output_file
  Card.merge_list read_json(filename), merge_opts
end

#merge_cards(names_or_keys) ⇒ Object

uses the data in cards.yml and the card content in db/migrate_cards/data/cards to update or create the cards given by name or key in names_or_keys



113
114
115
116
117
118
# File 'lib/cardio/migration.rb', line 113

def merge_cards names_or_keys
  names_or_keys = Array(names_or_keys)
  Card::Mailer.perform_deliveries = false

  Migration::Import.new(data_path).merge only: names_or_keys
end

#merge_pristine_cards(names_or_keys) ⇒ Object



120
121
122
123
124
125
# File 'lib/cardio/migration.rb', line 120

def merge_pristine_cards names_or_keys
  names_or_keys = Array(names_or_keys)

  pristine = names_or_keys.select { |n| !Card.exists?(n) || Card.fetch(n)&.pristine? }
  merge_cards pristine
end

#read_json(filename) ⇒ Object



127
128
129
130
131
# File 'lib/cardio/migration.rb', line 127

def read_json filename
  raw_json = File.read data_path(filename)
  json = JSON.parse raw_json
  json.is_a?(Hash) ? json["card"]["value"] : json
end

#update_machine_outputObject



158
159
160
# File 'lib/cardio/migration.rb', line 158

def update_machine_output
  Card.search(right: { codename: "machine_output" }).each(&:delete)
end