Class: Pgbus::Generators::UpdateGenerator

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

Overview

Upgrade command: 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-migrations
bin/rails generate pgbus:update --database=pgbus
bin/rails generate pgbus:update --quiet

Instance Method Summary collapse

Instance Method Details

#detect_and_install_missing_migrationsObject



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
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
104
105
106
# File 'lib/generators/pgbus/update_generator.rb', line 46

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/generators/pgbus/update_generator.rb', line 108

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