activerecord-mysql-unsigned 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e070aed8bcab2b5e3cfe9a0270c7d35533a0a8e
4
- data.tar.gz: 037d6c038e805d059b798bb34c75cdc9ed0bb0de
3
+ metadata.gz: d6986cc447a7c7abec51a46bcf834c5b5e775a17
4
+ data.tar.gz: a3936fb361b69df48c1d147cbcd61c657e346105
5
5
  SHA512:
6
- metadata.gz: aa65c448724bbb17df657f1ce3ce31fabc9f286713f2219ea9984929a7985de91ab5c5b6a414fd28d6d8d326a4d44dad0df892254040547b7e23f9b5d20d9897
7
- data.tar.gz: 2ddb43e93e4f37168828dd43867854a9edc77a5bcbc1aff4d226e81b986588d3fd9df46cc29a9efba2adf90314b43b58ec167ccb459884ce4d4acd0e867b3f90
6
+ metadata.gz: 16b5249d0028891abc806b1f292df3f86c06f734c51c9d815f5a25fa97b5fc4e02bfa41db5830ed77cd21a30633394f42c29d805bf672ffa6021f993e8e93a68
7
+ data.tar.gz: a74d7a96a632d0bdf5a3e0dc4daefc9aa42fc9ce9dc2af87ecdf208e1aa96428d89bcdb389f1dedd52735c87fa91f6eceacee2f3ea530d95b6c94a01d1d9357f
data/README.md CHANGED
@@ -5,7 +5,7 @@ Add unsigned option to integer type for ActiveRecord's MySQL2 adapter.
5
5
  ## Support version
6
6
 
7
7
  ```
8
- ActiveRecord::VERSION >= 3.2 # include v4.0!
8
+ ActiveRecord::VERSION >= 3.2 # include v4.0, and v4.1!
9
9
  ```
10
10
 
11
11
  ## Installation
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "database_cleaner"
25
- spec.add_runtime_dependency "activesupport", ">= 3.2"
26
- spec.add_runtime_dependency "activerecord", ">= 3.2"
25
+ spec.add_runtime_dependency "activesupport", ">= 3.2", "<= 4.1"
26
+ spec.add_runtime_dependency "activerecord", ">= 3.2", "<= 4.1"
27
27
  spec.add_runtime_dependency "mysql2"
28
28
  end
@@ -0,0 +1,36 @@
1
+ require 'active_record/connection_adapters/abstract/schema_dumper'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+
6
+ module ColumnDumper
7
+
8
+ def prepare_column_options(column, types)
9
+ spec = {}
10
+ #binding.pry if column.name.include?("ip")
11
+ spec[:name] = column.name.inspect
12
+
13
+ # AR has an optimization which handles zero-scale decimals as integers. This
14
+ # code ensures that the dumper still dumps the column as a decimal.
15
+ spec[:type] = if column.type == :integer && /^(numeric|decimal)/ =~ column.sql_type
16
+ 'decimal'
17
+ else
18
+ column.type.to_s
19
+ end
20
+ spec[:limit] = column.limit.inspect if column.limit != types[column.type][:limit] && spec[:type] != 'decimal'
21
+ spec[:precision] = column.precision.inspect if column.precision
22
+ spec[:scale] = column.scale.inspect if column.scale
23
+ spec[:null] = 'false' unless column.null
24
+ spec[:unsigned] = 'true' if column.unsigned
25
+ spec[:default] = default_string(column.default) if column.has_default?
26
+ spec
27
+ end
28
+ # Lists the valid migration options
29
+ def migration_keys
30
+ [:name, :limit, :precision, :unsigned, :scale, :default, :null]
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_record/connection_adapters/abstract/schema_definitions'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class ColumnDefinition
6
+
7
+ def unsigned=(value)
8
+ @unsigned = value
9
+ end
10
+
11
+ def unsigned
12
+ @unsigned
13
+ end
14
+
15
+ end
16
+
17
+ class TableDefinition
18
+
19
+ def new_column_definition(name, type, options)
20
+ column = create_column_definition name, type
21
+ limit = options.fetch(:limit) do
22
+ native[type][:limit] if native[type].is_a?(Hash)
23
+ end
24
+
25
+ column.limit = limit
26
+ column.array = options[:array] if column.respond_to?(:array)
27
+ column.precision = options[:precision]
28
+ column.scale = options[:scale]
29
+ column.unsigned = options[:unsigned]
30
+ column.default = options[:default]
31
+ column.null = options[:null]
32
+ column.first = options[:first]
33
+ column.after = options[:after]
34
+ column.primary_key = type == :primary_key || options[:primary_key]
35
+ column
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_record/connection_adapters/abstract/schema_dumper'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module ColumnDumper
6
+
7
+ def prepare_column_options(column, types)
8
+ spec = {}
9
+ #binding.pry if column.name.include?("ip")
10
+ spec[:name] = column.name.inspect
11
+
12
+ # AR has an optimization which handles zero-scale decimals as integers. This
13
+ # code ensures that the dumper still dumps the column as a decimal.
14
+ spec[:type] = if column.type == :integer && /^(numeric|decimal)/ =~ column.sql_type
15
+ 'decimal'
16
+ else
17
+ column.type.to_s
18
+ end
19
+ spec[:limit] = column.limit.inspect if column.limit != types[column.type][:limit] && spec[:type] != 'decimal'
20
+ spec[:precision] = column.precision.inspect if column.precision
21
+ spec[:scale] = column.scale.inspect if column.scale
22
+ spec[:null] = 'false' unless column.null
23
+ spec[:unsigned] = 'true' if column.unsigned
24
+ spec[:default] = default_string(column.default) if column.has_default?
25
+ spec
26
+ end
27
+
28
+ # Lists the valid migration options
29
+ def migration_keys
30
+ [:name, :limit, :precision, :unsigned, :scale, :default, :null]
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_record/connection_adapters/abstract/schema_statements'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ module SchemaStatements
6
+
7
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil)
8
+ if native = native_database_types[type.to_sym]
9
+ column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
10
+
11
+ if type == :decimal
12
+ scale ||= native[:scale]
13
+
14
+ if precision ||= native[:precision]
15
+ if scale
16
+ column_type_sql << "(#{precision},#{scale})"
17
+ else
18
+ column_type_sql << "(#{precision})"
19
+ end
20
+ elsif scale
21
+ raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale is specified"
22
+ end
23
+ elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
24
+ column_type_sql << "(#{limit})"
25
+ end
26
+
27
+ column_type_sql
28
+ else
29
+ type
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,83 @@
1
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class AbstractMysqlAdapter < AbstractAdapter
6
+
7
+ class SchemaCreation < AbstractAdapter::SchemaCreation
8
+
9
+ def visit_AddColumn(o)
10
+ sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale, o.unsigned)
11
+ sql = "ADD #{quote_column_name(o.name)} #{sql_type}"
12
+ add_column_options!(sql, column_options(o))
13
+ end
14
+
15
+ def visit_ChangeColumnDefinition(o)
16
+ column = o.column
17
+ options = o.options
18
+ sql_type = type_to_sql(o.type, options[:limit], options[:precision], options[:scale], options[:unsigned])
19
+ change_column_sql = "CHANGE #{quote_column_name(column.name)} #{quote_column_name(options[:name])} #{sql_type}"
20
+ add_column_options!(change_column_sql, options)
21
+ add_column_position!(change_column_sql, options)
22
+ end
23
+
24
+ def visit_ColumnDefinition(o)
25
+ sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale, o.unsigned)
26
+ column_sql = "#{quote_column_name(o.name)} #{sql_type}"
27
+ add_column_options!(column_sql, column_options(o)) unless o.primary_key?
28
+ column_sql
29
+ end
30
+
31
+ def type_to_sql(type, limit, precision, scale, unsigned)
32
+ @conn.type_to_sql type.to_sym, limit, precision, scale, unsigned
33
+ end
34
+ end
35
+
36
+ NATIVE_DATABASE_TYPES.merge!(
37
+ :primary_key => "int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY"
38
+ )
39
+
40
+ # Maps logical Rails types to MySQL-specific data types.
41
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
42
+ case type.to_s
43
+ when 'binary'
44
+ case limit
45
+ when 0..0xfff; "varbinary(#{limit})"
46
+ when nil; "blob"
47
+ when 0x1000..0xffffffff; "blob(#{limit})"
48
+ else raise(ActiveRecordError, "No binary type has character length #{limit}")
49
+ end
50
+ when 'integer'
51
+ case limit
52
+ when 1
53
+ 'tinyint' + (unsigned ? ' unsigned' : '')
54
+ when 2
55
+ 'smallint' + (unsigned ? ' unsigned' : '')
56
+ when 3
57
+ 'mediumint' + (unsigned ? ' unsigned' : '')
58
+ when nil, 4, 11 # compatibility with MySQL default
59
+ if unsigned
60
+ 'int(10) unsigned'
61
+ else
62
+ 'int(10)'
63
+ end
64
+ when 5..8
65
+ 'bigint' + (unsigned ? ' unsigned' : '')
66
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}")
67
+ end
68
+ when 'text'
69
+ case limit
70
+ when 0..0xff; 'tinytext'
71
+ when nil, 0x100..0xffff; 'text'
72
+ when 0x10000..0xffffff; 'mediumtext'
73
+ when 0x1000000..0xffffffff; 'longtext'
74
+ else raise(ActiveRecordError, "No text type has character length #{limit}")
75
+ end
76
+ else
77
+ super
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_record/connection_adapters/mysql2_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class Mysql2Adapter < AbstractMysqlAdapter
6
+
7
+ class Column < AbstractMysqlAdapter::Column
8
+ attr_reader :unsigned
9
+
10
+ def initialize(name, default, sql_type = nil, null = true, collation = nil, strict = false, extra = "")
11
+ if sql_type.present?
12
+ @unsigned = sql_type.include? "unsigned"
13
+ else
14
+ @unsigned = false
15
+ end
16
+ super(name, default, sql_type, null, collation, strict, extra)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -1,6 +1,13 @@
1
- if ActiveRecord::VERSION::MAJOR == 4
1
+ if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 1
2
+ require 'activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_definitions'
3
+ require 'activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_statements'
4
+ require 'activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_dumper'
5
+ require 'activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract_mysql_adapter'
6
+ require 'activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/mysql2_adapter'
7
+ elsif ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0
2
8
  require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_definitions'
3
9
  require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_statements'
10
+ require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_dumper'
4
11
  require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter'
5
12
  require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter'
6
13
  require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/mysql2_adapter'
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Mysql
3
3
  module Unsigned
4
- VERSION = "0.0.3"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-mysql-unsigned
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yo_waka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,9 @@ dependencies:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '3.2'
76
+ - - "<="
77
+ - !ruby/object:Gem::Version
78
+ version: '4.1'
76
79
  type: :runtime
77
80
  prerelease: false
78
81
  version_requirements: !ruby/object:Gem::Requirement
@@ -80,6 +83,9 @@ dependencies:
80
83
  - - ">="
81
84
  - !ruby/object:Gem::Version
82
85
  version: '3.2'
86
+ - - "<="
87
+ - !ruby/object:Gem::Version
88
+ version: '4.1'
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: activerecord
85
91
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +93,9 @@ dependencies:
87
93
  - - ">="
88
94
  - !ruby/object:Gem::Version
89
95
  version: '3.2'
96
+ - - "<="
97
+ - !ruby/object:Gem::Version
98
+ version: '4.1'
90
99
  type: :runtime
91
100
  prerelease: false
92
101
  version_requirements: !ruby/object:Gem::Requirement
@@ -94,6 +103,9 @@ dependencies:
94
103
  - - ">="
95
104
  - !ruby/object:Gem::Version
96
105
  version: '3.2'
106
+ - - "<="
107
+ - !ruby/object:Gem::Version
108
+ version: '4.1'
97
109
  - !ruby/object:Gem::Dependency
98
110
  name: mysql2
99
111
  requirement: !ruby/object:Gem::Requirement
@@ -128,10 +140,16 @@ files:
128
140
  - lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb
129
141
  - lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/mysql2_adapter.rb
130
142
  - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_definitions.rb
143
+ - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_dumper.rb
131
144
  - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_statements.rb
132
145
  - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter.rb
133
146
  - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter.rb
134
147
  - lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/mysql2_adapter.rb
148
+ - lib/activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_definitions.rb
149
+ - lib/activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_dumper.rb
150
+ - lib/activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract/schema_statements.rb
151
+ - lib/activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/abstract_mysql_adapter.rb
152
+ - lib/activerecord-mysql-unsigned/active_record/v4_1/connection_adapters/mysql2_adapter.rb
135
153
  - lib/activerecord-mysql-unsigned/base.rb
136
154
  - lib/activerecord-mysql-unsigned/railtie.rb
137
155
  - lib/activerecord-mysql-unsigned/version.rb