Class: Card::Migration

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/card/migration.rb

Direct Known Subclasses

CoreMigration

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assume_migrated_upto_versionObject



49
50
51
52
53
# File 'lib/card/migration.rb', line 49

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

.data_path(filename = nil) ⇒ Object



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

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

.find_unused_name(base_name) ⇒ Object



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

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

.migration_paths(mig_type = type) ⇒ Object



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

def migration_paths mig_type=type
  Cardio.migration_paths mig_type
end

.schema(mig_type = type) ⇒ Object



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

def schema mig_type=type
  Cardio.schema mig_type
end

.schema_mode(mig_type = type) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/card/migration.rb', line 40

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
  yield
  ActiveRecord::Base.table_name_suffix = original_suffix
end

.schema_suffix(mig_type = type) ⇒ Object



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

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.



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

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

Instance Method Details

#contentedly(&block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/card/migration.rb', line 62

def contentedly &block
  Card::Cache.reset_global
  Cardio.schema_mode '' do
    Card::Auth.as_bot do
      ActiveRecord::Base.transaction do
        begin
          yield
        ensure
          Card::Cache.reset_global
        end
      end
    end
  end
end

#data_path(filename = nil) ⇒ Object



89
90
91
# File 'lib/card/migration.rb', line 89

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

#downObject

Raises:

  • (ActiveRecord::IrreversibleMigration)


145
146
147
# File 'lib/card/migration.rb', line 145

def down
  raise ActiveRecord::IrreversibleMigration
end

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



77
78
79
80
81
# File 'lib/card/migration.rb', line 77

def import_json filename, merge_opts={}
  Card.config.action_mailer.perform_deliveries = false
  merge_opts.reverse_merge! :output_file=>File.join(data_path,"unmerged_#{ filename }")
  Card.merge_list read_json(filename), merge_opts
end

#migrate(direction) ⇒ Object

Execute this migration in the named direction copied from ActiveRecord to wrap “up” in “contentendly”



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/card/migration.rb', line 104

def migrate(direction)
  return unless respond_to?(direction)

  case direction
  when :up   then announce "migrating"
  when :down then announce "reverting"
  end

  time   = nil
  ActiveRecord::Base.connection_pool.with_connection do |conn|
    @connection = conn
    if respond_to?(:change)
      if direction == :down
        recorder = CommandRecorder.new(@connection)
        suppress_messages do
          @connection = recorder
          change
        end
        @connection = conn
        time = Benchmark.measure {
          self.revert {
            recorder.inverse.each do |cmd, args|
              send(cmd, *args)
            end
          }
        }
      else
        time = Benchmark.measure { change }
      end
    else
      time = Benchmark.measure { contentedly { send(direction) } }
    end
    @connection = nil
  end

  case direction
  when :up   then announce "migrated (%.4fs)" % time.real; write
  when :down then announce "reverted (%.4fs)" % time.real; write
  end
end

#migration_pathsObject



97
98
99
# File 'lib/card/migration.rb', line 97

def migration_paths
  Cardio.paths self.class.type
end

#read_json(filename) ⇒ Object



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

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

#schema_modeObject



93
94
95
# File 'lib/card/migration.rb', line 93

def schema_mode
  Cardio.schema_mode self.class.type
end