Module: Tapsoob::Schema
Instance Method Summary collapse
- #dump(database_url, options = {}) ⇒ Object
- #dump_table(database_url, table, options) ⇒ Object
- #foreign_keys(database_url) ⇒ Object
- #indexes(database_url) ⇒ Object
- #indexes_individual(database_url) ⇒ Object
- #load(database_url, schema, options = { drop: false }) ⇒ Object
- #load_foreign_keys(database_url, foreign_keys) ⇒ Object
- #load_indexes(database_url, indexes) ⇒ Object
- #reset_db_sequences(database_url) ⇒ Object
Instance Method Details
#dump(database_url, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tapsoob/schema.rb', line 12 def dump(database_url, = {}) db = Sequel.connect(database_url) db.extension :schema_dumper template = ERB.new <<-END_MIG Class.new(Sequel::Migration) do def up <% db.send(:sort_dumped_tables, db.tables, {}).each do |table| %> <%= db.dump_table_schema(table, options) %> <% end %> end def down <% db.send(:sort_dumped_tables, db.tables, {}).reverse.each do |table| %> drop_table("<%= table %>", if_exists: true, cascade: true) <% end %> end end END_MIG template.result(binding) end |
#dump_table(database_url, table, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/tapsoob/schema.rb', line 34 def dump_table(database_url, table, ) table = table.to_sym Sequel.connect(database_url) do |db| db.extension :schema_dumper <<END_MIG Class.new(Sequel::Migration) do def up #{db.dump_table_schema(table, )} end def down drop_table("#{table}", if_exists: true) end end END_MIG end end |
#foreign_keys(database_url) ⇒ Object
52 53 54 55 56 |
# File 'lib/tapsoob/schema.rb', line 52 def foreign_keys(database_url) db = Sequel.connect(database_url) db.extension :schema_dumper db.dump_foreign_key_migration end |
#indexes(database_url) ⇒ Object
58 59 60 61 62 |
# File 'lib/tapsoob/schema.rb', line 58 def indexes(database_url) db = Sequel.connect(database_url) db.extension :schema_dumper db.dump_indexes_migration end |
#indexes_individual(database_url) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tapsoob/schema.rb', line 64 def indexes_individual(database_url) idxs = {} Sequel.connect(database_url) do |db| db.extension :schema_dumper 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 JSON.generate(idxs) end |
#load(database_url, schema, options = { drop: false }) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tapsoob/schema.rb', line 89 def load(database_url, schema, = { drop: false }) Sequel.connect(database_url) do |db| db.extension :schema_dumper klass = eval(schema) if [:drop] # Start special hack for MySQL db.run("SET foreign_key_checks = 0") if [:mysql, :mysql2].include?(db.adapter_scheme) # Run down migration klass.apply(db, :down) # End special hack for MySQL db.run("SET foreign_key_checks = 1") if [:mysql, :mysql2].include?(db.adapter_scheme) end klass.apply(db, :up) end end |
#load_foreign_keys(database_url, foreign_keys) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/tapsoob/schema.rb', line 107 def load_foreign_keys(database_url, foreign_keys) Sequel.connect(database_url) do |db| db.extension :schema_dumper eval(foreign_keys).apply(db, :up) end end |
#load_indexes(database_url, indexes) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/tapsoob/schema.rb', line 114 def load_indexes(database_url, indexes) Sequel.connect(database_url) do |db| db.extension :schema_dumper eval(indexes).apply(db, :up) end end |
#reset_db_sequences(database_url) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/tapsoob/schema.rb', line 121 def reset_db_sequences(database_url) db = Sequel.connect(database_url) db.extension :schema_dumper return unless db.respond_to?(:reset_primary_key_sequence) db.tables.each do |table| db.reset_primary_key_sequence(table) end end |