-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
active_record.rb
101 lines (84 loc) · 2.68 KB
/
active_record.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
require 'rails_admin/extensions/history/history'
require 'rails_admin/adapters/active_record'
DatabaseCleaner.strategy = :transaction
ActiveRecord::Base.connection.data_sources.each do |table|
ActiveRecord::Base.connection.drop_table(table)
end
def silence_stream(stream)
old_stream = stream.dup
stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
old_stream.close
end
silence_stream(STDOUT) do
if ActiveRecord::Migrator.respond_to? :migrate
ActiveRecord::Migrator.migrate File.expand_path('../../dummy_app/db/migrate/', __FILE__)
else
ActiveRecord::MigrationContext.new(
*([File.expand_path('../../dummy_app/db/migrate/', __FILE__)] +
(ActiveRecord::MigrationContext.instance_method(:initialize).arity == 2 ? [ActiveRecord::SchemaMigration] : [])),
).migrate
end
end
class Tableless < ActiveRecord::Base
class <<self
def load_schema
# do nothing
end
def columns
@columns ||= []
end
def column(name, sql_type = nil, default = nil, null = true)
define_attribute(name.to_s,
connection.send(:lookup_cast_type, sql_type.to_s))
columns <<
ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, connection.send(:lookup_cast_type, sql_type.to_s), sql_type.to_s, null)
end
def columns_hash
@columns_hash ||= Hash[columns.collect { |column| [column.name, column] }]
end
def column_names
@column_names ||= columns.collect(&:name)
end
def column_defaults
@column_defaults ||= columns.collect { |column| [column.name, nil] }.each_with_object({}) do |e, a|
a[e[0]] = e[1]
a
end
end
def attribute_types
@attribute_types ||=
Hash[columns.collect { |column| [column.name, lookup_attribute_type(column.type)] }]
end
def table_exists?
true
end
def primary_key
"id"
end
private
def lookup_attribute_type(type)
ActiveRecord::Type.lookup({datetime: :time}[type] || type)
end
end
# Override the save method to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
end
##
# Column length detection seems to be broken for PostgreSQL.
# This is a workaround..
# Refs. https://github.com/rails/rails/commit/b404613c977a5cc31c6748723e903fa5a0709c3b
#
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def lookup_cast_type(sql_type)
oid = execute("SELECT #{quote(sql_type)}::regtype::oid", 'SCHEMA').first['oid'].to_i
type_map.lookup(oid, sql_type)
end
end
end