activerecord-mysql-unsigned 0.1.3 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a191990d328533d3c11ad7364dfe8bb039885b20
4
- data.tar.gz: f0b1b0baafd8b7c53a4cf9dc0434c98277f0687a
3
+ metadata.gz: bb8f5f38a9a5fd9b1b15e1368f3c7958b2c8719a
4
+ data.tar.gz: fdb22adc25e2d8da3752f01e92faf34064e9badd
5
5
  SHA512:
6
- metadata.gz: dbf0d9f8d490528fa43316aed3a46c44711ce33ca881c86be5f4c8cd23fc500d548be9860640311f5f5f9aadf51c8d27557c1ea5df750f3f0649110d295cd8e0
7
- data.tar.gz: 2c74230976b513f0c9fe11b318c6cc332db888ab07a79f54b818e46779b910df81cf03a3bfccbc25edd651283ca763820e147ba3bc52c9413a83e19b4e144411
6
+ metadata.gz: 0652a2d5af79c1ffd3a466240f2d6e5cd357c3997bab5279314632c6e5313f6c70dd6e79f4027a2383882e3b9c80fe1c706a6aa1d394beb2492c53ca38939c2f
7
+ data.tar.gz: 5bebff108fcc8bbdf786e8e71e8549ce1b5251ab0c19418a22616c54c48397a7e974fed08f54856c754ec332832b891900da6dc03814bd6c25a5228e71c23450
@@ -10,7 +10,28 @@ module ActiveRecord
10
10
 
11
11
  # Maps logical Rails types to MySQL-specific data types.
12
12
  def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
13
+ # return earlier, only need overwrite method when unsigned option == true
14
+ return super unless unsigned
15
+
13
16
  case type.to_s
17
+ when 'decimal'
18
+ # copy from rails core
19
+ # https://github.com/rails/rails/blob/600aaf4234c80064201ee34ddabed216b91559db/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
20
+ native = native_database_types[type.to_sym]
21
+ column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
22
+
23
+ scale ||= native[:scale]
24
+
25
+ if precision ||= native[:precision]
26
+ if scale
27
+ column_type_sql << "(#{precision},#{scale}) unsigned"
28
+ else
29
+ column_type_sql << "(#{precision}) unsigned"
30
+ end
31
+ elsif scale
32
+ raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale is specified"
33
+ end
34
+
14
35
  when 'binary'
15
36
  case limit
16
37
  when 0..0xfff; "varbinary(#{limit})"
@@ -21,29 +42,17 @@ module ActiveRecord
21
42
  when 'integer'
22
43
  case limit
23
44
  when 1
24
- 'tinyint' + (unsigned ? ' unsigned' : '')
45
+ 'tinyint unsigned'
25
46
  when 2
26
- 'smallint' + (unsigned ? ' unsigned' : '')
47
+ 'smallint unsigned'
27
48
  when 3
28
- 'mediumint' + (unsigned ? ' unsigned' : '')
49
+ 'mediumint unsigned'
29
50
  when nil, 4, 11 # compatibility with MySQL default
30
- if unsigned
31
- 'int(10) unsigned'
32
- else
33
- 'int(10)'
34
- end
51
+ 'int(10) unsigned'
35
52
  when 5..8
36
- 'bigint' + (unsigned ? ' unsigned' : '')
53
+ 'bigint unsigned'
37
54
  else raise(ActiveRecordError, "No integer type has byte size #{limit}")
38
55
  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
56
  else
48
57
  super
49
58
  end
@@ -41,7 +41,28 @@ module ActiveRecord
41
41
 
42
42
  # Maps logical Rails types to MySQL-specific data types.
43
43
  def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false)
44
+ # return earlier, only need overwrite method when unsigned option == true
45
+ return super unless unsigned
46
+
47
+
44
48
  case type.to_s
49
+ when 'decimal'
50
+ # copy from rails core
51
+ # https://github.com/rails/rails/blob/600aaf4234c80064201ee34ddabed216b91559db/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
52
+ native = native_database_types[type.to_sym]
53
+ column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
54
+
55
+ scale ||= native[:scale]
56
+
57
+ if precision ||= native[:precision]
58
+ if scale
59
+ column_type_sql << "(#{precision},#{scale}) unsigned"
60
+ else
61
+ column_type_sql << "(#{precision}) unsigned"
62
+ end
63
+ elsif scale
64
+ raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale is specified"
65
+ end
45
66
  when 'binary'
46
67
  case limit
47
68
  when 0..0xfff; "varbinary(#{limit})"
@@ -67,14 +88,6 @@ module ActiveRecord
67
88
  'bigint' + (unsigned ? ' unsigned' : '')
68
89
  else raise(ActiveRecordError, "No integer type has byte size #{limit}")
69
90
  end
70
- when 'text'
71
- case limit
72
- when 0..0xff; 'tinytext'
73
- when nil, 0x100..0xffff; 'text'
74
- when 0x10000..0xffffff; 'mediumtext'
75
- when 0x1000000..0xffffffff; 'longtext'
76
- else raise(ActiveRecordError, "No text type has character length #{limit}")
77
- end
78
91
  else
79
92
  super
80
93
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Mysql
3
3
  module Unsigned
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -36,6 +36,8 @@ class CreateUsersTable < ActiveRecord::Migration
36
36
  t.integer :will_unsigned_int, unsigned: false
37
37
  t.integer :will_signed_int, unsigned: true
38
38
  t.integer :will_bigint
39
+ t.decimal :signed_decimal, null: false, default: 0, precision: 15, scale: 2
40
+ t.decimal :unsigned_decimal, null: false, default: 0, unsigned: true, precision: 15, scale: 2
39
41
  end
40
42
  end
41
43
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "INT column" do
3
+ describe "INT/Decimal column" do
4
4
 
5
5
  before :all do
6
6
  ChangePrimaryKeyToGoodsTable.change
@@ -51,4 +51,27 @@ describe "INT column" do
51
51
  unsigned_int_col = User.columns[3]
52
52
  expect(unsigned_int_col.unsigned).to be_truthy
53
53
  end
54
+
55
+ it "allowed minus value of signed decimal" do
56
+ @user.signed_decimal = -10.0
57
+ @user.save
58
+ expect(@user.signed_decimal).to eq(-10.0)
59
+ end
60
+
61
+ it "not allowed minus value of unsigned decimal" do
62
+ @user.unsigned_decimal = -10
63
+
64
+ if ActiveRecord::VERSION::MAJOR == 4
65
+ begin
66
+ @user.save
67
+ expect(true).to be_falsey # should not be reached here
68
+ rescue => e
69
+ expect(e).to be_an_instance_of ActiveRecord::StatementInvalid
70
+ end
71
+ else
72
+ @user.save
73
+ @user.reload
74
+ expect(@user.unsigned_int).to be 0 # saved 0
75
+ end
76
+ end
54
77
  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.1.3
4
+ version: 0.2.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-06-29 00:00:00.000000000 Z
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -180,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  version: '0'
181
181
  requirements: []
182
182
  rubyforge_project:
183
- rubygems_version: 2.2.2
183
+ rubygems_version: 2.2.0
184
184
  signing_key:
185
185
  specification_version: 4
186
186
  summary: Add unsigned option to integer type for ActiveRecord's MySQL2 adapter