Class: Pgbus::Generators::UpdateGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/pgbus/update_generator.rb

Overview

Upgrade command with two independent jobs:

1. Config conversion: if config/pgbus.yml exists, convert it to
   config/initializers/pgbus.rb using the modern Ruby DSL. Skip
   silently if the initializer already exists or the YAML is
   absent — safe to re-run.

2. Migration detection: inspect the live database and add any
   missing pgbus migrations to db/migrate (or db/pgbus_migrate
   if a separate database is configured). Invokes each matching
   sub-generator in-process via Thor's invoke, so this mirrors
   what the user would get running each generator by hand.

Usage:

bin/rails generate pgbus:update
bin/rails generate pgbus:update --dry-run
bin/rails generate pgbus:update --skip-config
bin/rails generate pgbus:update --skip-migrations
bin/rails generate pgbus:update --database=pgbus
bin/rails generate pgbus:update --quiet

Instance Method Summary collapse

Instance Method Details

#convert_yaml_if_presentObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/pgbus/update_generator.rb', line 70

def convert_yaml_if_present
  return if options[:skip_config]

  source_path = File.expand_path(options[:source], destination_root)
  destination_path = File.expand_path(options[:destination], destination_root)

  unless File.exist?(source_path)
    log "YAML config not found at #{options[:source]}; skipping config conversion."
    return
  end

  if File.exist?(destination_path)
    log "Initializer already exists at #{options[:destination]}; skipping config conversion."
    return
  end

  ruby_source = load_and_convert(source_path)
  if options[:dry_run]
    log_change "[dry-run] would create #{options[:destination]}"
  else
    create_file destination_path, ruby_source
  end
end

#detect_and_install_missing_migrationsObject



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
150
151
152
153
154
# File 'lib/generators/pgbus/update_generator.rb', line 94

def detect_and_install_missing_migrations
  return if options[:skip_migrations]

  unless active_record_available?
    log "ActiveRecord not loaded — skipping migration detection. Run this generator from a Rails app."
    return
  end

  connection = resolve_connection
  unless connection
    log "No ActiveRecord connection available — skipping migration detection."
    return
  end

  detector = MigrationDetector.new(connection)
  missing = detector.missing_migrations

  if missing.empty?
    log "Database schema is up to date — no migrations needed."
    return
  end

  if missing == [MigrationDetector::FRESH_INSTALL]
    say ""
    say "Database looks empty of pgbus tables — this is a fresh install.", :yellow
    say "Run `rails generate pgbus:install` instead of `pgbus:update`.", :yellow
    say ""
    return
  end

  database_name = options[:database] || detected_database_name
  log "Auto-detected separate database: #{database_name}" if options[:database].nil? && database_name

  log "Found #{missing.size} missing migration(s):"
  missing.each do |key|
    description = MigrationDetector::DESCRIPTIONS[key] || key.to_s
    log "  - #{key}: #{description}"
  end

  # Two loops on purpose: print the full plan first so operators
  # see what's coming, then execute. Combining would interleave
  # "  - add_presence: foo" with "Invoking pgbus:add_presence..."
  # which hides the shape of the upgrade from the reader.
  missing.each do |key| # rubocop:disable Style/CombinableLoops
    generator = MigrationDetector::GENERATOR_MAP[key]
    unless generator
      say "  !  no generator mapped for #{key}, skipping", :red
      next
    end

    if options[:dry_run]
      log_change "[dry-run] would invoke #{generator}#{" --database=#{database_name}" if database_name}"
      next
    end

    invoke_args = []
    invoke_args << "--database=#{database_name}" if database_name
    log "Invoking #{generator}#{" --database=#{database_name}" if database_name}..."
    invoke generator, invoke_args
  end
end

#display_post_installObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/generators/pgbus/update_generator.rb', line 156

def display_post_install
  return if options[:quiet]

  say ""
  say "Pgbus update complete.", :green
  say ""
  if options[:dry_run]
    say "Dry-run: no files were created.", :yellow
  else
    say "Next steps:"
    say "  1. Review the generated migration files in db/migrate (or db/pgbus_migrate)"
    say "  2. Run: rails db:migrate#{":#{effective_database_name}" if effective_database_name}"
    say "  3. Restart pgbus: bin/pgbus start"
  end
  say ""
end