Make database offline mode work
This commit is contained in:
parent
f980e7e4a6
commit
8af14faa49
8 changed files with 106 additions and 84 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue