Class: Localtower::Tools
- Inherits:
-
Object
- Object
- Localtower::Tools
- Defined in:
- lib/localtower/tools.rb
Defined Under Namespace
Classes: ThorTools
Class Method Summary collapse
- .all_columns ⇒ Object
-
.create_log ⇒ Object
PRIVATE ==============.
- .force_reload! ⇒ Object
- .indexes_for_model(model) ⇒ Object
- .indexes_for_model_and_attribute(model, column_name) ⇒ Object
- .last_migration_pending ⇒ Object
- .line_for_attribute(attribute) ⇒ Object
- .log(str) ⇒ Object
- .log_file ⇒ Object
- .models ⇒ Object
- .models_presented ⇒ Object
- .pending_migrations ⇒ Object
- .perform_cmd(cmd_str) ⇒ Object
- .perform_migration(str) ⇒ Object
- .perform_raw_cmd(cmd_str) ⇒ Object
Class Method Details
.all_columns ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/localtower/tools.rb', line 123 def all_columns models.map do |model| columns = begin model.columns rescue [] end columns.map { |column| column.name }.flatten end.flatten.uniq.sort end |
.create_log ⇒ Object
PRIVATE ==============
192 193 194 195 196 |
# File 'lib/localtower/tools.rb', line 192 def create_log return nil if File.exist?(self.log_file) File.open(self.log_file, 'w') { |f| f.write('') } end |
.force_reload! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/localtower/tools.rb', line 15 def force_reload! app_folders = Dir["#{Rails.root}/app/models/**/*.rb"] all_folders = (app_folders).flatten all_folders.each do |file| begin require file rescue Exception => e puts "Error loading: #{file}, #{e.message}" end end end |
.indexes_for_model(model) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/localtower/tools.rb', line 80 def indexes_for_model(model) indexes = ActiveRecord::Base.connection.indexes(model.table_name) return [] if indexes.empty? max_size = indexes.map { |index| index.name.size }.max + 1 list = [] indexes.each do |index| list << { unique: index.unique, name: index.name, columns: index.columns, max_size: max_size } end list end |
.indexes_for_model_and_attribute(model, column_name) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/localtower/tools.rb', line 100 def indexes_for_model_and_attribute(model, column_name) indexes = self.indexes_for_model(model) list = [] done = [] indexes.each do |index| conditions = index[:columns].include?(column_name.to_s) and not done.include?(index[:name]) next unless conditions infos = { unique: index[:unique], name: index[:name], columns: index[:columns], max_size: index[:max_size] } done << column_name list << infos end list end |
.last_migration_pending ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/localtower/tools.rb', line 173 def last_migration_pending last_not_run_migration = Localtower::Status.new.run.select {|i| i['status'] == :todo}.sort { |a, b| a['time'] <=> b['time'] }.last return nil if last_not_run_migration.blank? return nil unless File.exist?(last_not_run_migration["file_full_path"]) last_not_run_migration["file_full_path"] end |
.line_for_attribute(attribute) ⇒ Object
187 188 189 |
# File 'lib/localtower/tools.rb', line 187 def line_for_attribute(attribute) (File.read(last_migration_pending).match(/^\s*t\.(.*)\s*:#{attribute}.*$/) || []) end |
.log(str) ⇒ Object
163 164 165 166 167 |
# File 'lib/localtower/tools.rb', line 163 def log(str) self.create_log log_str = "[#{Time.now}] - #{str}\n" File.open(self.log_file, 'a') { |f| f.write(log_str) } end |
.log_file ⇒ Object
169 170 171 |
# File 'lib/localtower/tools.rb', line 169 def log_file @log_file ||= Rails.root.join('log', 'localtower.log') end |
.models ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/localtower/tools.rb', line 28 def models self.force_reload! root_klass = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base root_klass.subclasses - [ActiveRecord::SchemaMigration] end |
.models_presented ⇒ Object
36 37 38 39 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 75 76 77 78 |
# File 'lib/localtower/tools.rb', line 36 def models_presented self.force_reload! list = [] self.models.each do |model| attributes_list = [] # Using this trick because the model might exist but not migrated yet columns_hash = begin model.columns_hash rescue [] end columns_hash.each do |_k, v| belongs_to = nil if v.name.strip =~ /\_id$/ belongs_to = v.name.strip.gsub(/_id$/, "").singularize.camelize end attributes_list << { 'name' => v.name.strip, 'type' => v.sql_type.strip, 'belongs_to' => belongs_to, 'type_clean' => v.sql_type.split(' ')[0].strip, 'primary' => (v.respond_to?(:primary) ? v.primary : false), 'index' => self.indexes_for_model_and_attribute(model, v.name), } end list << { name: model.name, underscore: model.name.underscore, table_name: model.table_name, attributes_list: attributes_list, } end list end |
.pending_migrations ⇒ Object
183 184 185 |
# File 'lib/localtower/tools.rb', line 183 def pending_migrations Localtower::Status.new.run.select {|i| i['status'] == :todo} end |
.perform_cmd(cmd_str) ⇒ Object
139 140 141 |
# File 'lib/localtower/tools.rb', line 139 def perform_cmd(cmd_str) self.perform_raw_cmd("bundle exec #{cmd_str}") end |
.perform_migration(str) ⇒ Object
135 136 137 |
# File 'lib/localtower/tools.rb', line 135 def perform_migration(str) self.perform_cmd("rails g migration #{str}") end |
.perform_raw_cmd(cmd_str) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/localtower/tools.rb', line 143 def perform_raw_cmd(cmd_str) root_dir = ::Rails.root cmd = "cd \"#{root_dir}\" && #{cmd_str}" cmd = cmd.strip self.log("DOING...: #{cmd}") before_sec = Time.now output = `#{cmd}` after_sec = Time.now self.log(output) sec = after_sec - before_sec self.log("DONE: #{cmd} in #{sec} sec") output end |