activerecord-native_db_types_override 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09286096663b0d7e97e7bb99b064d0772db91534
4
+ data.tar.gz: 1f208e3fd9b7f020f3105e1d60525fb53cd4c1f4
5
+ SHA512:
6
+ metadata.gz: 1d7dc24d2ff96d672f561150a8523dd43dab3c4abd6d7317e513f846bf1fae2e385a4248a65c34d9e68c89c7b03a9411f7149a9d49081e2a35c5449e510d49f6
7
+ data.tar.gz: 93e37eadb0e9ff0529767afa9e57162bf4bc90998b3f9a5ac9522bfca813a824f6bd97c0a95923368fa38af5d22980f4d98675bc7dadfc41525842a5ec435cad
data/README.md CHANGED
@@ -1,17 +1,10 @@
1
- ActiveRecord 3.1+/4.0 Native Database Types Override
2
- =====
1
+ ## ActiveRecord 4.x Native Database Types Override
3
2
 
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.
3
+ Define native database types and change default migration behavior in ActiveRecord/Rails.
11
4
 
12
5
  ### Setup
13
6
 
14
- In your ActiveRecord/Rails 3.1+ project, add this to your Gemfile:
7
+ In your ActiveRecord/Rails 4.x project, add this to your Gemfile:
15
8
 
16
9
  gem 'activerecord-native_db_types_override'
17
10
 
@@ -21,46 +14,85 @@ Then run:
21
14
 
22
15
  ### Usage
23
16
 
24
- In your config/environment.rb or an environment specific 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.
17
+ Define configuration to be run prior to migration execution, such as in a Rails initializer or environment file.
18
+
19
+ #### Configure Adapters
20
+
21
+ The following symbols can be used can be used with the NativeDbTypesOverride.configure_adapters method to automatically require and load the right adapter class prior to configuring it:
22
+
23
+ Adapters included in ActiveRecord:
24
+ * [:postgres](https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb)
25
+ * [:mysql](https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb)
26
+ * [:sqlite](https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb)
25
27
 
26
- If using Ruby 1.8.x, use hashrocket notation instead for the following examples.
28
+ Adapters that require other gems:
29
+ * [:oracle_enhanced](https://github.com/rsim/oracle-enhanced/blob/release16/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb)
30
+ * [:sqlserver](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/4-2-stable/lib/active_record/connection_adapters/sqlserver_adapter.rb)
31
+
32
+ You can also specify a custom adapter class that uses a native_database_types method to map types. See 'Other Adapters' for more information.
27
33
 
28
34
  #### PostgreSQL
29
35
 
30
- For example, if you want Rails to use the timestamptz type for all datetimes and timestamps created by migrations, you could use:
36
+ The following would:
37
+ * use primary key type 'bigserial primary key' for in migrations
38
+ * change add_reference behavior in migrations to use bigint type by default
39
+ * use timestamptz for all datetimes and timestamps created by migrations*
31
40
 
32
- NativeDbTypesOverride.configure({
41
+ NativeDbTypesOverride.configure_adapters({
33
42
  postgres: {
34
43
  datetime: { name: "timestamptz" },
35
- timestamp: { name: "timestamptz" }
44
+ timestamp: { name: "timestamptz" },
45
+ primary_key: { name: "bigserial primary key"}
36
46
  }
37
47
  })
38
48
 
39
- See [PostgreSQLAdapter][postgres_adapter] for the default types.
49
+ NativeDbTypesOverride.configure_migrations({
50
+ add_reference: {type: :bigint}
51
+ })
52
+
53
+ See PostgreSQLAdapter source for the default types.
40
54
 
41
55
  #### MySQL
42
56
 
43
57
  For the MySQL/MySQL2 adapters, maybe you could change boolean to a string type:
44
58
 
45
- NativeDbTypesOverride.configure({
59
+ NativeDbTypesOverride.configure_adapters({
46
60
  mysql: {
47
61
  boolean: { name: "varchar", limit: 1 }
48
62
  }
49
63
  })
50
64
 
51
- See [AbstractMysqlAdapter][mysql_adapter] for the default types.
65
+ Alternatively, you could use the this Gem to override [primary_key](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L43) to overcome errors that occur when trying to run `rake db:migrate` that are caused by MySQL updates (i.e. MySQL 5.7) that no longer support DEFAULT NULL values for PRIMARY KEY. Simply create the following:
66
+
67
+ *config/initializers/abstract_mysql2_adapter.rb*
68
+
69
+ require 'active_record/connection_adapters/mysql2_adapter'
70
+ NativeDbTypesOverride.configure_adapters({
71
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter => {
72
+ primary_key: "int(11) auto_increment PRIMARY KEY"
73
+ }
74
+ })
75
+
76
+ *config/environment.rb*
77
+
78
+ # Load monkey patches to prevent migration errors after importing legacy sql dumpfile
79
+ require File.expand_path('../initializers/abstract_mysql2_adapter.rb', __FILE__)
80
+
81
+ *config/database.yml* check contains 'adapter: mysql2' for db connection to be used
82
+
83
+ See [AbstractMysqlAdapter][mysql_adapter] for the default types. (Change code branch as needed.)
52
84
 
53
85
  #### SQLite3
54
86
 
55
87
  Maybe you need to extend the default string limit from 255 to 4096:
56
88
 
57
- NativeDbTypesOverride.configure({
89
+ NativeDbTypesOverride.configure_adapters({
58
90
  sqlite: {
59
91
  string: { :name: "varchar", limit: 4096 }
60
92
  }
61
93
  })
62
94
 
63
- See [SQLite3Adapter][sqlite_adapter] for the default types.
95
+ See [SQLite3Adapter][sqlite_adapter] for the default types. (Change code branch as needed.)
64
96
 
65
97
  #### Oracle
66
98
 
@@ -72,7 +104,7 @@ In addition, it's native_database_types method can define boolean as VARCHAR2 (1
72
104
 
73
105
  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:
74
106
 
75
- NativeDbTypesOverride.configure({
107
+ NativeDbTypesOverride.configure_adapters({
76
108
  oracle: {
77
109
  datetime: { name: "TIMESTAMP WITH TIMEZONE" },
78
110
  timestamp: { name: "TIMESTAMP WITH TIMEZONE" },
@@ -82,33 +114,40 @@ However, if you need to make another change like making datetime and timestamp s
82
114
 
83
115
  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}.
84
116
 
85
- See [OracleEnhancedAdapter][oracle_adapter] for the default types.
117
+ See oracle_enhanced_adapter link below for the default types.
86
118
 
87
- #### Others
119
+ #### Other Adapters
88
120
 
89
- 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.
121
+ ActiveRecord adapter class that uses a `native_database_types` method to define the type mappings (as most do) should work. Just specify the fully-qualified name (e.g. ActiveRecord::ConnectionAdapters::MyDbAdapter) as the key in the options hash.
90
122
 
91
123
  Be sure to add a require for the adapter, if it has not been loaded already prior to configuration, e.g.:
92
124
 
93
125
  require 'active_record/connection_adapters/postgresql_adapter'
94
- NativeDbTypesOverride.configure({
126
+ NativeDbTypesOverride.configure_adapters({
95
127
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
96
128
  datetime: { name: "timestamptz" },
97
129
  timestamp: { name: "timestamptz" }
98
130
  }
99
131
  })
100
132
 
101
- Let us know if we can add support for an adapter, so it can be referenced by a symbol instead. Pull requests welcome!
102
133
 
103
134
  ### Troubleshooting
104
135
 
136
+ Turn on debug output:
137
+
138
+ NativeDbTypesOverride.debug = true
139
+
105
140
  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.
106
141
 
107
- 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:
142
+ 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.:
108
143
 
109
144
  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
110
145
  rake db:migrate
111
146
 
147
+ With Rails you can output the schema to see if the types were set correctly:
148
+
149
+ rake db:structure:dump
150
+
112
151
  Then just to ensure it really is working, go into rails console:
113
152
 
114
153
  rails c
@@ -131,16 +170,12 @@ And try to play with data (this is postgres-specific):
131
170
  m.a_tsvector = nil
132
171
  m.save!
133
172
 
134
- Turn on debug output by adding this prior to NativeDbTypesOverride.configure:
173
+ ### Contributors
135
174
 
136
- NativeDbTypesOverride.debug = true
175
+ * Gary Weaver (https://github.com/garysweaver)
137
176
 
138
177
  ### License
139
178
 
140
- Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
179
+ (c) 2012-2014 FineLine Prototyping, Inc., (c) 2016-2017 Proto Labs, Inc., released under the [MIT license][lic].
141
180
 
142
- [postgres_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
143
- [mysql_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
144
- [sqlite_adapter]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
145
- [oracle_adapter]: https://github.com/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
146
- [lic]: http://github.com/garysweaver/activerecord-native_db_types_override/blob/master/LICENSE
181
+ [lic]: LICENSE
@@ -1,2 +1,5 @@
1
1
  require 'activerecord-native_db_types_override/version'
2
+ require 'activerecord-native_db_types_override/configuration_error'
3
+ require 'activerecord-native_db_types_override/adapter_overrider'
4
+ require 'activerecord-native_db_types_override/patches'
2
5
  require 'activerecord-native_db_types_override/options'
@@ -0,0 +1,40 @@
1
+ module NativeDbTypesOverride
2
+ class AdapterOverrider
3
+
4
+ def self.override_native_database_types(adapter_class, hash)
5
+ puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION}" if NativeDbTypesOverride.debug?
6
+ new_types = {}
7
+ begin
8
+ new_types = adapter_class.const_get('NATIVE_DATABASE_TYPES')
9
+ rescue
10
+ puts "No NATIVE_DATABASE_TYPES constant on #{adapter_class} so expecting the whole types hash to be specified via NativeDbTypesOverride.configure()" if NativeDbTypesOverride.debug?
11
+ end
12
+ new_types = new_types.merge(hash)
13
+ puts "Defining #{adapter_class}.native_database_types as #{new_types.inspect}" if NativeDbTypesOverride.debug?
14
+
15
+ adapter_class.class_eval "def native_database_types; #{new_types.inspect}; end"
16
+ end
17
+
18
+ def self.convert_symbol_to_class(adapter_class_or_sym)
19
+ case adapter_class_or_sym
20
+ when :postgres
21
+ require 'active_record/connection_adapters/postgresql_adapter'
22
+ '::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'.constantize
23
+ when :mysql
24
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
25
+ '::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter'.constantize
26
+ when :sqlite
27
+ require 'active_record/connection_adapters/sqlite3_adapter'
28
+ '::ActiveRecord::ConnectionAdapters::SQLite3Adapter'.constantize
29
+ when :oracle
30
+ require 'active_record/connection_adapters/oracle_enhanced_adapter'
31
+ '::ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter'.constantize
32
+ when :sqlserver
33
+ require 'active_record/connection_adapters/sqlserver_adapter'
34
+ '::ActiveRecord::ConnectionAdapters::SQLServer'.constantize
35
+ else
36
+ adapter_class_or_sym
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ module NativeDbTypesOverride
2
+ class ConfigurationError < ::StandardError
3
+ end
4
+ end
@@ -1,50 +1,32 @@
1
1
  module NativeDbTypesOverride
2
2
  class << self
3
- attr_accessor :debug
3
+ attr_accessor :debug, :adapters_options, :migrations_options
4
4
 
5
5
  def debug?
6
6
  !!send(:debug)
7
7
  end
8
-
8
+
9
9
  def configure(hash)
10
- puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION}" if NativeDbTypesOverride.debug?
10
+ warn 'NativeDbTypesOverride.configure is deprecated! Call NativeDbTypesOverride.adapters instead of NativeDbTypesOverride.configure.'
11
+ configure_adapters(hash)
12
+ end
13
+
14
+ def configure_adapters(hash)
15
+ puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION} - configure_adapters called" if NativeDbTypesOverride.debug?
11
16
  hash.keys.each do |key|
12
17
  begin
13
- clazz = convert_symbol_to_class(key)
18
+ clazz = ::NativeDbTypesOverride::AdapterOverrider.convert_symbol_to_class(key)
14
19
  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
17
- new_types = {}
18
- begin
19
- new_types = clazz.const_get('NATIVE_DATABASE_TYPES')
20
- rescue
21
- puts "No NATIVE_DATABASE_TYPES constant on #{clazz} so expecting the whole types hash to be specified via NativeDbTypesOverride.configure()" if NativeDbTypesOverride.debug?
20
+ raise ::NativeDbTypesOverride::ConfigurationError.new "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")}"
22
21
  end
23
- new_types = new_types.merge(hash[key])
24
- puts "Defining #{clazz}.native_database_types as #{new_types.inspect}" if NativeDbTypesOverride.debug?
25
- clazz.class_eval "def native_database_types; #{new_types.inspect}; end"
22
+
23
+ ::NativeDbTypesOverride::AdapterOverrider.override_native_database_types(clazz, hash[key])
26
24
  end
27
25
  end
28
26
 
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
27
+ def configure_migrations(hash)
28
+ puts "ActiveRecord - Native Database Types Override #{NativeDbTypesOverride::VERSION} - configure_adapters called" if NativeDbTypesOverride.debug?
29
+ self.migrations_options = hash
48
30
  end
49
31
  end
50
32
  end
@@ -0,0 +1,16 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module SchemaStatements
4
+ alias_method :add_reference_without_native_db_types_override, :add_reference
5
+
6
+ def add_reference(table_name, ref_name, options = {})
7
+ if ::NativeDbTypesOverride.migrations_options
8
+ add_ref_opts = ::NativeDbTypesOverride.migrations_options[:add_reference]
9
+ options = Marshal.load(Marshal.dump(add_ref_opts)).merge(options) if add_ref_opts
10
+ end
11
+
12
+ add_reference_without_native_db_types_override(table_name, ref_name, options)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module NativeDbTypesOverride
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,52 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-native_db_types_override
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gary S. Weaver
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-12 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: Overrides native database types for any database adapter with a native_database_types
15
- method. Compatible with ActiveRecord 3.1+/4.0.
13
+ description: Define native database types and change default migration behavior in
14
+ ActiveRecord/Rails.
16
15
  email:
17
16
  - garysweaver@gmail.com
18
17
  executables: []
19
18
  extensions: []
20
19
  extra_rdoc_files: []
21
20
  files:
21
+ - README.md
22
+ - Rakefile
23
+ - lib/activerecord-native_db_types_override.rb
24
+ - lib/activerecord-native_db_types_override/adapter_overrider.rb
25
+ - lib/activerecord-native_db_types_override/configuration_error.rb
22
26
  - lib/activerecord-native_db_types_override/options.rb
27
+ - lib/activerecord-native_db_types_override/patches.rb
23
28
  - lib/activerecord-native_db_types_override/version.rb
24
- - lib/activerecord-native_db_types_override.rb
25
- - Rakefile
26
- - README.md
27
29
  homepage: https://github.com/garysweaver/activerecord-native_db_types_override
28
30
  licenses:
29
31
  - MIT
32
+ metadata: {}
30
33
  post_install_message:
31
34
  rdoc_options: []
32
35
  require_paths:
33
36
  - lib
34
37
  required_ruby_version: !ruby/object:Gem::Requirement
35
- none: false
36
38
  requirements:
37
- - - ! '>='
39
+ - - ">="
38
40
  - !ruby/object:Gem::Version
39
41
  version: '0'
40
42
  required_rubygems_version: !ruby/object:Gem::Requirement
41
- none: false
42
43
  requirements:
43
- - - ! '>='
44
+ - - ">="
44
45
  - !ruby/object:Gem::Version
45
46
  version: '0'
46
47
  requirements: []
47
48
  rubyforge_project:
48
- rubygems_version: 1.8.24
49
+ rubygems_version: 2.6.8
49
50
  signing_key:
50
- specification_version: 3
51
- summary: Overrides native database types in ActiveRecord DB adapters.
51
+ specification_version: 4
52
+ summary: Define native database types and change default migration behavior.
52
53
  test_files: []