Module: Cardio::Schema

Defined in:
lib/cardio/schema.rb

Class Method Summary collapse

Class Method Details

.assume_migrated_upto_version(type, version = nil) ⇒ Object



4
5
6
7
8
9
# File 'lib/cardio/schema.rb', line 4

def assume_migrated_upto_version type, version=nil
  mode type do |_paths|
    version ||= version type
    ActiveRecord::Schema.assume_migrated_upto_version version
  end
end

.migrate(type, version = nil, verbose = true) ⇒ Object



11
12
13
14
15
16
# File 'lib/cardio/schema.rb', line 11

def migrate type, version=nil, verbose=true
  migration_context type do |mc|
    ActiveRecord::Migration.verbose = verbose
    mc.migrate version
  end
end

.migration_context(type) ⇒ Object



57
58
59
60
61
62
# File 'lib/cardio/schema.rb', line 57

def migration_context type
  with_suffix type do
    yield ActiveRecord::MigrationContext.new(migration_paths(type),
                                             ActiveRecord::SchemaMigration)
  end
end

.migration_paths(type) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cardio/schema.rb', line 44

def migration_paths type
  list = Cardio.paths["db/migrate#{suffix type}"].to_a
  case type
  when :core_cards
    list += mod_migration_paths "migrate_core_cards"
  when :deck_cards
    list += mod_migration_paths "migrate_cards"
  when :deck
    list += mod_migration_paths "migrate"
  end
  list.flatten
end

.mode(type) ⇒ Object



27
28
29
30
31
# File 'lib/cardio/schema.rb', line 27

def mode type
  with_suffix type do
    yield migration_paths(type)
  end
end

.stamp_path(type) ⇒ Object



37
38
39
40
41
42
# File 'lib/cardio/schema.rb', line 37

def stamp_path type
  root_dir = deck_migration?(type) ? Cardio.root : Cardio.gem_root
  stamp_dir = ENV["SCHEMA_STAMP_PATH"] || File.join(root_dir, "db")

  File.join stamp_dir, "version#{suffix type}.txt"
end

.suffix(type) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cardio/schema.rb', line 18

def suffix type
  case type
  when :core_cards then "_core_cards"
  when :deck_cards then "_deck_cards"
  when :deck then "_deck"
  else ""
  end
end

.version(type = nil) ⇒ Object



33
34
35
# File 'lib/cardio/schema.rb', line 33

def version type=nil
  File.read(stamp_path(type)).strip
end