rails-bigint-primarykey 2.0.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: 383a175ca8be5682b1eb3b6f0ae07bb8bf4949d8
4
+ data.tar.gz: f42d08c2a3a3516fe20eeebd6648a674bdfdbe2a
5
+ SHA512:
6
+ metadata.gz: 48dc303d99340c51e1225d09de971da7e1331f00b9ac1ca2074b1e532c5ced6177c0fbe1d322e2e267c8ef4e29b4e9e10971659fc6b30ed71bee3a2c9fee9956
7
+ data.tar.gz: 33e79be5a75bae39be7e021ca35f940c66c716f7a318b6ec23d78290147000abc4804f90eee027516f43d9c3d79a456653dd7009c54c024f3d9c36c2e965edeb
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2017 Shopify Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included
11
+ in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+
21
+ Copyright (c) 2012 VerticalResponse Inc.
22
+
23
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
24
+
25
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
@@ -0,0 +1,34 @@
1
+ ## rails-bigint-primarykey [![Build Status](https://secure.travis-ci.org/Shopify/rails-bigint-primarykey.png?branch=master)](https://travis-ci.org/Shopify/rails-bigint-primarykey)
2
+
3
+ ### Overview
4
+
5
+ rails-bigint-primarykey aims to be a simple, transparent way to use 64bit primary keys
6
+ in MySQL and PostgreSQL.
7
+
8
+ This gem was initially a fork of the [rails-bigint-pk](https://github.com/caboteria/rails-bigint-pk) gem but it was
9
+ significantly rewritten to support Rails 5.
10
+
11
+ ### Installation & Usage
12
+
13
+ Add the following to your `Gemfile`:
14
+
15
+ ```
16
+ gem 'rails-bigint-primarykey'
17
+ ```
18
+
19
+ ### Gotchas
20
+
21
+ When adding foreign key columns, be sure to use `references` and not
22
+ `add_column`.
23
+
24
+ ```ruby
25
+ change_table :my_table do |t|
26
+ t.references :other_table
27
+ end
28
+
29
+ # Doing this will not use 64bit ints
30
+ add_column :my_table, :other_table_id, :int
31
+ ```
32
+
33
+ When upgrading to Rails 5.1 you still need this gem if your were using it before and your migrations still use the
34
+ version 5.0 or previous.
@@ -0,0 +1,100 @@
1
+ require 'bigint_primarykey/version'
2
+ require 'bigint_primarykey/railtie'
3
+ require 'active_record'
4
+
5
+ module BigintPrimarykey
6
+ extend self
7
+
8
+ def enable!(adapter)
9
+ install_patches!(adapter)
10
+ end
11
+
12
+ private
13
+
14
+ module PostgresBigintPrimaryKey
15
+ def primary_key(name, type = :primary_key, **options)
16
+ if type == :primary_key
17
+ super(name, :bigserial, **options)
18
+ else
19
+ super
20
+ end
21
+ end
22
+ end
23
+
24
+ module MysqlBigintPrimaryKey
25
+ def primary_key(name, type = :primary_key, **options)
26
+ if type == :primary_key
27
+ options[:auto_increment] = true unless options.key?(:default)
28
+ super(name, ActiveRecord::VERSION::MAJOR < 5 ? :primary_key : :bigint, **options)
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
34
+
35
+ module DefaultBigintForeignKeyReferences
36
+ def references(*args)
37
+ options = args.extract_options!
38
+ options.reverse_merge! limit: 8
39
+ # Limit shouldn't affect "#{col}_type" column in polymorphic reference.
40
+ # But don't change value if it isn't simple 'true'.
41
+ # Examples:
42
+ # t.references :subject, null: false, polymorphic: true ==> t.integer :subject_id, limit: 8, null: false
43
+ # t.string :subject_type, null: false
44
+ # t.references :subject, polymorphic: { limit: 120 } ==> t.integer :subject_id, limit: 8
45
+ # t.string :subject_type, limit: 120
46
+ options[:polymorphic] = options.except(:polymorphic, :limit) if options[:polymorphic] == true
47
+ super(*args, options)
48
+ end
49
+ end
50
+
51
+ module CompatibilityWithBigint
52
+ def self.included(base)
53
+ base.remove_possible_method :create_table
54
+ end
55
+
56
+ def create_table(table_name, options = {})
57
+ if adapter_name == "PostgreSQL"
58
+ if options[:id] == :uuid && !options.key?(:default)
59
+ options[:default] = "uuid_generate_v4()"
60
+ end
61
+ end
62
+
63
+ unless adapter_name == "Mysql2" && options[:id] == :bigint
64
+ if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
65
+ options[:default] = nil
66
+ end
67
+ end
68
+
69
+ super
70
+ end
71
+ end
72
+
73
+ def install_patches!(adapter)
74
+ if ActiveRecord.gem_version >= Gem::Version.new("5.1")
75
+ ActiveRecord::Migration::Compatibility::V5_0.include CompatibilityWithBigint
76
+ else
77
+ ca = ActiveRecord::ConnectionAdapters
78
+
79
+ case adapter
80
+ when 'postgresql'
81
+ primarykey_module = PostgresBigintPrimaryKey
82
+ require 'active_record/connection_adapters/postgresql_adapter'
83
+ ca::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'
84
+ when /mysql\d+/
85
+ primarykey_module = MysqlBigintPrimaryKey
86
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
87
+ ca::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigint(20) auto_increment PRIMARY KEY'
88
+ ca::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:integer] = { name: "bigint", limit: 6 }
89
+ else
90
+ raise "Only MySQL and PostgreSQL adapters are supported now. Tried to patch #{adapter}."
91
+ end
92
+
93
+ [ca::TableDefinition,
94
+ ca::Table].each do |abstract_table_type|
95
+ abstract_table_type.prepend(primarykey_module)
96
+ abstract_table_type.prepend(DefaultBigintForeignKeyReferences)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/railtie'
2
+
3
+ module BigintPrimarykey
4
+ class Railtie < Rails::Railtie
5
+ initializer 'bigint_primarykey.install' do
6
+ ActiveSupport.on_load(:active_record) do
7
+ config = configurations[Rails.env]
8
+ BigintPrimarykey.enable!(config['adapter'])
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module BigintPrimarykey
2
+ VERSION = "2.0.0"
3
+ end
@@ -0,0 +1 @@
1
+ require 'bigint_primarykey'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-bigint-primarykey
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafael Mendonça França
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
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: pg
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
+ description: Easily use 64-bit primary keys in Rails
70
+ email:
71
+ - gems@shopify.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - Readme.md
78
+ - lib/bigint_primarykey.rb
79
+ - lib/bigint_primarykey/railtie.rb
80
+ - lib/bigint_primarykey/version.rb
81
+ - lib/rails-bigint-primarykey.rb
82
+ homepage: https://github.com/Shopify/rails-bigint-primarykey
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.2.2
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Easily use 64-bit primary keys in Rails
105
+ test_files: []