Permalink
Browse files

Add support schema cache dump and load.

  • Loading branch information...
kennyj committed Feb 29, 2012
1 parent 8cecb47 commit 5ca4fc95818047108e69e22d200e7a4a22969477
@@ -6,21 +6,12 @@ class SchemaCache
def initialize(conn)
@connection = conn
@tables = {}
@columns = Hash.new do |h, table_name|
h[table_name] = conn.columns(table_name)
end
@columns_hash = Hash.new do |h, table_name|
h[table_name] = Hash[columns[table_name].map { |col|
[col.name, col]
}]
end
@primary_keys = Hash.new do |h, table_name|
h[table_name] = table_exists?(table_name) ? conn.primary_key(table_name) : nil
end
@columns = {}
@columns_hash = {}
@primary_keys = {}
@tables = {}
prepare_default_proc
end
# A cached lookup for table existence.
@@ -45,6 +36,35 @@ def clear_table_cache!(table_name)
@primary_keys.delete table_name
@tables.delete table_name
end
def marshal_dump
[:@columns, :@columns_hash, :@primary_keys, :@tables].map do |val|
self.instance_variable_get(val).inject({}) { |h, v| h[v[0]] = v[1]; h }
end
end
def marshal_load(array)
@columns, @columns_hash, @primary_keys, @tables = array
prepare_default_proc
end
private
def prepare_default_proc
@columns.default_proc = Proc.new do |h, table_name|
h[table_name] = connection.columns(table_name)
end
@columns_hash.default_proc = Proc.new do |h, table_name|
h[table_name] = Hash[columns[table_name].map { |col|
[col.name, col]
}]
end
@primary_keys.default_proc = Proc.new do |h, table_name|
h[table_name] = table_exists?(table_name) ? connection.primary_key(table_name) : nil
end
end
end
end
end
@@ -39,6 +39,21 @@ def test_clearing
assert_equal 0, @cache.tables.size
assert_equal 0, @cache.primary_keys.size
end
def test_dump_and_load
@cache.columns['posts']
@cache.columns_hash['posts']
@cache.tables['posts']
@cache.primary_keys['posts']
@cache = Marshal.load(Marshal.dump(@cache))
assert_equal 12, @cache.columns['posts'].size
assert_equal 12, @cache.columns_hash['posts'].size
assert @cache.tables['posts']
assert_equal 'id', @cache.primary_keys['posts']
end
end
end
end

0 comments on commit 5ca4fc9

Please sign in to comment.