Module: Cardio::Schema

Included in:
Cardio
Defined in:
lib/cardio/schema.rb

Instance Method Summary collapse

Instance Method Details

#assume_migrated_upto_version(type) ⇒ Object



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

def assume_migrated_upto_version type
  Cardio.schema_mode(type) do
    ActiveRecord::Schema.assume_migrated_upto_version(Cardio.schema(type),
                                                      Cardio.migration_paths(type))
  end
end

#deck_migration?(type) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cardio/schema.rb', line 72

def deck_migration? type
  type.in? %i[deck_cards deck]
end

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



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

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



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

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

#schema(type = nil) ⇒ Object



61
62
63
# File 'lib/cardio/schema.rb', line 61

def schema type=nil
  File.read(schema_stamp_path(type)).strip
end

#schema_mode(type) ⇒ Object



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

def schema_mode type
  with_suffix type do
    paths = Cardio.migration_paths(type)
    yield(paths)
  end
end

#schema_stamp_path(type) ⇒ Object



65
66
67
68
69
70
# File 'lib/cardio/schema.rb', line 65

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

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

#schema_suffix(type) ⇒ Object



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

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

#with_migration_table(new_table_name, old_table_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/cardio/schema.rb', line 50

def with_migration_table new_table_name, old_table_name
  ActiveRecord::Base.schema_migrations_table_name = new_table_name
  ActiveRecord::SchemaMigration.table_name = new_table_name
  ActiveRecord::SchemaMigration.reset_column_information
  yield
ensure
  ActiveRecord::Base.schema_migrations_table_name = old_table_name
  ActiveRecord::SchemaMigration.table_name = old_table_name
  ActiveRecord::SchemaMigration.reset_column_information
end

#with_suffix(type) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/cardio/schema.rb', line 40

def with_suffix type
  return yield unless (new_suffix = Cardio.schema_suffix type) &&
                      new_suffix.present?

  original_name = ActiveRecord::Base.schema_migrations_table_name
  with_migration_table "#{original_name}#{new_suffix}", original_name do
    yield
  end
end