Class: Exwiw::SchemaGenerator
- Inherits:
-
Object
- Object
- Exwiw::SchemaGenerator
- Defined in:
- lib/exwiw/schema_generator.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#build_table_groups ⇒ Object
Returns a Hash keyed by the database name.
-
#build_tables ⇒ Object
Backwards-compatible flat list of all table configs.
- #generate! ⇒ Object
-
#initialize(models:, output_dir:) ⇒ SchemaGenerator
constructor
A new instance of SchemaGenerator.
- #write_files(dir, tables) ⇒ Object
- #write_groups(groups) ⇒ Object
Constructor Details
#initialize(models:, output_dir:) ⇒ SchemaGenerator
Returns a new instance of SchemaGenerator.
13 14 15 16 |
# File 'lib/exwiw/schema_generator.rb', line 13 def initialize(models:, output_dir:) @models = models @output_dir = output_dir end |
Class Method Details
.from_rails_application(output_dir:) ⇒ Object
8 9 10 11 |
# File 'lib/exwiw/schema_generator.rb', line 8 def self.from_rails_application(output_dir:) Rails.application.eager_load! new(models: ActiveRecord::Base.descendants, output_dir: output_dir) end |
Instance Method Details
#build_table_groups ⇒ Object
Returns a Hash keyed by the database name.
-
Single-database setup: the only key is ‘nil`, signalling that the table configs should be written flat into `output_dir` (backwards compatible).
-
Multi-database setup (Rails ‘connects_to`): one key per database (`connection_db_config.name`, e.g. “primary” / “analytics”), each mapping to that database’s table configs. They are written into ‘output_dir/<db_name>/`.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/exwiw/schema_generator.rb', line 32 def build_table_groups models = concrete_models grouped = models.group_by { |model| database_name_for(model) } if grouped.size <= 1 conn = models.empty? ? ActiveRecord::Base.connection : models.first.connection return { nil => build_tables_for(models, conn) } end grouped.each_with_object({}) do |(db_name, group_models), result| conn = group_models.first.connection result[db_name] = build_tables_for(group_models, conn) end end |
#build_tables ⇒ Object
Backwards-compatible flat list of all table configs. Only meaningful for a single-database setup; for multi-database setups prefer ‘#build_table_groups` so the database association is preserved.
50 51 52 |
# File 'lib/exwiw/schema_generator.rb', line 50 def build_tables build_table_groups.values.flatten end |
#generate! ⇒ Object
18 19 20 21 22 |
# File 'lib/exwiw/schema_generator.rb', line 18 def generate! groups = build_table_groups write_groups(groups) groups end |
#write_files(dir, tables) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/exwiw/schema_generator.rb', line 61 def write_files(dir, tables) FileUtils.mkdir_p(dir) tables.each do |table| path = File.join(dir, "#{table.name}.json") config_to_write = if File.exist?(path) TableConfig.from(JSON.parse(File.read(path))).merge(table) else table end File.write(path, JSON.pretty_generate(config_to_write.to_hash) + "\n") end end |
#write_groups(groups) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/exwiw/schema_generator.rb', line 54 def write_groups(groups) groups.each do |db_name, tables| dir = db_name.nil? ? @output_dir : File.join(@output_dir, db_name) write_files(dir, tables) end end |