activerecord-mysql-unsigned 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 +7 -0
- data/.gitignore +21 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +11 -0
- data/activerecord-mysql-unsigned.gemspec +28 -0
- data/lib/activerecord-mysql-unsigned.rb +15 -0
- data/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_definitions.rb +51 -0
- data/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_statements.rb +35 -0
- data/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb +72 -0
- data/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/mysql2_adapter.rb +22 -0
- data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_definitions.rb +40 -0
- data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_statements.rb +35 -0
- data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter.rb +28 -0
- data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter.rb +79 -0
- data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/mysql2_adapter.rb +22 -0
- data/lib/activerecord-mysql-unsigned/base.rb +14 -0
- data/lib/activerecord-mysql-unsigned/railtie.rb +13 -0
- data/lib/activerecord-mysql-unsigned/version.rb +8 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/database_cleaner.rb +16 -0
- data/spec/support/migrations.rb +53 -0
- data/spec/support/models.rb +4 -0
- data/spec/unsigned_spec.rb +46 -0
- data/vendor/.keep +0 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e3b30a816a5a87ec9918c6d663ca68625628704
|
4
|
+
data.tar.gz: 29b8ba7a2f1c97124db38612aa44db37bf5ad68a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bce65b5d5ac3f7058b27954a8faa60513c4feed651922338a01371ee5c029fe8eca3109eaf0badc583c7868e9bd2c9452e7c6ef2d630333e7658a184de8031fa
|
7
|
+
data.tar.gz: aca9f67ba7b7a3659a2d2fe206e8cbdf424395a8ec58919656b51bd104d101793252a9124c61313f5557f2dc3548608094565861cc192eb680662d1d90a31589
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 yo_waka
|
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,66 @@
|
|
1
|
+
# Activerecord::Mysql::Unsigned [](https://travis-ci.org/waka/activerecord-mysql-unsigned)
|
2
|
+
|
3
|
+
Add unsigned option to integer type for ActiveRecord's MySQL2 adapter.
|
4
|
+
|
5
|
+
## Support version
|
6
|
+
|
7
|
+
```
|
8
|
+
ActiveRecord::VERSION >= 3.2 # include v4.0!
|
9
|
+
```
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'activerecord-mysql-unsigned'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install activerecord-mysql-unsigned
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
In your migrations you can define integer fields such as:
|
28
|
+
|
29
|
+
```
|
30
|
+
class CreateUsersTable < ActiveRecord::Migration
|
31
|
+
def self.change
|
32
|
+
create_table :users, force: true do |t|
|
33
|
+
t.string :name, null: false
|
34
|
+
t.integer :age, null: false, unsigned: true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
You can redefine in the existing fields.
|
41
|
+
|
42
|
+
```
|
43
|
+
class ChangeColumnToUsersTable < ActiveRecord::Migration
|
44
|
+
def self.change
|
45
|
+
change_column :users, :age, :integer, null: false, unsigned: false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
And you can also redefine in the primary key.
|
51
|
+
|
52
|
+
```
|
53
|
+
class ChangeColumnToUsersTable < ActiveRecord::Migration
|
54
|
+
def self.change
|
55
|
+
change_column :users, :id, :integer, null: false, auto_increment: true, unsigned: true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
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-unsigned/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-mysql-unsigned"
|
8
|
+
spec.version = ActiveRecord::Mysql::Unsigned::VERSION
|
9
|
+
spec.authors = ["yo_waka"]
|
10
|
+
spec.email = ["y.wakahara@gmail.com"]
|
11
|
+
spec.description = %q{Add unsigned option to integer type for ActiveRecord's MySQL2 adapter}
|
12
|
+
spec.summary = %q{Add unsigned option to integer type for ActiveRecord's MySQL2 adapter}
|
13
|
+
spec.homepage = "https://github.com/waka/activerecord-mysql-unsigned"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "database_cleaner"
|
25
|
+
spec.add_runtime_dependency "activesupport", ">= 3.0.0"
|
26
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0.0"
|
27
|
+
spec.add_runtime_dependency "mysql2"
|
28
|
+
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-unsigned/railtie'
|
11
|
+
else
|
12
|
+
ActiveSupport.on_load :active_record do
|
13
|
+
require 'activerecord-mysql-unsigned/base'
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract/schema_definitions'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class ColumnDefinition
|
6
|
+
def unsigned=(value)
|
7
|
+
@unsigned = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def unsigned
|
11
|
+
@unsigned
|
12
|
+
end
|
13
|
+
|
14
|
+
def auto_increment=(value)
|
15
|
+
@auto_increment = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def auto_increment
|
19
|
+
@auto_increment
|
20
|
+
end
|
21
|
+
|
22
|
+
def sql_type
|
23
|
+
base.type_to_sql(type.to_sym, limit, precision, scale, unsigned, auto_increment) rescue type
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class TableDefinition
|
28
|
+
|
29
|
+
def column(name, type, options = {})
|
30
|
+
name = name.to_s
|
31
|
+
type = type.to_sym
|
32
|
+
|
33
|
+
column = self[name] || new_column_definition(@base, name, type)
|
34
|
+
|
35
|
+
limit = options.fetch(:limit) do
|
36
|
+
native[type][:limit] if native[type].is_a?(Hash)
|
37
|
+
end
|
38
|
+
|
39
|
+
column.limit = limit
|
40
|
+
column.unsigned = options[:unsigned]
|
41
|
+
column.auto_increment = options[:auto_increment]
|
42
|
+
column.precision = options[:precision]
|
43
|
+
column.scale = options[:scale]
|
44
|
+
column.default = options[:default]
|
45
|
+
column.null = options[:null]
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
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, auto_increment = 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
|
data/lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_mysql_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class AbstractMysqlAdapter < AbstractAdapter
|
6
|
+
|
7
|
+
NATIVE_DATABASE_TYPES.merge!(
|
8
|
+
primary_key: "int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY"
|
9
|
+
)
|
10
|
+
|
11
|
+
# Maps logical Rails types to MySQL-specific data types.
|
12
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false, auto_increment = false)
|
13
|
+
case type.to_s
|
14
|
+
when 'integer'
|
15
|
+
case limit
|
16
|
+
when 1
|
17
|
+
'tinyint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
|
18
|
+
when 2
|
19
|
+
'smallint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
|
20
|
+
when 3
|
21
|
+
'mediumint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
|
22
|
+
when nil, 4, 11 # compatibility with MySQL default
|
23
|
+
if unsigned
|
24
|
+
'int(10) unsigned' + (auto_increment ? ' AUTO_INCREMENT' : '')
|
25
|
+
else
|
26
|
+
'int(10)'
|
27
|
+
end
|
28
|
+
when 5..8
|
29
|
+
'bigint' + (unsigned ? ' unsigned' : '')
|
30
|
+
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
|
31
|
+
end
|
32
|
+
when 'text'
|
33
|
+
case limit
|
34
|
+
when 0..0xff; 'tinytext'
|
35
|
+
when nil, 0x100..0xffff; 'text'
|
36
|
+
when 0x10000..0xffffff; 'mediumtext'
|
37
|
+
when 0x1000000..0xffffffff; 'longtext'
|
38
|
+
else raise(ActiveRecordError, "No text type has character length #{limit}")
|
39
|
+
end
|
40
|
+
else
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_column_sql(table_name, column_name, type, options = {})
|
46
|
+
add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned], options[:auto_increment])}"
|
47
|
+
add_column_options!(add_column_sql, options)
|
48
|
+
add_column_position!(add_column_sql, options)
|
49
|
+
add_column_sql
|
50
|
+
end
|
51
|
+
|
52
|
+
def change_column_sql(table_name, column_name, type, options = {})
|
53
|
+
column = column_for(table_name, column_name)
|
54
|
+
|
55
|
+
unless type.to_sym == :primary_key
|
56
|
+
unless options_include_default?(options)
|
57
|
+
options[:default] = column.default
|
58
|
+
end
|
59
|
+
unless options.has_key?(:null)
|
60
|
+
options[:null] = column.null
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
change_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned], options[:auto_increment])}"
|
65
|
+
add_column_options!(change_column_sql, options)
|
66
|
+
add_column_position!(change_column_sql, options)
|
67
|
+
change_column_sql
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
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)
|
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)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
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_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,28 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class AbstractAdapter
|
6
|
+
|
7
|
+
class SchemaCreation
|
8
|
+
def visit_AddColumn(o)
|
9
|
+
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale, o.unsigned)
|
10
|
+
sql = "ADD #{quote_column_name(o.name)} #{sql_type}"
|
11
|
+
add_column_options!(sql, column_options(o))
|
12
|
+
end
|
13
|
+
|
14
|
+
def visit_ColumnDefinition(o)
|
15
|
+
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale, o.unsigned)
|
16
|
+
column_sql = "#{quote_column_name(o.name)} #{sql_type}"
|
17
|
+
add_column_options!(column_sql, column_options(o)) unless o.primary_key?
|
18
|
+
column_sql
|
19
|
+
end
|
20
|
+
|
21
|
+
def type_to_sql(type, limit, precision, scale, unsigned)
|
22
|
+
@conn.type_to_sql type.to_sym, limit, precision, scale, unsigned
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_mysql_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
class AbstractMysqlAdapter < AbstractAdapter
|
6
|
+
|
7
|
+
NATIVE_DATABASE_TYPES.merge!(
|
8
|
+
:primary_key => "int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY"
|
9
|
+
)
|
10
|
+
|
11
|
+
# Maps logical Rails types to MySQL-specific data types.
|
12
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
|
13
|
+
case type.to_s
|
14
|
+
when 'binary'
|
15
|
+
case limit
|
16
|
+
when 0..0xfff; "varbinary(#{limit})"
|
17
|
+
when nil; "blob"
|
18
|
+
when 0x1000..0xffffffff; "blob(#{limit})"
|
19
|
+
else raise(ActiveRecordError, "No binary type has character length #{limit}")
|
20
|
+
end
|
21
|
+
when 'integer'
|
22
|
+
case limit
|
23
|
+
when 1
|
24
|
+
'tinyint' + (unsigned ? ' unsigned' : '')
|
25
|
+
when 2
|
26
|
+
'smallint' + (unsigned ? ' unsigned' : '')
|
27
|
+
when 3
|
28
|
+
'mediumint' + (unsigned ? ' unsigned' : '')
|
29
|
+
when nil, 4, 11 # compatibility with MySQL default
|
30
|
+
if unsigned
|
31
|
+
'int(10) unsigned'
|
32
|
+
else
|
33
|
+
'int(10)'
|
34
|
+
end
|
35
|
+
when 5..8
|
36
|
+
'bigint' + (unsigned ? ' unsigned' : '')
|
37
|
+
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
|
38
|
+
end
|
39
|
+
when 'text'
|
40
|
+
case limit
|
41
|
+
when 0..0xff; 'tinytext'
|
42
|
+
when nil, 0x100..0xffff; 'text'
|
43
|
+
when 0x10000..0xffffff; 'mediumtext'
|
44
|
+
when 0x1000000..0xffffffff; 'longtext'
|
45
|
+
else raise(ActiveRecordError, "No text type has character length #{limit}")
|
46
|
+
end
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def add_column_sql(table_name, column_name, type, options = {})
|
53
|
+
add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned])}"
|
54
|
+
add_column_options!(add_column_sql, options)
|
55
|
+
add_column_position!(add_column_sql, options)
|
56
|
+
add_column_sql
|
57
|
+
end
|
58
|
+
|
59
|
+
def change_column_sql(table_name, column_name, type, options = {})
|
60
|
+
column = column_for(table_name, column_name)
|
61
|
+
|
62
|
+
unless type.to_sym == :primary_key
|
63
|
+
unless options_include_default?(options)
|
64
|
+
options[:default] = column.default
|
65
|
+
end
|
66
|
+
unless options.has_key?(:null)
|
67
|
+
options[:null] = column.null
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
change_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned])}"
|
72
|
+
add_column_options!(change_column_sql, options)
|
73
|
+
add_column_position!(change_column_sql, options)
|
74
|
+
change_column_sql
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
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
|
@@ -0,0 +1,14 @@
|
|
1
|
+
if ActiveRecord::VERSION::MAJOR == 4
|
2
|
+
require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_definitions'
|
3
|
+
require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_statements'
|
4
|
+
require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter'
|
5
|
+
require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter'
|
6
|
+
require 'activerecord-mysql-unsigned/active_record/v4/connection_adapters/mysql2_adapter'
|
7
|
+
elsif ActiveRecord::VERSION::MAJOR == 3
|
8
|
+
require 'activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_definitions'
|
9
|
+
require 'activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_statements'
|
10
|
+
require 'activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter'
|
11
|
+
require 'activerecord-mysql-unsigned/active_record/v3/connection_adapters/mysql2_adapter'
|
12
|
+
else
|
13
|
+
raise "activerecord-mysql-unsigned supprts ActiveRecord::VERSION::MAJOR >= 3"
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Mysql
|
3
|
+
module Unsigned
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'activerecord-mysql-unsigned' do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
require 'activerecord-mysql-unsigned/base'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'activerecord-mysql-unsigned'
|
4
|
+
|
5
|
+
# Requires supporting files with custom matchers and macros, etc,
|
6
|
+
# in ./support/ and its subdirectories.
|
7
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|file| require file}
|
8
|
+
|
9
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
config.color_enabled = true
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'database_cleaner'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.before :suite do
|
5
|
+
DatabaseCleaner.strategy = :transaction
|
6
|
+
DatabaseCleaner.clean_with :truncation
|
7
|
+
end
|
8
|
+
|
9
|
+
config.before :each do
|
10
|
+
DatabaseCleaner.start
|
11
|
+
end
|
12
|
+
|
13
|
+
config.after :each do
|
14
|
+
DatabaseCleaner.clean
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
config = {
|
4
|
+
adapter: 'mysql2',
|
5
|
+
encoding: 'utf8',
|
6
|
+
database: 'activerecord_mysql_unsigned'
|
7
|
+
}
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(config.merge(database: 'mysql'))
|
10
|
+
ActiveRecord::Base.connection.drop_database(config[:database]) rescue nil
|
11
|
+
ActiveRecord::Base.connection.create_database(config[:database])
|
12
|
+
ActiveRecord::Base.establish_connection(config)
|
13
|
+
|
14
|
+
ActiveRecord::Migration.verbose = true
|
15
|
+
|
16
|
+
class CreateGoodsTable < ActiveRecord::Migration
|
17
|
+
def self.change
|
18
|
+
create_table :goods, force: true do |t|
|
19
|
+
t.string :name, null: false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
CreateGoodsTable.change
|
24
|
+
|
25
|
+
class CreateUsersTable < ActiveRecord::Migration
|
26
|
+
def self.change
|
27
|
+
create_table :users, force: true, id: false do |t|
|
28
|
+
t.column :id, "int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT" # AR's default primary_key
|
29
|
+
t.string :name, null: false
|
30
|
+
t.integer :signed_int
|
31
|
+
t.integer :unsigned_int, unsigned: true
|
32
|
+
t.integer :will_unsigned_int, unsigned: false
|
33
|
+
t.integer :will_signed_int, unsigned: true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
CreateUsersTable.change
|
38
|
+
|
39
|
+
class ChangeColumnToUsersTable < ActiveRecord::Migration
|
40
|
+
def self.change
|
41
|
+
change_column :users, :id, :integer, unsigned: true, null: false, auto_increment: true
|
42
|
+
change_column :users, :will_unsigned_int, :integer, unsigned: true
|
43
|
+
change_column :users, :will_signed_int, :integer, unsigned: false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
ChangeColumnToUsersTable.change
|
47
|
+
|
48
|
+
class AddColumnToUsersTable < ActiveRecord::Migration
|
49
|
+
def self.change
|
50
|
+
add_column :users, :added_unsigned_int, :integer, unsigned: true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
AddColumnToUsersTable.change
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "INT column" do
|
4
|
+
before(:each) do
|
5
|
+
@user = User.new(name: "bob")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "max value of signed int" do
|
9
|
+
@user.signed_int = 2147483647
|
10
|
+
expect(@user.save).to be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "max value of unsigned int" do
|
14
|
+
@user.unsigned_int = 4294967295
|
15
|
+
expect(@user.save).to be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "allowed minus value of signed int" do
|
19
|
+
@user.signed_int = -2147483648
|
20
|
+
expect(@user.save).to be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "not allowed minus value of unsigned int" do
|
24
|
+
@user.unsigned_int = -2147483648
|
25
|
+
|
26
|
+
if ActiveRecord::VERSION::MAJOR == 4
|
27
|
+
begin
|
28
|
+
@user.save
|
29
|
+
expect(true).to be_false # should not be reached here
|
30
|
+
rescue => e
|
31
|
+
expect(e).to be_an_instance_of ActiveRecord::StatementInvalid
|
32
|
+
end
|
33
|
+
else
|
34
|
+
@user.save
|
35
|
+
@user.reload
|
36
|
+
expect(@user.unsigned_int).to be 0 # saved 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "unsigned column has 'unsigned' field" do
|
41
|
+
signed_int_col = User.columns[2]
|
42
|
+
expect(signed_int_col.unsigned).to be_false
|
43
|
+
unsigned_int_col = User.columns[3]
|
44
|
+
expect(unsigned_int_col.unsigned).to be_true
|
45
|
+
end
|
46
|
+
end
|
data/vendor/.keep
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-mysql-unsigned
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yo_waka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-16 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: database_cleaner
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.0.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.0.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: mysql2
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Add unsigned option to integer type for ActiveRecord's MySQL2 adapter
|
112
|
+
email:
|
113
|
+
- y.wakahara@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .travis.yml
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- activerecord-mysql-unsigned.gemspec
|
125
|
+
- lib/activerecord-mysql-unsigned.rb
|
126
|
+
- lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_definitions.rb
|
127
|
+
- lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract/schema_statements.rb
|
128
|
+
- lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb
|
129
|
+
- lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/mysql2_adapter.rb
|
130
|
+
- lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_definitions.rb
|
131
|
+
- lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract/schema_statements.rb
|
132
|
+
- lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_adapter.rb
|
133
|
+
- lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter.rb
|
134
|
+
- lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/mysql2_adapter.rb
|
135
|
+
- lib/activerecord-mysql-unsigned/base.rb
|
136
|
+
- lib/activerecord-mysql-unsigned/railtie.rb
|
137
|
+
- lib/activerecord-mysql-unsigned/version.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
- spec/support/database_cleaner.rb
|
140
|
+
- spec/support/migrations.rb
|
141
|
+
- spec/support/models.rb
|
142
|
+
- spec/unsigned_spec.rb
|
143
|
+
- vendor/.keep
|
144
|
+
homepage: https://github.com/waka/activerecord-mysql-unsigned
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
metadata: {}
|
148
|
+
post_install_message:
|
149
|
+
rdoc_options: []
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - '>='
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubyforge_project:
|
164
|
+
rubygems_version: 2.0.7
|
165
|
+
signing_key:
|
166
|
+
specification_version: 4
|
167
|
+
summary: Add unsigned option to integer type for ActiveRecord's MySQL2 adapter
|
168
|
+
test_files:
|
169
|
+
- spec/spec_helper.rb
|
170
|
+
- spec/support/database_cleaner.rb
|
171
|
+
- spec/support/migrations.rb
|
172
|
+
- spec/support/models.rb
|
173
|
+
- spec/unsigned_spec.rb
|