Class: Pgbus::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, MigrationPath
Defined in:
lib/generators/pgbus/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#configure_active_jobObject



43
44
45
46
47
48
49
50
51
# File 'lib/generators/pgbus/install_generator.rb', line 43

def configure_active_job
  application_config = "config.active_job.queue_adapter = :pgbus"

  return unless File.exist?(File.join(destination_root, "config", "application.rb"))

  inject_into_file "config/application.rb",
                   "\n    #{application_config}\n",
                   after: "class Application < Rails::Application\n"
end

#configure_separate_databaseObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/generators/pgbus/install_generator.rb', line 53

def configure_separate_database
  return unless separate_database?

  db_config = <<~YAML

    # Pgbus separate database (added by pgbus:install --database=#{database_name})
    # Uncomment and configure for your environment:
    # #{database_name}:
    #   primary:
    #     <<: *default
    #     database: #{Rails.application.class.module_parent_name.underscore}_#{database_name}_<%= Rails.env %>
    #     migrations_paths: db/pgbus_migrate
  YAML

  say ""
  say "Separate database mode enabled!", :yellow
  say "Add the following to your config/database.yml:", :yellow
  say db_config, :cyan
  say ""
  say "Then add to config/application.rb or an initializer:", :yellow
  say "  Pgbus.configure { |c| c.connects_to = { database: { writing: :#{database_name} } } }", :cyan
  say ""
end

#create_binstubObject



38
39
40
41
# File 'lib/generators/pgbus/install_generator.rb', line 38

def create_binstub
  template "pgbus_binstub.erb", "bin/pgbus"
  chmod "bin/pgbus", 0o755
end

#create_config_fileObject



34
35
36
# File 'lib/generators/pgbus/install_generator.rb', line 34

def create_config_file
  template "pgbus.yml.erb", "config/pgbus.yml"
end

#create_migration_fileObject



29
30
31
32
# File 'lib/generators/pgbus/install_generator.rb', line 29

def create_migration_file
  migration_template "migration.rb.erb",
                     File.join(pgbus_migrate_path, "create_pgbus_tables.rb")
end

#display_post_installObject



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/install_generator.rb', line 77

def display_post_install
  say ""
  say "Pgbus installed successfully!", :green
  say ""
  say "PGMQ schema mode: #{pgmq_schema_mode}", :yellow
  case pgmq_schema_mode
  when "auto"
    say "  The migration will try the pgmq extension first."
    say "  If unavailable, it falls back to embedded SQL (no extension needed)."
  when "extension"
    say "  The migration requires the pgmq PostgreSQL extension."
    say "  Install it: CREATE EXTENSION pgmq;"
  when "embedded"
    say "  The migration uses embedded SQL — no pgmq extension needed."
    say "  PGMQ #{Pgbus::PgmqSchema.latest_version} schema will be created directly."
  end

  if separate_database?
    say ""
    say "Migrations path: db/pgbus_migrate/", :yellow
    say "Schema dump: db/pgbus_schema.rb", :yellow
  end

  say ""
  say "Next steps:"
  say "  1. Run: rails db:migrate#{":#{database_name}" if separate_database?}"
  say "  2. Edit config/pgbus.yml to configure workers"
  say "  3. Start processing: bin/pgbus start"
  say ""
end