Module: Taps::Schema
Instance Method Summary collapse
- #dump(database_url) ⇒ Object
- #dump_table(database_url, table) ⇒ Object
- #indexes(database_url) ⇒ Object
- #indexes_individual(database_url) ⇒ Object
- #load(database_url, schema) ⇒ Object
- #load_indexes(database_url, indexes) ⇒ Object
- #reset_db_sequences(database_url) ⇒ Object
Instance Method Details
#dump(database_url) ⇒ Object
11 12 13 14 |
# File 'lib/taps/schema.rb', line 11 def dump(database_url) db = Sequel.connect(database_url) db.dump_schema_migration(:indexes => false) end |
#dump_table(database_url, table) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/taps/schema.rb', line 16 def dump_table(database_url, table) table = table.to_sym Sequel.connect(database_url) do |db| <<END_MIG Class.new(Sequel::Migration) do def up #{db.dump_table_schema(table.identifier, :indexes => false)} end def down drop_table("#{table}") if @db.table_exists?("#{table}") end end END_MIG end end |
#indexes(database_url) ⇒ Object
33 34 35 36 |
# File 'lib/taps/schema.rb', line 33 def indexes(database_url) db = Sequel.connect(database_url) db.dump_indexes_migration end |
#indexes_individual(database_url) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/taps/schema.rb', line 38 def indexes_individual(database_url) idxs = {} Sequel.connect(database_url) do |db| tables = db.tables tables.each do |table| idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n") end end idxs.each do |table, indexes| idxs[table] = indexes.map do |idx| <<END_MIG Class.new(Sequel::Migration) do def up #{idx} end end END_MIG end end idxs.to_json end |
#load(database_url, schema) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/taps/schema.rb', line 61 def load(database_url, schema) Sequel.connect(database_url) do |db| klass = eval(schema) klass.apply(db, :down) klass.apply(db, :up) end end |
#load_indexes(database_url, indexes) ⇒ Object
69 70 71 72 73 |
# File 'lib/taps/schema.rb', line 69 def load_indexes(database_url, indexes) Sequel.connect(database_url) do |db| eval(indexes).apply(db, :up) end end |
#reset_db_sequences(database_url) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/taps/schema.rb', line 75 def reset_db_sequences(database_url) db = Sequel.connect(database_url) return unless db.respond_to?(:reset_primary_key_sequence) db.tables.each do |table| db.reset_primary_key_sequence(table) end end |