Class: Card::Migration

Inherits:
ActiveRecord::Migration
  • Object
show all
Includes:
Card::Model::SaveHelper
Defined in:
lib/card/migration.rb,
lib/card/migration/core.rb,
lib/card/migration/import.rb,
lib/card/migration/import/import_data.rb

Direct Known Subclasses

Core

Defined Under Namespace

Classes: Core, Import

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Model::SaveHelper

#create_card, #create_card!, #create_or_update, #create_or_update!, #ensure_card, #resolve_name_conflict, #update_card, #update_card!

Class Method Details

.assume_migrated_upto_versionObject



53
54
55
56
57
58
# File 'lib/card/migration.rb', line 53

def assume_migrated_upto_version
  schema_mode do
    ActiveRecord::Schema.assume_migrated_upto_version schema,
                                                      migration_paths
  end
end

.data_path(filename = nil) ⇒ Object



60
61
62
63
# File 'lib/card/migration.rb', line 60

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

.find_unused_name(base_name) ⇒ Object



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

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



29
30
31
# File 'lib/card/migration.rb', line 29

def migration_paths mig_type=type
  Cardio.migration_paths mig_type
end

.schema(mig_type = type) ⇒ Object



33
34
35
# File 'lib/card/migration.rb', line 33

def schema mig_type=type
  Cardio.schema mig_type
end

.schema_mode(mig_type = type) {|paths| ... } ⇒ Object

Yields:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/card/migration.rb', line 41

def schema_mode mig_type=type
  new_suffix = schema_suffix mig_type
  original_suffix = ActiveRecord::Base.table_name_suffix

  ActiveRecord::Base.table_name_suffix = new_suffix
  ActiveRecord::SchemaMigration.reset_table_name
  paths = Cardio.migration_paths(type)
  yield(paths)
  ActiveRecord::Base.table_name_suffix = original_suffix
  ActiveRecord::SchemaMigration.reset_table_name
end

.schema_suffix(mig_type = type) ⇒ Object



37
38
39
# File 'lib/card/migration.rb', line 37

def schema_suffix mig_type=type
  Cardio.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 Card::Migration or Card::CoreMigration but they don't inherit the @type. The method below solves this problem.



15
16
17
# File 'lib/card/migration.rb', line 15

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

Instance Method Details

#contentedlyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/card/migration.rb', line 66

def contentedly
  Card::Cache.reset_all
  Cardio.schema_mode "" do
    Card::Auth.as_bot do
      ActiveRecord::Base.transaction do
        begin
          yield
        ensure
          Card::Cache.reset_all
        end
      end
    end
  end
end

#data_path(filename = nil) ⇒ Object



108
109
110
# File 'lib/card/migration.rb', line 108

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

#downObject

Raises:

  • (ActiveRecord::IrreversibleMigration)


137
138
139
# File 'lib/card/migration.rb', line 137

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 'contentendly'



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/card/migration.rb', line 122

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/card/migration.rb', line 88

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



81
82
83
84
85
86
# File 'lib/card/migration.rb', line 81

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

#migration_pathsObject



116
117
118
# File 'lib/card/migration.rb', line 116

def migration_paths
  Cardio.paths self.class.type
end

#read_json(filename) ⇒ Object



102
103
104
105
106
# File 'lib/card/migration.rb', line 102

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

#schema_modeObject



112
113
114
# File 'lib/card/migration.rb', line 112

def schema_mode
  Cardio.schema_mode self.class.type
end

#update_machine_outputObject



141
142
143
# File 'lib/card/migration.rb', line 141

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