Module: Rubee::CLI::Db
- Defined in:
- lib/rubee/cli/db.rb
Class Method Summary collapse
- .call(command, argv) ⇒ Object
- .drop_tables(_argv) ⇒ Object
- .init(_argv) ⇒ Object
- .run(argv) ⇒ Object
- .schema(argv) ⇒ Object
- .structure(_argv) ⇒ Object
- .truncate_tables(_argv) ⇒ Object
Class Method Details
.call(command, argv) ⇒ Object
5 6 7 8 9 |
# File 'lib/rubee/cli/db.rb', line 5 def call(command, argv) command = argv[1].split(':').first ENV['RACK_ENV'] ||= 'development' send(command, argv) end |
.drop_tables(_argv) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rubee/cli/db.rb', line 40 def drop_tables(_argv) adapter = Rubee::SequelObject::DB.adapter_scheme.to_s tables = Rubee::SequelObject::DB.tables case adapter when /postgres/ tables.each { |table| Rubee::SequelObject::DB.drop_table(table, cascade: true) } when /mysql/ Rubee::SequelObject::DB.run("SET FOREIGN_KEY_CHECKS = 0") tables.each { |table| Rubee::SequelObject::DB.drop_table(table) } Rubee::SequelObject::DB.run("SET FOREIGN_KEY_CHECKS = 1") when /sqlite/ Rubee::SequelObject::DB.run("PRAGMA foreign_keys = OFF") tables.each { |table| Rubee::SequelObject::DB.drop_table(table) } Rubee::SequelObject::DB.run("PRAGMA foreign_keys = ON") else # Fallback — try topological drop order by retrying failures remaining = tables.dup max_attempts = tables.size attempts = 0 while remaining.any? && attempts < max_attempts failed = [] remaining.each do |table| Rubee::SequelObject::DB.drop_table(table) rescue StandardError failed << table end remaining = failed attempts += 1 end end color_puts("These tables have been dropped for the #{ENV['RACK_ENV']} env:", color: :cyan) color_puts(tables, color: :gray) end |
.init(_argv) ⇒ Object
32 33 34 |
# File 'lib/rubee/cli/db.rb', line 32 def init(_argv) ensure_database_exists(Rubee::Configuration.get_database_url) end |
.run(argv) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubee/cli/db.rb', line 11 def run(argv) _, file_name = argv[1]&.split(':') file_names = if file_name == 'all' lib = Rubee::PROJECT_NAME == 'rubee' ? '/lib' : '' Dir.glob(".#{lib}/db/*.rb").sort.map do |file| File.basename(file, '.rb') end.reject { |file| file == 'structure' } else [file_name] end file_names.each do |file| color_puts("Run #{file} file for #{ENV['RACK_ENV']} env", color: :cyan) Object.const_get(file.gsub(/^\d+_/, '').split('_').map(&:capitalize).join).new.call end color_puts("Migration for #{file_name} completed", color: :green) unless Rubee::PROJECT_NAME == 'rubee' color_puts('Regenerate schema file', color: :cyan) generate_structure end end |
.schema(argv) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 144 145 146 147 148 149 |
# File 'lib/rubee/cli/db.rb', line 82 def schema(argv) target_table_hash = argv[2] ? { argv[2].to_sym => STRUCTURE[argv[2].to_sym] } : nil (target_table_hash || STRUCTURE).each do |table_name, table_def| unless Rubee::SequelObject::DB.table_exists?(table_name.to_sym) color_puts("Table #{table_name} not found", color: :red) next end columns = table_def[:columns] foreign_keys = table_def[:foreign_keys] || [] # Table header color_puts( "--- #{table_name}", color: :cyan, style: :bold ) # Columns columns.each do |column_name, | parts = [] # column name parts << "- #{column_name}" # PK parts << "(PK)" if [:primary_key] # type parts << "type (#{[:db_type]})" if [:db_type] # nullable parts << "nullable" if [:allow_null] line = parts.join(", ") color_puts( line, color: [:primary_key] ? :yellow : :gray ) end # Foreign keys if foreign_keys.any? puts color_puts(" Foreign keys:", color: :magenta, style: :bold) foreign_keys.each do |fk| cols = Array(fk[:columns]).join(", ") ref_table = fk.dig(:references, :table) || fk[:table] ref_cols = Array(fk.dig(:references, :columns)) || fk["id"] fk_line = " - #{cols} → #{ref_table}(#{ref_cols.join(', ')})" fk_line += " on delete #{fk[:on_delete]}" if fk[:on_delete] fk_line += " on update #{fk[:on_update]}" if fk[:on_update] color_puts(fk_line, color: :gray) end end puts end end |
.structure(_argv) ⇒ Object
36 37 38 |
# File 'lib/rubee/cli/db.rb', line 36 def structure(_argv) generate_structure end |
.truncate_tables(_argv) ⇒ Object
76 77 78 79 80 |
# File 'lib/rubee/cli/db.rb', line 76 def truncate_tables(_argv) out = Rubee::SequelObject::DB.tables.each { |table| Rubee::SequelObject::DB[table].truncate } color_puts("These tables have been truncated for the #{ENV['RACK_ENV']} env:", color: :cyan) color_puts(out, color: :gray) end |