From 8af14faa49deed681fa835dd6bd3f77316b44a55 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 15 Jun 2010 10:07:39 +0100 Subject: [PATCH] Make database offline mode work --- config/environment.rb | 8 +- config/initializers/abstract_adapter.rb | 42 ++++---- config/initializers/postgresql_adapter.rb | 100 +++++++++--------- config/initializers/query_cache.rb | 16 +-- config/initializers/sql_session_store.rb | 4 +- vendor/plugins/deadlock_retry/init.rb | 5 +- vendor/plugins/file_column/init.rb | 11 +- .../validates_email_format_of/rails/init.rb | 4 +- 8 files changed, 106 insertions(+), 84 deletions(-) diff --git a/config/environment.rb b/config/environment.rb index 0a67be745..55c2df4ae 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -51,7 +51,9 @@ Rails::Initializer.run do |config| # config.gem "bj" # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" # config.gem "aws-s3", :lib => "aws/s3" - config.gem 'composite_primary_keys', :version => '2.2.2' + unless OSM_STATUS == :database_offline + config.gem 'composite_primary_keys', :version => '2.2.2' + end config.gem 'libxml-ruby', :version => '>= 1.1.1', :lib => 'libxml' config.gem 'rmagick', :lib => 'RMagick' config.gem 'oauth', :version => '>= 0.3.6' @@ -86,7 +88,9 @@ Rails::Initializer.run do |config| # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information # (create the session table with 'rake db:sessions:create') - config.action_controller.session_store = :sql_session_store + unless OSM_STATUS == :database_offline + config.action_controller.session_store = :sql_session_store + end # Use SQL instead of Active Record's schema dumper when creating the test database. # This is necessary if your schema can't be completely dumped by the schema dumper, diff --git a/config/initializers/abstract_adapter.rb b/config/initializers/abstract_adapter.rb index 5cc24d25c..9042823c2 100644 --- a/config/initializers/abstract_adapter.rb +++ b/config/initializers/abstract_adapter.rb @@ -1,24 +1,26 @@ -module ActiveRecord - module ConnectionAdapters - class AbstractAdapter - protected - alias_method :old_log, :log - - def log(sql, name) - if block_given? - old_log(sql, name) do - yield +if defined?(ActionRecord::ConnectionAdaptors::AbstractAdaptor) + module ActiveRecord + module ConnectionAdapters + class AbstractAdapter + protected + alias_method :old_log, :log + + def log(sql, name) + if block_given? + old_log(sql, name) do + yield + end + else + old_log(sql, name) + end + rescue ActiveRecord::StatementInvalid => ex + if ex.message =~ /^OSM::APITimeoutError: / + raise OSM::APITimeoutError.new + elsif ex.message =~ /^Timeout::Error: / + raise Timeout::Error.new("time's up!") + else + raise end - else - old_log(sql, name) - end - rescue ActiveRecord::StatementInvalid => ex - if ex.message =~ /^OSM::APITimeoutError: / - raise OSM::APITimeoutError.new - elsif ex.message =~ /^Timeout::Error: / - raise Timeout::Error.new("time's up!") - else - raise end end end diff --git a/config/initializers/postgresql_adapter.rb b/config/initializers/postgresql_adapter.rb index c5a69a1d9..b9adf38bb 100644 --- a/config/initializers/postgresql_adapter.rb +++ b/config/initializers/postgresql_adapter.rb @@ -1,54 +1,56 @@ -module ActiveRecord - module ConnectionAdapters - class PostgreSQLAdapter - def pk_and_sequence_for(table) - # First try looking for a sequence with a dependency on the - # given table's primary key. - result = query(<<-end_sql, 'PK and serial sequence')[0] - SELECT attr.attname, seq.relname - FROM pg_class seq, - pg_attribute attr, - pg_depend dep, - pg_namespace name, - pg_constraint cons - WHERE seq.oid = dep.objid - AND seq.relkind = 'S' - AND attr.attrelid = dep.refobjid - AND attr.attnum = dep.refobjsubid - AND attr.attrelid = cons.conrelid - AND attr.attnum = cons.conkey[1] - AND cons.contype = 'p' - AND dep.classid = '"pg_class"'::regclass - AND dep.refclassid = '"pg_class"'::regclass - AND dep.refobjid = '#{quote_table_name(table)}'::regclass - end_sql - - if result.nil? or result.empty? - # If that fails, try parsing the primary key's default value. - # Support the 7.x and 8.0 nextval('foo'::text) as well as - # the 8.1+ nextval('foo'::regclass). - result = query(<<-end_sql, 'PK and custom sequence')[0] - SELECT attr.attname, - CASE - WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN - substr(split_part(def.adsrc, '''', 2), - strpos(split_part(def.adsrc, '''', 2), '.')+1) - ELSE split_part(def.adsrc, '''', 2) - END - FROM pg_class t - JOIN pg_attribute attr ON (t.oid = attrelid) - JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum) - JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1]) - WHERE t.oid = '#{quote_table_name(table)}'::regclass - AND cons.contype = 'p' - AND def.adsrc ~* 'nextval' +if defined?(ActionRecord::ConnectionAdaptors::PostgreSQLAdaptor) + module ActiveRecord + module ConnectionAdapters + class PostgreSQLAdapter + def pk_and_sequence_for(table) + # First try looking for a sequence with a dependency on the + # given table's primary key. + result = query(<<-end_sql, 'PK and serial sequence')[0] + SELECT attr.attname, seq.relname + FROM pg_class seq, + pg_attribute attr, + pg_depend dep, + pg_namespace name, + pg_constraint cons + WHERE seq.oid = dep.objid + AND seq.relkind = 'S' + AND attr.attrelid = dep.refobjid + AND attr.attnum = dep.refobjsubid + AND attr.attrelid = cons.conrelid + AND attr.attnum = cons.conkey[1] + AND cons.contype = 'p' + AND dep.classid = '"pg_class"'::regclass + AND dep.refclassid = '"pg_class"'::regclass + AND dep.refobjid = '#{quote_table_name(table)}'::regclass end_sql + + if result.nil? or result.empty? + # If that fails, try parsing the primary key's default value. + # Support the 7.x and 8.0 nextval('foo'::text) as well as + # the 8.1+ nextval('foo'::regclass). + result = query(<<-end_sql, 'PK and custom sequence')[0] + SELECT attr.attname, + CASE + WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN + substr(split_part(def.adsrc, '''', 2), + strpos(split_part(def.adsrc, '''', 2), '.')+1) + ELSE split_part(def.adsrc, '''', 2) + END + FROM pg_class t + JOIN pg_attribute attr ON (t.oid = attrelid) + JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum) + JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1]) + WHERE t.oid = '#{quote_table_name(table)}'::regclass + AND cons.contype = 'p' + AND def.adsrc ~* 'nextval' + end_sql + end + + # [primary_key, sequence] + [result.first, result.last] + rescue + nil end - - # [primary_key, sequence] - [result.first, result.last] - rescue - nil end end end diff --git a/config/initializers/query_cache.rb b/config/initializers/query_cache.rb index 83d42feec..92372faf3 100644 --- a/config/initializers/query_cache.rb +++ b/config/initializers/query_cache.rb @@ -1,10 +1,12 @@ -module ActiveRecord - module ConnectionAdapters - module QueryCache - private - def cache_sql(sql) - yield - end +if defined?(ActionRecord::ConnectionAdaptors::QueryCache) + module ActiveRecord + module ConnectionAdapters + module QueryCache + private + def cache_sql(sql) + yield + end + end end end end diff --git a/config/initializers/sql_session_store.rb b/config/initializers/sql_session_store.rb index 986037afe..2b9543fc2 100644 --- a/config/initializers/sql_session_store.rb +++ b/config/initializers/sql_session_store.rb @@ -4,4 +4,6 @@ adapter = Rails.configuration.database_configuration[environment]["adapter"] session_class = adapter + "_session" # Configure SqlSessionStore -SqlSessionStore.session_class = session_class.camelize.constantize +unless OSM_STATUS == :database_offline + SqlSessionStore.session_class = session_class.camelize.constantize +end diff --git a/vendor/plugins/deadlock_retry/init.rb b/vendor/plugins/deadlock_retry/init.rb index e090f68af..9c00ea234 100644 --- a/vendor/plugins/deadlock_retry/init.rb +++ b/vendor/plugins/deadlock_retry/init.rb @@ -1,2 +1,5 @@ require 'deadlock_retry' -ActiveRecord::Base.send :include, DeadlockRetry + +if defined?(ActionRecord::Base) + ActiveRecord::Base.send :include, DeadlockRetry +end diff --git a/vendor/plugins/file_column/init.rb b/vendor/plugins/file_column/init.rb index d31ef1b9c..6ecbba5f6 100644 --- a/vendor/plugins/file_column/init.rb +++ b/vendor/plugins/file_column/init.rb @@ -8,6 +8,11 @@ require 'file_column_helper' require 'validations' require 'test_case' -ActiveRecord::Base.send(:include, FileColumn) -ActionView::Base.send(:include, FileColumnHelper) -ActiveRecord::Base.send(:include, FileColumn::Validations) \ No newline at end of file +if defined?(ActionRecord::Base) + ActiveRecord::Base.send(:include, FileColumn) + ActiveRecord::Base.send(:include, FileColumn::Validations) +end + +if defined?(ActionView::Base) + ActionView::Base.send(:include, FileColumnHelper) +end diff --git a/vendor/plugins/validates_email_format_of/rails/init.rb b/vendor/plugins/validates_email_format_of/rails/init.rb index bdc8b5794..bf4d5e80a 100644 --- a/vendor/plugins/validates_email_format_of/rails/init.rb +++ b/vendor/plugins/validates_email_format_of/rails/init.rb @@ -1 +1,3 @@ -require 'validates_email_format_of' +if defined?(ActionRecord::Base) + require 'validates_email_format_of' +end -- 2.39.5