activerecord-native_db_types_override 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
ActiveRecord 3.1+/4.0 Native Database Types Override
|
2
|
+
=====
|
3
|
+
|
4
|
+
Define native database types that Rails migrations and ActiveRecord should use. Theoretically this should work for any ActiveRecord adapter written to support the access defined in the "How it works" section.
|
5
|
+
|
6
|
+
Does not work in Rails/ActiveRecord 3.0 or earlier versions, and even though it should work in Rails/ActiveRecord 3.1+/4.0.
|
7
|
+
|
8
|
+
### How it works
|
9
|
+
|
10
|
+
All adapter classes in Rails 3.1-4.0 should have a NATIVE_DATABASE_TYPES constant and a native_database_types method that can be redefined, so we get the NATIVE_DATABASE_TYPES if it exists, if not we start with an empty hash. Then we merge in your settings, and redefine the native_database_types instance method (defined at class level so using class_eval) to return the value of inspect from the merged hash.
|
11
|
+
|
12
|
+
### Setup
|
13
|
+
|
14
|
+
In your ActiveRecord/Rails 3.1+ project, add this to your Gemfile:
|
15
|
+
|
16
|
+
gem 'activerecord-native_db_types_override', :git => 'git://github.com/garysweaver/activerecord-native_db_types_override.git'
|
17
|
+
|
18
|
+
Then run:
|
19
|
+
|
20
|
+
bundle install
|
21
|
+
|
22
|
+
To stay up-to-date, periodically run:
|
23
|
+
|
24
|
+
bundle update activerecord-native_db_types_override
|
25
|
+
|
26
|
+
### Usage
|
27
|
+
|
28
|
+
In your config/environment.rb or environment specfic configuration, you may specify one or more options in the config hash that will be merged into the default types. The act of configuring does the hash merge/activation of changes.
|
29
|
+
|
30
|
+
#### PostgreSQL
|
31
|
+
|
32
|
+
For example, if you want Rails to use the timestamptz type for all datetimes and timestamps created by migrations, you could use:
|
33
|
+
|
34
|
+
NativeDbTypesOverride::Options.configure({
|
35
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
|
36
|
+
:datetime => { :name => "timestamptz" },
|
37
|
+
:timestamp => { :name => "timestamptz" }
|
38
|
+
}
|
39
|
+
})
|
40
|
+
|
41
|
+
See [PostgreSQLAdapter][postgres_adapter] for the default types.
|
42
|
+
|
43
|
+
#### MySQL
|
44
|
+
|
45
|
+
For the MySQL/MySQL2 adapters, maybe you could change boolean to a string type:
|
46
|
+
|
47
|
+
NativeDbTypesOverride::Options.configure({
|
48
|
+
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter => {
|
49
|
+
:boolean => { :name => "varchar", :limit => 1 }
|
50
|
+
}
|
51
|
+
})
|
52
|
+
|
53
|
+
See [AbstractMysqlAdapter][mysql_adapter] for the default types.
|
54
|
+
|
55
|
+
#### SQLite3
|
56
|
+
|
57
|
+
Maybe you need to extend the default string limit from 255 to 4096:
|
58
|
+
|
59
|
+
NativeDbTypesOverride::Options.configure({
|
60
|
+
ActiveRecord::ConnectionAdapters::SQLite3Adapter => {
|
61
|
+
:string => { :name => "varchar", :limit => 4096 }
|
62
|
+
}
|
63
|
+
})
|
64
|
+
|
65
|
+
See [SQLite3Adapter][sqlite_adapter] for the default types.
|
66
|
+
|
67
|
+
#### Oracle
|
68
|
+
|
69
|
+
Oracle enhanced adapter isn't included in Rails, so you have to include its gem in your Gemfile along with any other requirements it has.
|
70
|
+
|
71
|
+
In addition, it's native_database_types method can define boolean as VARCHAR2 (1 char) or NUMBER (0 or 1), so if that is all you are trying to do, then *don't use this gem* and just try this or look in its adapter/README to see how this could be done:
|
72
|
+
|
73
|
+
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
|
74
|
+
|
75
|
+
However, if you need to make another change like making datetime and timestamp store timezones *and* you want to emulate_booleans_from_strings, just ensure that you define the boolean shown in the following example rather than using OracleEnhancedAdapter's emulate_booleans_from_strings option:
|
76
|
+
|
77
|
+
NativeDbTypesOverride::Options.configure({
|
78
|
+
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter => {
|
79
|
+
:datetime => { :name => "TIMESTAMP WITH TIMEZONE" },
|
80
|
+
:timestamp => { :name => "TIMESTAMP WITH TIMEZONE" },
|
81
|
+
:boolean => { :name => "VARCHAR2", :limit => 1 }
|
82
|
+
}
|
83
|
+
})
|
84
|
+
|
85
|
+
The reason for this is that Native Database Types Override gem tries to use the constant NATIVE_DATABASE_TYPES available on most adapters to get the existing hash before doing the overrides, but OracleEnhancedAdapter's emulate_booleans_from_strings option changes what is returned by the native_database_types method to NATIVE_DATABASE_TYPES but with :boolean changed to the value {:name => "VARCHAR2", :limit => 1}.
|
86
|
+
|
87
|
+
See [OracleEnhancedAdapter][oracle_adapter] for the default types.
|
88
|
+
|
89
|
+
#### Others
|
90
|
+
|
91
|
+
Look for the adapter class that contains the native_database_types method and specify the fully-qualified name (e.g. ActiveRecord::ConnectionAdapters::MyDbAdapter) as the key in the options hash. Let us know if we can list your adapter here.
|
92
|
+
|
93
|
+
### Troubleshooting
|
94
|
+
|
95
|
+
Test out a migration and include all the types defined in your adapter's NATIVE_DATABASE_TYPES if you're unsure whether it is defining things correctly, e.g. in a test project for PostgreSQL in Rails 3.1/3.2 you could do this and then look at the my_models table in your database:
|
96
|
+
|
97
|
+
rails g model MyModel a_string:string a_text:text an_integer:integer a_float:float a_decimal:decimal a_datetime:datetime a_timestamp:timestamp a_time:time a_date:date a_binary:binary a_boolean:boolean a_xml:xml a_tsvector:tsvector
|
98
|
+
rake db:migrate
|
99
|
+
|
100
|
+
Then just to ensure it really is working, go into rails console:
|
101
|
+
|
102
|
+
rails c
|
103
|
+
|
104
|
+
And try to play with data (this is postgres-specific):
|
105
|
+
|
106
|
+
MyModel.all
|
107
|
+
m = MyModel.new
|
108
|
+
m.a_string = 'test make this string as long as the limit'
|
109
|
+
m.an_integer = 12345678
|
110
|
+
m.a_float = 1.2345789
|
111
|
+
m.a_decimal = 1.2345789
|
112
|
+
m.a_datetime = Time.now
|
113
|
+
m.a_timestamp = Time.now
|
114
|
+
m.a_time = Time.now
|
115
|
+
m.a_date = Time.now
|
116
|
+
m.a_binary = 1
|
117
|
+
m.a_boolean = true
|
118
|
+
m.a_xml = '<testing>123</testing>'
|
119
|
+
m.a_tsvector = nil
|
120
|
+
m.save!
|
121
|
+
|
122
|
+
### License
|
123
|
+
|
124
|
+
Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
|
125
|
+
|
126
|
+
[postgres_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
|
127
|
+
[mysql_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
|
128
|
+
[sqlite_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
|
129
|
+
[oracle_adapter]: https://github.com/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
|
130
|
+
[lic]: http://github.com/garysweaver/activerecord-native_db_types_override/blob/master/LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
#http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.test_files = FileList['test/**/*_test.rb']
|
7
|
+
t.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Run tests"
|
11
|
+
task :default => :test
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module NativeDbTypesOverride
|
2
|
+
class Options
|
3
|
+
def self.configure(hash)
|
4
|
+
puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION}"
|
5
|
+
|
6
|
+
hash.keys.each do |clazz|
|
7
|
+
# do the override
|
8
|
+
new_types = {}
|
9
|
+
|
10
|
+
begin
|
11
|
+
new_types = clazz.const_get('NATIVE_DATABASE_TYPES')
|
12
|
+
rescue
|
13
|
+
puts "No NATIVE_DATABASE_TYPES constant on #{clazz} so expecting the whole types hash to be specified in the NativeDbTypesOverride::Options.configure"
|
14
|
+
end
|
15
|
+
|
16
|
+
new_types = new_types.merge(hash[clazz])
|
17
|
+
|
18
|
+
puts "Setting #{clazz}.native_database_types to #{new_types.inspect}"
|
19
|
+
|
20
|
+
clazz.class_eval "def native_database_types; #{new_types.inspect}; end"
|
21
|
+
|
22
|
+
puts "ActiveRecord - Native Database Types Override success"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-native_db_types_override
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gary S. Weaver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Overrides native database types for any database adapter with a native_database_types
|
15
|
+
method. Compatible with ActiveRecord 3.x/4.x and Ruby 1.8.7/1.9.x.
|
16
|
+
email:
|
17
|
+
- garysweaver@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/activerecord-native_db_types_override/options.rb
|
23
|
+
- lib/activerecord-native_db_types_override/version.rb
|
24
|
+
- lib/activerecord-native_db_types_override.rb
|
25
|
+
- Rakefile
|
26
|
+
- README.md
|
27
|
+
homepage: https://github.com/garysweaver/activerecord-native_db_types_override
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.24
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Overrides native database types in ActiveRecord DB adapters.
|
52
|
+
test_files: []
|