pg_random_id 0.1.0 → 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.
- data/README.md +26 -0
- data/lib/pg_random_id/migrations.rb +15 -0
- data/lib/pg_random_id/sql.rb +15 -0
- data/lib/pg_random_id/version.rb +1 -1
- data/pg_random_id.gemspec +1 -1
- data/spec/helpers/migration_spec_helper.rb +30 -0
- data/spec/sql/crockford_spec.rb +0 -1
- data/spec/sql/scramble_spec.rb +0 -1
- metadata +17 -1
data/README.md
CHANGED
@@ -40,6 +40,10 @@ class InstallRandomId < ActiveRecord::Migration
|
|
40
40
|
# install the necessary SQL functions in the DB
|
41
41
|
create_random_id_functions
|
42
42
|
end
|
43
|
+
|
44
|
+
def down
|
45
|
+
drop_random_id_functions
|
46
|
+
end
|
43
47
|
end
|
44
48
|
|
45
49
|
class CreateProducts < ActiveRecord::Migration
|
@@ -51,6 +55,11 @@ class CreateProducts < ActiveRecord::Migration
|
|
51
55
|
# make the table use random ids
|
52
56
|
random_id :products
|
53
57
|
end
|
58
|
+
|
59
|
+
def down
|
60
|
+
# the random id will be removed along with the table
|
61
|
+
drop_table :products
|
62
|
+
end
|
54
63
|
end
|
55
64
|
|
56
65
|
class RandomizeIdsOnWidgets < ActiveRecord::Migration
|
@@ -59,6 +68,10 @@ class RandomizeIdsOnWidgets < ActiveRecord::Migration
|
|
59
68
|
# 'widgets' random (using string ids)
|
60
69
|
random_str_id :widgets, :widget_id # you can specify id column name
|
61
70
|
end
|
71
|
+
|
72
|
+
def down
|
73
|
+
remove_random_id :widgets, :widget_id
|
74
|
+
end
|
62
75
|
end
|
63
76
|
```
|
64
77
|
|
@@ -70,6 +83,10 @@ Sequel.migration do
|
|
70
83
|
# install the necessary SQL functions in the DB
|
71
84
|
create_random_id_functions
|
72
85
|
end
|
86
|
+
|
87
|
+
down do
|
88
|
+
drop_random_id_functions
|
89
|
+
end
|
73
90
|
end
|
74
91
|
|
75
92
|
Sequel.migration do
|
@@ -82,6 +99,11 @@ Sequel.migration do
|
|
82
99
|
# make the table use random ids
|
83
100
|
random_id :products
|
84
101
|
end
|
102
|
+
|
103
|
+
down do
|
104
|
+
# the random id will be removed along with the table
|
105
|
+
drop_table :products
|
106
|
+
end
|
85
107
|
end
|
86
108
|
|
87
109
|
Sequel.migration do
|
@@ -90,6 +112,10 @@ Sequel.migration do
|
|
90
112
|
# 'widgets' random (using string ids)
|
91
113
|
random_str_id :widgets, :widget_id # you can specify id column name
|
92
114
|
end
|
115
|
+
|
116
|
+
down do
|
117
|
+
remove_random_id :widgets, :widget_id
|
118
|
+
end
|
93
119
|
end
|
94
120
|
```
|
95
121
|
|
@@ -8,6 +8,11 @@ module PgRandomId
|
|
8
8
|
execute PgRandomId::Sql::install
|
9
9
|
end
|
10
10
|
|
11
|
+
# Drop the functions installed by #create_random_id_functions
|
12
|
+
def drop_random_id_functions
|
13
|
+
execute PgRandomId::Sql::uninstall
|
14
|
+
end
|
15
|
+
|
11
16
|
# Apply a random id to a table.
|
12
17
|
# If you don't give a key, a random one will be generated.
|
13
18
|
# The ids will be based on sequence "#{table}_#{column}_seq".
|
@@ -16,6 +21,16 @@ module PgRandomId
|
|
16
21
|
execute PgRandomId::Sql::apply(table, column, key)
|
17
22
|
end
|
18
23
|
|
24
|
+
# Changes type of a column to int and restores sequence default on it.
|
25
|
+
#
|
26
|
+
# This is mainly useful for a down migration while developing the database and switching back and forth.
|
27
|
+
# Not needed in real use, as dropping a table will obliterate the default value anyway.
|
28
|
+
#
|
29
|
+
# You need to make sure the table is empty; migrating existing records is not implemented.
|
30
|
+
def remove_random_id table, column = :id
|
31
|
+
execute PgRandomId::Sql::unapply(table, column)
|
32
|
+
end
|
33
|
+
|
19
34
|
# Apply a random string id to a table.
|
20
35
|
# Also changes the type of the id column to char(6).
|
21
36
|
# If you don't give a key, a random one will be generated.
|
data/lib/pg_random_id/sql.rb
CHANGED
@@ -5,12 +5,27 @@ module PgRandomId
|
|
5
5
|
FILES.map {|f| read_file f}.join
|
6
6
|
end
|
7
7
|
|
8
|
+
def uninstall
|
9
|
+
"""
|
10
|
+
DROP FUNCTION crockford(input bigint);
|
11
|
+
DROP FUNCTION pri_scramble(key bigint, input bigint);
|
12
|
+
"""
|
13
|
+
end
|
14
|
+
|
8
15
|
def apply table, column, key = nil, base = nil
|
9
16
|
key ||= rand(2**15)
|
10
17
|
base ||= sequence_nextval "#{table}_#{column}_seq"
|
11
18
|
"ALTER TABLE #{table} ALTER COLUMN #{column} SET DEFAULT pri_scramble(#{key}, #{base})"
|
12
19
|
end
|
13
20
|
|
21
|
+
def unapply table, column, base = nil
|
22
|
+
base ||= sequence_nextval "#{table}_#{column}_seq"
|
23
|
+
"""
|
24
|
+
ALTER TABLE #{table} ALTER COLUMN #{column} SET DEFAULT #{base};
|
25
|
+
ALTER TABLE #{table} ALTER COLUMN #{column} SET DATA TYPE integer USING 0;
|
26
|
+
"""
|
27
|
+
end
|
28
|
+
|
14
29
|
def apply_str table, column, key = nil, base = nil
|
15
30
|
key ||= rand(2**15)
|
16
31
|
base ||= sequence_nextval "#{table}_#{column}_seq"
|
data/lib/pg_random_id/version.rb
CHANGED
data/pg_random_id.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
%w(activerecord rspec sequel).each do |g|
|
20
|
+
%w(activerecord rspec sequel pg).each do |g|
|
21
21
|
gem.add_development_dependency g
|
22
22
|
end
|
23
23
|
end
|
@@ -8,6 +8,16 @@ shared_context 'test_migration' do
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
describe '#drop_random_id_functions' do
|
12
|
+
it "removes the functions" do
|
13
|
+
migration.create_random_id_functions
|
14
|
+
migration.drop_random_id_functions
|
15
|
+
|
16
|
+
execute("SELECT 1 FROM pg_proc WHERE proname = 'crockford'").should_not be
|
17
|
+
execute("SELECT 1 FROM pg_proc WHERE proname = 'pri_scramble'").should_not be
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
11
21
|
describe '#random_id' do
|
12
22
|
it "changes the default value" do
|
13
23
|
migration.create_random_id_functions
|
@@ -28,6 +38,26 @@ shared_context 'test_migration' do
|
|
28
38
|
execute("SELECT COUNT(*) FROM foo").first[1].to_i.should == 10
|
29
39
|
end
|
30
40
|
end
|
41
|
+
|
42
|
+
describe '#remove_random_id' do
|
43
|
+
it "restores the default value" do
|
44
|
+
migration.create_random_id_functions
|
45
|
+
create_table :foo
|
46
|
+
migration.random_id :foo
|
47
|
+
migration.remove_random_id :foo
|
48
|
+
execute("SELECT 1 FROM pg_attrdef WHERE adrelid = 'foo'::regclass AND adsrc LIKE '%pri_scramble%'").should_not be
|
49
|
+
execute("INSERT INTO foo VALUES (DEFAULT) RETURNING id;").first[1].to_i.should == 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "also works with str id" do
|
53
|
+
migration.create_random_id_functions
|
54
|
+
create_table :foo
|
55
|
+
migration.random_str_id :foo
|
56
|
+
migration.remove_random_id :foo
|
57
|
+
execute("SELECT 1 FROM pg_attrdef WHERE adrelid = 'foo'::regclass AND adsrc LIKE '%pri_scramble%'").should_not be
|
58
|
+
execute("INSERT INTO foo VALUES (DEFAULT) RETURNING id;").first[1].to_i.should == 1
|
59
|
+
end
|
60
|
+
end
|
31
61
|
|
32
62
|
describe '#random_str_id' do
|
33
63
|
it "changes the default value" do
|
data/spec/sql/crockford_spec.rb
CHANGED
data/spec/sql/scramble_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_random_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pg
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
62
78
|
description: Easily use randomized keys instead of sequential values for your record
|
63
79
|
surrogate ids.
|
64
80
|
email:
|