Class: RailsAgents::DatabaseDiscovery
- Inherits:
-
Object
- Object
- RailsAgents::DatabaseDiscovery
- Defined in:
- lib/rails_agents/database_discovery.rb
Overview
Discovers SQL (ActiveRecord) and document (Mongoid) databases from the host Rails app so install/agent generators and Tool Bridge know what to expose.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- CONFIG_RELATIVE =
"config/rails_agents_database.yml"
Class Method Summary collapse
- .adapters_from_database_yml(path) ⇒ Object
- .attached_by_default?(root = nil) ⇒ Boolean
- .config_path(root = nil) ⇒ Object
- .default_root ⇒ Object
-
.discover(root: nil, live: true) ⇒ Object
Static + live discovery.
- .discover!(root: nil, live: true) ⇒ Object
- .live_collections ⇒ Object
- .live_primary_adapter ⇒ Object
- .live_tables ⇒ Object
- .load(root = nil) ⇒ Object
- .mongoid_capable?(root = nil) ⇒ Boolean
- .normalize_adapter(adapter) ⇒ Object
- .primary_adapter(root = nil) ⇒ Object
- .sql_capable?(root = nil) ⇒ Boolean
- .write!(result, root: nil) ⇒ Object
Class Method Details
.adapters_from_database_yml(path) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rails_agents/database_discovery.rb', line 139 def adapters_from_database_yml(path) return [] unless path.file? raw = YAML.safe_load( ERB.new(path.read).result, permitted_classes: [Symbol], aliases: true ) || {} env = defined?(Rails) ? Rails.env.to_s : (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development") entry = raw[env] || raw["development"] || raw.values.find { |v| v.is_a?(Hash) } return [] unless entry.is_a?(Hash) # Multi-db: primary / animals / etc. if entry.values.any? { |v| v.is_a?(Hash) && (v["adapter"] || v[:adapter]) } entry.filter_map do |_name, cfg| next unless cfg.is_a?(Hash) normalize_adapter(cfg["adapter"] || cfg[:adapter]) end else [normalize_adapter(entry["adapter"] || entry[:adapter])].compact end rescue StandardError [] end |
.attached_by_default?(root = nil) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rails_agents/database_discovery.rb', line 43 def attached_by_default?(root = nil) data = load(root) return true if data.nil? # optimistic default until install discovery runs data["default_attach"] != false && ( data["active_record"] == true || data["mongoid"] == true || Array(data["adapters"]).any? || Array(data["engines"]).any? ) end |
.config_path(root = nil) ⇒ Object
28 29 30 |
# File 'lib/rails_agents/database_discovery.rb', line 28 def config_path(root = nil) Pathname.new(root || default_root).join(CONFIG_RELATIVE) end |
.default_root ⇒ Object
135 136 137 |
# File 'lib/rails_agents/database_discovery.rb', line 135 def default_root defined?(Rails) && Rails.respond_to?(:root) && Rails.root ? Rails.root : Pathname.pwd end |
.discover(root: nil, live: true) ⇒ Object
Static + live discovery. Safe to call from generators and runtime.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails_agents/database_discovery.rb', line 75 def discover(root: nil, live: true) root = Pathname.new(root || default_root) config_files = %w[config/database.yml config/mongoid.yml] .select { |rel| root.join(rel).exist? } adapters = adapters_from_database_yml(root.join("config/database.yml")) has_database_yml = root.join("config/database.yml").exist? has_mongoid_yml = root.join("config/mongoid.yml").exist? mongoid = has_mongoid_yml || (live && defined?(Mongoid)) active_record = has_database_yml || (live && defined?(ActiveRecord::Base)) tables = [] collections = [] if live tables = live_tables if defined?(ActiveRecord::Base) collections = live_collections if defined?(Mongoid) adapters = [live_primary_adapter].compact if adapters.empty? && defined?(ActiveRecord::Base) end Result.new( active_record: !!active_record, mongoid: !!mongoid, adapters: adapters.uniq, config_files: config_files, tables: tables, collections: collections, default_attach: !!(active_record || mongoid) ) end |
.discover!(root: nil, live: true) ⇒ Object
105 106 107 108 109 |
# File 'lib/rails_agents/database_discovery.rb', line 105 def discover!(root: nil, live: true) result = discover(root: root, live: live) write!(result, root: root) result end |
.live_collections ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/rails_agents/database_discovery.rb', line 194 def live_collections return [] unless defined?(Mongoid) && Mongoid.respond_to?(:default_client) Mongoid.default_client.database.collection_names .reject { |name| name.start_with?("system.") } .sort rescue StandardError [] end |
.live_primary_adapter ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/rails_agents/database_discovery.rb', line 177 def live_primary_adapter return nil unless defined?(ActiveRecord::Base) cfg = ActiveRecord::Base.connection_db_config normalize_adapter(cfg.adapter) rescue StandardError nil end |
.live_tables ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/rails_agents/database_discovery.rb', line 186 def live_tables ActiveRecord::Base.connection.tables .reject { |t| t.start_with?("ar_", "solid_", "schema_migrations", "ar_internal_metadata") } .sort rescue StandardError [] end |
.load(root = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rails_agents/database_discovery.rb', line 32 def load(root = nil) path = config_path(root) return nil unless path.file? data = YAML.safe_load(path.read, permitted_classes: [Time, Date, Symbol], aliases: true) || {} data = data["rails_agents_database"] if data.is_a?(Hash) && data.key?("rails_agents_database") data.is_a?(Hash) ? data : nil rescue Psych::Exception nil end |
.mongoid_capable?(root = nil) ⇒ Boolean
62 63 64 65 66 67 |
# File 'lib/rails_agents/database_discovery.rb', line 62 def mongoid_capable?(root = nil) data = load(root) return defined?(Mongoid) if data.nil? data["mongoid"] == true || Array(data["engines"]).include?("mongoid") end |
.normalize_adapter(adapter) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rails_agents/database_discovery.rb', line 165 def normalize_adapter(adapter) value = adapter.to_s.downcase return nil if value.empty? return "postgresql" if value.match?(/postgres|postgis|cockroach/) return "mysql" if value.match?(/mysql|trilogy|maria/) return "sqlite" if value.include?("sqlite") return "sqlserver" if value.include?("sqlserver") return "oracle" if value.include?("oracle") value end |
.primary_adapter(root = nil) ⇒ Object
69 70 71 72 |
# File 'lib/rails_agents/database_discovery.rb', line 69 def primary_adapter(root = nil) data = load(root) Array(data && data["adapters"]).first || live_primary_adapter end |
.sql_capable?(root = nil) ⇒ Boolean
55 56 57 58 59 60 |
# File 'lib/rails_agents/database_discovery.rb', line 55 def sql_capable?(root = nil) data = load(root) return defined?(ActiveRecord::Base) if data.nil? data["active_record"] == true || Array(data["engines"]).include?("active_record") end |
.write!(result, root: nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rails_agents/database_discovery.rb', line 111 def write!(result, root: nil) path = config_path(root) FileUtils.mkdir_p(path.dirname) payload = { "version" => 1, "discovered_at" => Time.now.utc.iso8601, "default_attach" => result.default_attach, "active_record" => result.active_record, "mongoid" => result.mongoid, "engines" => [ (result.active_record ? "active_record" : nil), (result.mongoid ? "mongoid" : nil) ].compact, "adapters" => result.adapters, "config_files" => result.config_files, "table_count" => result.tables.size, "collection_count" => result.collections.size, "tables_sample" => result.tables.first(40), "collections_sample" => result.collections.first(40) } path.write(YAML.dump(payload)) path end |