]> git.openstreetmap.org Git - rails.git/blob - config/application.rb
Fix database offline mode
[rails.git] / config / application.rb
1 require_relative "boot"
2
3 # Guard against deployments with old-style application.yml configurations
4 # Otherwise, admins might not be aware that they are now silently ignored
5 # and major problems could occur
6 # rubocop:disable Rails/Output, Rails/Exit
7 if File.exist?(File.expand_path("application.yml", __dir__))
8   puts "The config/application.yml file is no longer supported"
9   puts "Default settings are now found in config/settings.yml and you can override these in config/settings.local.yml"
10   puts "To prevent unexpected behaviour, please copy any custom settings to config/settings.local.yml"
11   puts " and then remove your config/application.yml file."
12   exit!
13 end
14 # rubocop:enable Rails/Output, Rails/Exit
15
16 # Set the STATUS constant from the environment, if it matches a recognized value
17 ALLOWED_STATUS = [
18   :online,            # online and operating normally
19   :api_readonly,      # site online but API in read-only mode
20   :api_offline,       # site online but API offline
21   :database_readonly, # database and site in read-only mode
22   :database_offline,  # database offline with site in emergency mode
23   :gpx_offline        # gpx storage offline
24 ].freeze
25
26 status = if ENV["STATUS"] && ALLOWED_STATUS.include?(ENV["STATUS"].to_sym)
27            ENV["STATUS"].to_sym
28          else
29            :online
30          end
31 Object.const_set("STATUS", status)
32
33 if STATUS == :database_offline
34   require "active_model/railtie"
35   require "active_job/railtie"
36   require "active_storage/engine"
37   require "action_controller/railtie"
38   require "action_mailer/railtie"
39   require "action_view/railtie"
40   require "action_cable/engine"
41   require "sprockets/railtie"
42   require "rails/test_unit/railtie"
43 else
44   require "rails/all"
45 end
46
47 # Require the gems listed in Gemfile, including any gems
48 # you've limited to :test, :development, or :production.
49 Bundler.require(*Rails.groups)
50
51 module OpenStreetMap
52   class Application < Rails::Application
53     # Initialize configuration defaults for originally generated Rails version.
54     config.load_defaults 5.2
55
56     # Settings in config/environments/* take precedence over those specified here.
57     # Application configuration can go into files in config/initializers
58     # -- all .rb files in that directory are automatically loaded after loading
59     # the framework and any gems in your application.
60
61     # Custom directories with classes and modules you want to be autoloadable.
62     config.autoload_paths += %W[#{config.root}/lib]
63
64     # This defaults to true from rails 5.0 but our code doesn't comply
65     # with it at all so we turn it off
66     config.active_record.belongs_to_required_by_default = false unless STATUS == :database_offline
67
68     # Use SQL instead of Active Record's schema dumper when creating the database.
69     # This is necessary if your schema can't be completely dumped by the schema dumper,
70     # like if you have constraints or database-specific column types
71     config.active_record.schema_format = :sql unless STATUS == :database_offline
72
73     # Don't eager load models when the database is offline
74     config.paths["app/models"].skip_eager_load! if STATUS == :database_offline
75
76     # Use memcached for caching if required
77     config.cache_store = :mem_cache_store, Settings.memcache_servers, { :namespace => "rails:cache" } if Settings.key?(:memcache_servers)
78
79     # Use logstash for logging if required
80     if Settings.key?(:logstash_path)
81       config.logstasher.enabled = true
82       config.logstasher.suppress_app_log = false
83       config.logstasher.logger_path = Settings.logstash_path
84       config.logstasher.log_controller_parameters = true
85     end
86   end
87 end