activerecord-native_db_types_override 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,13 +25,15 @@ In your config/environment.rb or an environment specific configuration, you may
25
25
 
26
26
  Some gems may require the adapter prior to this point, so it may not need to be required, but we do not load the Rails adapters in the NativeDbTypesOverride gem because all are most likely not needed.
27
27
 
28
+ Note: I left the Ruby 1.8 hashrocket notation to show it shouldn't matter. Feel free to use 1.9+ hash syntax.
29
+
28
30
  #### PostgreSQL
29
31
 
30
32
  For example, if you want Rails to use the timestamptz type for all datetimes and timestamps created by migrations, you could use:
31
33
 
32
34
  require 'active_record/connection_adapters/postgresql_adapter'
33
35
  NativeDbTypesOverride.configure({
34
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
36
+ :postgres => {
35
37
  :datetime => { :name => "timestamptz" },
36
38
  :timestamp => { :name => "timestamptz" }
37
39
  }
@@ -43,9 +45,8 @@ See [PostgreSQLAdapter][postgres_adapter] for the default types.
43
45
 
44
46
  For the MySQL/MySQL2 adapters, maybe you could change boolean to a string type:
45
47
 
46
- require 'active_record/connection_adapters/abstract_mysql_adapter'
47
48
  NativeDbTypesOverride.configure({
48
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter => {
49
+ :mysql => {
49
50
  :boolean => { :name => "varchar", :limit => 1 }
50
51
  }
51
52
  })
@@ -56,9 +57,8 @@ See [AbstractMysqlAdapter][mysql_adapter] for the default types.
56
57
 
57
58
  Maybe you need to extend the default string limit from 255 to 4096:
58
59
 
59
- require 'active_record/connection_adapters/sqlite3_adapter'
60
60
  NativeDbTypesOverride.configure({
61
- ActiveRecord::ConnectionAdapters::SQLite3Adapter => {
61
+ :sqlite => {
62
62
  :string => { :name => "varchar", :limit => 4096 }
63
63
  }
64
64
  })
@@ -75,9 +75,8 @@ In addition, it's native_database_types method can define boolean as VARCHAR2 (1
75
75
 
76
76
  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:
77
77
 
78
- require 'active_record/connection_adapters/oracle_enhanced_adapter'
79
78
  NativeDbTypesOverride.configure({
80
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter => {
79
+ :oracle => {
81
80
  :datetime => { :name => "TIMESTAMP WITH TIMEZONE" },
82
81
  :timestamp => { :name => "TIMESTAMP WITH TIMEZONE" },
83
82
  :boolean => { :name => "VARCHAR2", :limit => 1 }
@@ -92,13 +91,21 @@ See [OracleEnhancedAdapter][oracle_adapter] for the default types.
92
91
 
93
92
  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.
94
93
 
95
- Be sure to add a require for the adapter, if it has not been loaded already prior to configuration.
94
+ Be sure to add a require for the adapter, if it has not been loaded already prior to configuration, e.g.:
95
+
96
+ require 'active_record/connection_adapters/postgresql_adapter'
97
+ NativeDbTypesOverride.configure({
98
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
99
+ :datetime => { :name => "timestamptz" },
100
+ :timestamp => { :name => "timestamptz" }
101
+ }
102
+ })
96
103
 
97
- Let us know if we can list a new or revised adapter here. Pull requests are welcome!
104
+ Let us know if we can add support for an adapter, so it can be referenced by a symbol instead. Pull requests welcome!
98
105
 
99
106
  ### Troubleshooting
100
107
 
101
- Make sure that you either add a require for the adapter before the configuration or you may get an `uninitialized constant (adapter class)` error, depending on whether something else loaded the adapter prior to configuration.
108
+ If not using one of the supported symbols you may need to add a require for the adapter before the configuration or you may get an `uninitialized constant (adapter class)` error, depending on whether something else loaded the adapter prior.
102
109
 
103
110
  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:
104
111
 
@@ -8,17 +8,43 @@ module NativeDbTypesOverride
8
8
 
9
9
  def configure(hash)
10
10
  puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION}" if NativeDbTypesOverride.debug?
11
- hash.keys.each do |clazz|
11
+ hash.keys.each do |key|
12
+ begin
13
+ clazz = convert_symbol_to_class(key)
14
+ rescue => e
15
+ puts "Unable to get adapter class for #{key.inspect}. Try specifying the adapter class itself as the key in the hash in NativeDbTypesOverride.configure(). Error: #{e.message}\n#{e.backtrace.join("\n")}"
16
+ end
12
17
  new_types = {}
13
18
  begin
14
19
  new_types = clazz.const_get('NATIVE_DATABASE_TYPES')
15
20
  rescue
16
- puts "No NATIVE_DATABASE_TYPES constant on #{clazz} so expecting the whole types hash to be specified in the NativeDbTypesOverride::Options.configure" if NativeDbTypesOverride.debug?
21
+ puts "No NATIVE_DATABASE_TYPES constant on #{clazz} so expecting the whole types hash to be specified via NativeDbTypesOverride.configure()" if NativeDbTypesOverride.debug?
17
22
  end
18
- new_types = new_types.merge(hash[clazz])
23
+ new_types = new_types.merge(hash[key])
19
24
  puts "Defining #{clazz}.native_database_types as #{new_types.inspect}" if NativeDbTypesOverride.debug?
20
25
  clazz.class_eval "def native_database_types; #{new_types.inspect}; end"
21
26
  end
22
27
  end
28
+
29
+ private
30
+
31
+ def convert_symbol_to_class(clazz)
32
+ case clazz
33
+ when :postgres
34
+ require 'active_record/connection_adapters/postgresql_adapter'
35
+ 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'.constantize
36
+ when :mysql
37
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
38
+ 'ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter'.constantize
39
+ when :sqlite
40
+ require 'active_record/connection_adapters/sqlite3_adapter'
41
+ 'ActiveRecord::ConnectionAdapters::SQLite3Adapter'.constantize
42
+ when :oracle
43
+ require 'active_record/connection_adapters/oracle_enhanced_adapter'
44
+ 'ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter'.constantize
45
+ else
46
+ clazz
47
+ end
48
+ end
23
49
  end
24
50
  end
@@ -1,3 +1,3 @@
1
1
  module NativeDbTypesOverride
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-native_db_types_override
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: