activerecord-mysql-awesome 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: acaf5232085a194cd6be165d88c391949371c823
4
+ data.tar.gz: 7454e2e8c349fa58074f9dfee87b40dc7e06f656
5
+ SHA512:
6
+ metadata.gz: 867ae8b90d562e056db29aa6d0f0a5f77b69d479661df53f3aa1f92760c4fb4fff72d903b79620ac5223cd08bc9ab1771dde9ac14dee549be71dfbd86bf5284d
7
+ data.tar.gz: 7e592e3d399b3c2421fafa289707caf3f986967e2254c25ce44fbdde87d8c0662173eddec3ebc9c792989f20ac3bea57367494e60f2e04e8831217be04fcecc5
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> #{ENV['AR_VERSION']}" if ENV['AR_VERSION']
4
+
5
+ # Specify your gem's dependencies in activerecord-mysql-awesome.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ryuta Kamizono
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # ActiveRecord::Mysql::Awesome
2
+
3
+ [![Build Status](https://travis-ci.org/kamipo/activerecord-mysql-awesome.png?branch=master)](https://travis-ci.org/kamipo/activerecord-mysql-awesome)
4
+
5
+ Awecome patches backported for ActiveRecord MySQL adapters.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'activerecord-mysql-awesome'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install activerecord-mysql-awesome
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/kamipo/activerecord-mysql-awesome/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/mysql/awesome/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-mysql-awesome"
8
+ spec.version = ActiveRecord::Mysql::Awesome::VERSION
9
+ spec.authors = ["Ryuta Kamizono"]
10
+ spec.email = ["kamipo@gmail.com"]
11
+ spec.summary = %q{Awecome patches backported for ActiveRecord MySQL adapters}
12
+ spec.description = %q{Awecome patches backported for ActiveRecord MySQL adapters}
13
+ spec.homepage = "https://github.com/kamipo/activerecord-mysql-awesome"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
24
+ spec.add_runtime_dependency "activerecord", "~> 4.0"
25
+ spec.add_runtime_dependency "mysql2"
26
+ end
@@ -0,0 +1,7 @@
1
+ if ActiveRecord::VERSION::MAJOR == 4
2
+ require 'activerecord-mysql-awesome/active_record/schema_dumper'
3
+ require 'activerecord-mysql-awesome/active_record/connection_adapters/abstract/schema_dumper'
4
+ require 'activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter'
5
+ else
6
+ raise "activerecord-mysql-awesome supports activerecord ~> 4.x"
7
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module Mysql
3
+ module Awesome
4
+ class Railtie < Rails::Railtie
5
+ initializer 'activerecord-mysql-awesome' do
6
+ ActiveSupport.on_load :active_record do
7
+ require 'activerecord/mysql/awesome/base'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ module Mysql
3
+ module Awesome
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_support'
2
+
3
+ begin
4
+ require 'rails'
5
+ rescue LoadError
6
+ # nothing to do! yay!
7
+ end
8
+
9
+ if defined? Rails
10
+ require 'activerecord/mysql/awesome/railtie'
11
+ else
12
+ ActiveSupport.on_load :active_record do
13
+ require 'activerecord/mysql/awesome/base'
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_record/connection_adapters/abstract/schema_dumper'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module ColumnDumper
6
+ def options_for_column_spec(table_name)
7
+ { table_name: table_name }
8
+ end
9
+
10
+ def table_options(table_name)
11
+ nil
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,209 @@
1
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class AbstractMysqlAdapter < AbstractAdapter
6
+
7
+ class ChangeColumnDefinition < Struct.new(:column, :name) #:nodoc:
8
+ end
9
+
10
+ class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
11
+ attr_accessor :auto_increment, :unsigned, :charset, :collation
12
+ end
13
+
14
+ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
15
+ def initialize(types, name, temporary, options, as = nil)
16
+ super(types, name, temporary, options)
17
+ @as = as
18
+ end
19
+
20
+ def new_column_definition(name, type, options) # :nodoc:
21
+ column = super
22
+ column.auto_increment = options[:auto_increment]
23
+ column.unsigned = options[:unsigned]
24
+ column.charset = options[:charset]
25
+ column.collation = options[:collation]
26
+ column
27
+ end
28
+
29
+ private
30
+
31
+ def create_column_definition(name, type)
32
+ ColumnDefinition.new name, type
33
+ end
34
+ end
35
+
36
+ class SchemaCreation < AbstractAdapter::SchemaCreation
37
+ def visit_AddColumn(o)
38
+ add_column_position!("ADD #{accept(o)}", column_options(o))
39
+ end
40
+
41
+ private
42
+
43
+ def visit_ColumnDefinition(o)
44
+ sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale, o.unsigned)
45
+ column_sql = "#{quote_column_name(o.name)} #{sql_type}"
46
+ add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
47
+ column_sql
48
+ end
49
+
50
+ def visit_ChangeColumnDefinition(o)
51
+ change_column_sql = "CHANGE #{quote_column_name(o.name)} #{accept(o.column)}"
52
+ add_column_position!(change_column_sql, column_options(o.column))
53
+ end
54
+
55
+ def column_options(o)
56
+ column_options = super
57
+ column_options[:first] = o.first
58
+ column_options[:after] = o.after
59
+ column_options[:auto_increment] = o.auto_increment
60
+ column_options[:primary_key] = o.primary_key
61
+ column_options[:charset] = o.charset
62
+ column_options[:collation] = o.collation
63
+ column_options
64
+ end
65
+
66
+ def add_column_options!(sql, options)
67
+ if options[:charset]
68
+ sql << " CHARACTER SET #{options[:charset]}"
69
+ end
70
+ if options[:collation]
71
+ sql << " COLLATE #{options[:collation]}"
72
+ end
73
+ if options[:primary_key] == true
74
+ sql << " PRIMARY KEY"
75
+ end
76
+ super
77
+ end
78
+
79
+ def add_column_position!(sql, options)
80
+ if options[:first]
81
+ sql << " FIRST"
82
+ elsif options[:after]
83
+ sql << " AFTER #{quote_column_name(options[:after])}"
84
+ end
85
+ sql
86
+ end
87
+
88
+ def type_to_sql(type, limit, precision, scale, unsigned = false)
89
+ @conn.type_to_sql type.to_sym, limit, precision, scale, unsigned
90
+ end
91
+
92
+ def quote_value(value, column)
93
+ column.sql_type ||= type_to_sql(column.type, column.limit, column.precision, column.scale, column.unsigned)
94
+ super
95
+ end
96
+ end
97
+
98
+ class Column < ConnectionAdapters::Column # :nodoc:
99
+ def unsigned?
100
+ sql_type =~ /unsigned/i
101
+ end
102
+ end
103
+
104
+ def options_for_column_spec(table_name)
105
+ if collation = select_one("SHOW TABLE STATUS LIKE '#{table_name}'")["Collation"]
106
+ super.merge(collation: collation)
107
+ else
108
+ super
109
+ end
110
+ end
111
+
112
+ def prepare_column_options(column, options) # :nodoc:
113
+ spec = super
114
+ spec[:unsigned] = 'true' if column.unsigned?
115
+ if column.collation && column.collation != options[:collation]
116
+ spec[:collation] = column.collation.inspect
117
+ end
118
+ spec
119
+ end
120
+
121
+ def migration_keys
122
+ super + [:unsigned, :collation]
123
+ end
124
+
125
+ def table_options(table_name)
126
+ create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]
127
+
128
+ # strip create_definitions and partition_options
129
+ raw_table_options = create_table_info.sub(/\A.*\n\) /m, '').sub(/\n\/\*!.*\*\/\n\z/m, '').strip
130
+
131
+ # strip AUTO_INCREMENT
132
+ raw_table_options.sub(/(ENGINE=\w+)(?: AUTO_INCREMENT=\d+)/, '\1')
133
+ end
134
+
135
+ alias type_to_sql_without_awesome type_to_sql
136
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
137
+ case type.to_s
138
+ when 'integer'
139
+ case limit
140
+ when nil, 4, 11; 'int' # compatibility with MySQL default
141
+ else
142
+ type_to_sql_without_awesome(type, limit, precision, scale)
143
+ end.tap do |sql_type|
144
+ sql_type << ' unsigned' if unsigned
145
+ end
146
+ when 'float', 'decimal'
147
+ type_to_sql_without_awesome(type, limit, precision, scale).tap do |sql_type|
148
+ sql_type << ' unsigned' if unsigned
149
+ end
150
+ when 'primary_key'
151
+ "#{type_to_sql(:integer, limit, precision, scale, unsigned)} auto_increment PRIMARY KEY"
152
+ else
153
+ type_to_sql_without_awesome(type, limit, precision, scale)
154
+ end
155
+ end
156
+
157
+ def add_column_sql(table_name, column_name, type, options = {})
158
+ td = create_table_definition(table_name)
159
+ cd = td.new_column_definition(column_name, type, options)
160
+ schema_creation.visit_AddColumn cd
161
+ end
162
+
163
+ def change_column_sql(table_name, column_name, type, options = {})
164
+ column = column_for(table_name, column_name)
165
+
166
+ unless options_include_default?(options)
167
+ options[:default] = column.default
168
+ end
169
+
170
+ unless options.has_key?(:null)
171
+ options[:null] = column.null
172
+ end
173
+
174
+ td = create_table_definition(table_name)
175
+ cd = td.new_column_definition(column.name, type, options)
176
+ schema_creation.accept ChangeColumnDefinition.new cd, column.name
177
+ end
178
+
179
+ def rename_column_sql(table_name, column_name, new_column_name)
180
+ column = column_for(table_name, column_name)
181
+ options = {
182
+ default: column.default,
183
+ null: column.null,
184
+ auto_increment: column.extra == "auto_increment"
185
+ }
186
+
187
+ current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'", 'SCHEMA')["Type"]
188
+ td = create_table_definition(table_name)
189
+ cd = td.new_column_definition(new_column_name, current_type, options)
190
+ schema_creation.accept ChangeColumnDefinition.new cd, column.name
191
+ end
192
+
193
+ alias configure_connection_without_awesome configure_connection
194
+ def configure_connection
195
+ _config = @config
196
+ if [':default', :default].include?(@config[:strict])
197
+ @config = @config.deep_merge(variables: { sql_mode: :default })
198
+ end
199
+ configure_connection_without_awesome
200
+ ensure
201
+ @config = _config
202
+ end
203
+
204
+ def create_table_definition(name, temporary = false, options = nil, as = nil) # :nodoc:
205
+ TableDefinition.new native_database_types, name, temporary, options, as
206
+ end
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_record/schema_dumper'
2
+
3
+ module ActiveRecord
4
+ class SchemaDumper #:nodoc:
5
+ private
6
+
7
+ alias table_without_awesome table
8
+ def table(table, stream)
9
+ @types = @types.merge(@connection.options_for_column_spec(table))
10
+ if table_options = @connection.table_options(table)
11
+ buf = StringIO.new
12
+ table_without_awesome(table, buf)
13
+ stream.print buf.string.sub(/(?= do \|t\|)/, ", options: #{table_options.inspect}")
14
+ stream
15
+ else
16
+ table_without_awesome(table, stream)
17
+ end
18
+ ensure
19
+ @types = @connection.native_database_types
20
+ end
21
+ end
22
+ end
@@ -0,0 +1 @@
1
+ require 'activerecord/mysql/awesome'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-mysql-awesome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryuta Kamizono
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Awecome patches backported for ActiveRecord MySQL adapters
84
+ email:
85
+ - kamipo@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - activerecord-mysql-awesome.gemspec
96
+ - lib/activerecord-mysql-awesome.rb
97
+ - lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract/schema_dumper.rb
98
+ - lib/activerecord-mysql-awesome/active_record/connection_adapters/abstract_mysql_adapter.rb
99
+ - lib/activerecord-mysql-awesome/active_record/schema_dumper.rb
100
+ - lib/activerecord/mysql/awesome.rb
101
+ - lib/activerecord/mysql/awesome/base.rb
102
+ - lib/activerecord/mysql/awesome/railtie.rb
103
+ - lib/activerecord/mysql/awesome/version.rb
104
+ homepage: https://github.com/kamipo/activerecord-mysql-awesome
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Awecome patches backported for ActiveRecord MySQL adapters
128
+ test_files: []