ardb 0.28.3 → 0.30.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 +7 -7
- data/.l.yml +9 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -0
- data/.t.yml +6 -0
- data/Gemfile +24 -8
- data/README.md +252 -3
- data/ardb.gemspec +14 -10
- data/bin/ardb +3 -1
- data/lib/ardb/adapter/base.rb +72 -47
- data/lib/ardb/adapter/mysql.rb +4 -17
- data/lib/ardb/adapter/postgresql.rb +51 -46
- data/lib/ardb/adapter/sqlite.rb +11 -15
- data/lib/ardb/adapter_spy.rb +18 -30
- data/lib/ardb/cli/clirb.rb +16 -18
- data/lib/ardb/cli/commands.rb +308 -129
- data/lib/ardb/cli.rb +29 -24
- data/lib/ardb/db_tests.rb +4 -4
- data/lib/ardb/default_order_by.rb +13 -21
- data/lib/ardb/migration.rb +15 -16
- data/lib/ardb/record_spy.rb +46 -61
- data/lib/ardb/relation_spy.rb +28 -32
- data/lib/ardb/require_autoloaded_active_record_files.rb +258 -57
- data/lib/ardb/test_helpers.rb +33 -29
- data/lib/ardb/use_db_default.rb +13 -21
- data/lib/ardb/version.rb +3 -1
- data/lib/ardb.rb +105 -86
- data/script/determine_autoloaded_active_record_files.rb +31 -24
- data/test/helper.rb +6 -13
- data/test/support/factory.rb +4 -3
- data/test/support/fake_schema.rb +3 -1
- data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
- data/test/support/postgresql/schema.rb +2 -1
- data/test/support/postgresql/setup_test_db.rb +23 -21
- data/test/support/relative_require_test_db_file.rb +1 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/system/.keep +0 -0
- data/test/unit/adapter/base_tests.rb +80 -55
- data/test/unit/adapter/mysql_tests.rb +4 -19
- data/test/unit/adapter/postgresql_tests.rb +21 -30
- data/test/unit/adapter/sqlite_tests.rb +5 -11
- data/test/unit/adapter_spy_tests.rb +6 -17
- data/test/unit/ardb_tests.rb +75 -53
- data/test/unit/cli_tests.rb +234 -158
- data/test/unit/db_tests_tests.rb +7 -7
- data/test/unit/default_order_by_tests.rb +26 -24
- data/test/unit/migration_tests.rb +17 -18
- data/test/unit/record_spy_tests.rb +45 -41
- data/test/unit/relation_spy_tests.rb +40 -63
- data/test/unit/test_helpers_tests.rb +7 -15
- data/test/unit/use_db_default_tests.rb +35 -27
- metadata +109 -87
- data/lib/ardb/has_slug.rb +0 -107
- data/lib/ardb/migration_helpers.rb +0 -77
- data/lib/ardb/pg_json.rb +0 -90
- data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
- data/test/system/pg_json_tests.rb +0 -85
- data/test/unit/has_slug_tests.rb +0 -341
- data/test/unit/migration_helpers_tests.rb +0 -65
- data/test/unit/pg_json_tests.rb +0 -39
metadata
CHANGED
@@ -1,78 +1,114 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ardb
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.30.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Kelly Redding
|
8
8
|
- Collin Redding
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
date: 2022-01-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: much-style-guide
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.6.7
|
21
|
+
type: :development
|
17
22
|
prerelease: false
|
18
|
-
|
19
|
-
requirements:
|
20
|
-
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.6.7
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: assert
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.19.8
|
23
35
|
type: :development
|
24
|
-
version_requirements: *id001
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: activerecord
|
27
36
|
prerelease: false
|
28
|
-
|
29
|
-
requirements:
|
30
|
-
- - ~>
|
31
|
-
-
|
32
|
-
version:
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.19.8
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: activerecord
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
33
49
|
type: :runtime
|
34
|
-
version_requirements: *id002
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: activesupport
|
37
50
|
prerelease: false
|
38
|
-
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
-
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: activesupport
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '5.0'
|
42
63
|
type: :runtime
|
43
|
-
version_requirements: *id004
|
44
|
-
- !ruby/object:Gem::Dependency
|
45
|
-
name: much-plugin
|
46
64
|
prerelease: false
|
47
|
-
|
48
|
-
requirements:
|
49
|
-
- -
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
version:
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '5.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: much-mixin
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.2.5
|
52
77
|
type: :runtime
|
53
|
-
version_requirements: *id005
|
54
|
-
- !ruby/object:Gem::Dependency
|
55
|
-
name: scmd
|
56
78
|
prerelease: false
|
57
|
-
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 0.2.5
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: scmd
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 3.0.5
|
62
91
|
type: :runtime
|
63
|
-
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 3.0.5
|
64
98
|
description: Activerecord database tools.
|
65
|
-
email:
|
99
|
+
email:
|
66
100
|
- kelly@kellyredding.com
|
67
101
|
- collin.redding@me.com
|
68
|
-
executables:
|
102
|
+
executables:
|
69
103
|
- ardb
|
70
104
|
extensions: []
|
71
|
-
|
72
105
|
extra_rdoc_files: []
|
73
|
-
|
74
|
-
|
75
|
-
- .
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- ".l.yml"
|
109
|
+
- ".rubocop.yml"
|
110
|
+
- ".ruby-version"
|
111
|
+
- ".t.yml"
|
76
112
|
- Gemfile
|
77
113
|
- LICENSE
|
78
114
|
- README.md
|
@@ -89,10 +125,7 @@ files:
|
|
89
125
|
- lib/ardb/cli/commands.rb
|
90
126
|
- lib/ardb/db_tests.rb
|
91
127
|
- lib/ardb/default_order_by.rb
|
92
|
-
- lib/ardb/has_slug.rb
|
93
128
|
- lib/ardb/migration.rb
|
94
|
-
- lib/ardb/migration_helpers.rb
|
95
|
-
- lib/ardb/pg_json.rb
|
96
129
|
- lib/ardb/record_spy.rb
|
97
130
|
- lib/ardb/relation_spy.rb
|
98
131
|
- lib/ardb/require_autoloaded_active_record_files.rb
|
@@ -104,13 +137,12 @@ files:
|
|
104
137
|
- test/helper.rb
|
105
138
|
- test/support/factory.rb
|
106
139
|
- test/support/fake_schema.rb
|
107
|
-
- test/support/postgresql/migrations/.
|
108
|
-
- test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb
|
140
|
+
- test/support/postgresql/migrations/.keep
|
109
141
|
- test/support/postgresql/schema.rb
|
110
142
|
- test/support/postgresql/setup_test_db.rb
|
111
143
|
- test/support/relative_require_test_db_file.rb
|
112
144
|
- test/support/require_test_db_file.rb
|
113
|
-
- test/system
|
145
|
+
- test/system/.keep
|
114
146
|
- test/unit/adapter/base_tests.rb
|
115
147
|
- test/unit/adapter/mysql_tests.rb
|
116
148
|
- test/unit/adapter/postgresql_tests.rb
|
@@ -120,52 +152,45 @@ files:
|
|
120
152
|
- test/unit/cli_tests.rb
|
121
153
|
- test/unit/db_tests_tests.rb
|
122
154
|
- test/unit/default_order_by_tests.rb
|
123
|
-
- test/unit/has_slug_tests.rb
|
124
|
-
- test/unit/migration_helpers_tests.rb
|
125
155
|
- test/unit/migration_tests.rb
|
126
|
-
- test/unit/pg_json_tests.rb
|
127
156
|
- test/unit/record_spy_tests.rb
|
128
157
|
- test/unit/relation_spy_tests.rb
|
129
158
|
- test/unit/test_helpers_tests.rb
|
130
159
|
- test/unit/use_db_default_tests.rb
|
131
160
|
- tmp/.gitkeep
|
132
161
|
homepage: http://github.com/redding/ardb
|
133
|
-
licenses:
|
162
|
+
licenses:
|
134
163
|
- MIT
|
135
164
|
metadata: {}
|
136
|
-
|
137
|
-
post_install_message:
|
165
|
+
post_install_message:
|
138
166
|
rdoc_options: []
|
139
|
-
|
140
|
-
require_paths:
|
167
|
+
require_paths:
|
141
168
|
- lib
|
142
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
-
requirements:
|
144
|
-
-
|
145
|
-
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
169
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '2.5'
|
174
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
151
179
|
requirements: []
|
152
|
-
|
153
|
-
|
154
|
-
rubygems_version: 2.5.1
|
155
|
-
signing_key:
|
180
|
+
rubygems_version: 3.3.5
|
181
|
+
signing_key:
|
156
182
|
specification_version: 4
|
157
183
|
summary: Activerecord database tools.
|
158
|
-
test_files:
|
184
|
+
test_files:
|
159
185
|
- test/helper.rb
|
160
186
|
- test/support/factory.rb
|
161
187
|
- test/support/fake_schema.rb
|
162
|
-
- test/support/postgresql/migrations/.
|
163
|
-
- test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb
|
188
|
+
- test/support/postgresql/migrations/.keep
|
164
189
|
- test/support/postgresql/schema.rb
|
165
190
|
- test/support/postgresql/setup_test_db.rb
|
166
191
|
- test/support/relative_require_test_db_file.rb
|
167
192
|
- test/support/require_test_db_file.rb
|
168
|
-
- test/system
|
193
|
+
- test/system/.keep
|
169
194
|
- test/unit/adapter/base_tests.rb
|
170
195
|
- test/unit/adapter/mysql_tests.rb
|
171
196
|
- test/unit/adapter/postgresql_tests.rb
|
@@ -175,10 +200,7 @@ test_files:
|
|
175
200
|
- test/unit/cli_tests.rb
|
176
201
|
- test/unit/db_tests_tests.rb
|
177
202
|
- test/unit/default_order_by_tests.rb
|
178
|
-
- test/unit/has_slug_tests.rb
|
179
|
-
- test/unit/migration_helpers_tests.rb
|
180
203
|
- test/unit/migration_tests.rb
|
181
|
-
- test/unit/pg_json_tests.rb
|
182
204
|
- test/unit/record_spy_tests.rb
|
183
205
|
- test/unit/relation_spy_tests.rb
|
184
206
|
- test/unit/test_helpers_tests.rb
|
data/lib/ardb/has_slug.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
require 'much-plugin'
|
2
|
-
|
3
|
-
module Ardb
|
4
|
-
|
5
|
-
module HasSlug
|
6
|
-
include MuchPlugin
|
7
|
-
|
8
|
-
DEFAULT_ATTRIBUTE = :slug
|
9
|
-
DEFAULT_PREPROCESSOR = :downcase
|
10
|
-
DEFAULT_SEPARATOR = '-'.freeze
|
11
|
-
|
12
|
-
plugin_included do
|
13
|
-
extend ClassMethods
|
14
|
-
include InstanceMethods
|
15
|
-
|
16
|
-
@ardb_has_slug_configs = Hash.new{ |h, k| h[k] = {} }
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
|
21
|
-
def has_slug(options = nil)
|
22
|
-
options ||= {}
|
23
|
-
raise(ArgumentError, "a source must be provided") unless options[:source]
|
24
|
-
|
25
|
-
attribute = (options[:attribute] || DEFAULT_ATTRIBUTE).to_sym
|
26
|
-
@ardb_has_slug_configs[attribute].merge!({
|
27
|
-
:source_proc => options[:source].to_proc,
|
28
|
-
:preprocessor_proc => (options[:preprocessor] || DEFAULT_PREPROCESSOR).to_proc,
|
29
|
-
:separator => options[:separator] || DEFAULT_SEPARATOR,
|
30
|
-
:allow_underscores => !!options[:allow_underscores]
|
31
|
-
})
|
32
|
-
|
33
|
-
# since the slug isn't written till an after callback we can't always
|
34
|
-
# validate presence of it
|
35
|
-
validates_presence_of(attribute, :on => :update)
|
36
|
-
|
37
|
-
if options[:skip_unique_validation] != true
|
38
|
-
validates_uniqueness_of(attribute, {
|
39
|
-
:case_sensitive => true,
|
40
|
-
:scope => options[:unique_scope]
|
41
|
-
})
|
42
|
-
end
|
43
|
-
|
44
|
-
after_create :ardb_has_slug_generate_slugs
|
45
|
-
after_update :ardb_has_slug_generate_slugs
|
46
|
-
end
|
47
|
-
|
48
|
-
def ardb_has_slug_configs
|
49
|
-
@ardb_has_slug_configs
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
module InstanceMethods
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def reset_slug(attribute = nil)
|
59
|
-
attribute ||= DEFAULT_ATTRIBUTE
|
60
|
-
self.send("#{attribute}=", nil)
|
61
|
-
end
|
62
|
-
|
63
|
-
def ardb_has_slug_generate_slugs
|
64
|
-
self.class.ardb_has_slug_configs.each do |attr_name, config|
|
65
|
-
slug_source = if !self.send(attr_name) || self.send(attr_name).to_s.empty?
|
66
|
-
self.instance_eval(&config[:source_proc])
|
67
|
-
else
|
68
|
-
self.send(attr_name)
|
69
|
-
end
|
70
|
-
|
71
|
-
generated_slug = Slug.new(slug_source, {
|
72
|
-
:preprocessor => config[:preprocessor_proc],
|
73
|
-
:separator => config[:separator],
|
74
|
-
:allow_underscores => config[:allow_underscores]
|
75
|
-
})
|
76
|
-
next if self.send(attr_name) == generated_slug
|
77
|
-
self.send("#{attr_name}=", generated_slug)
|
78
|
-
self.update_column(attr_name, generated_slug)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
module Slug
|
85
|
-
def self.new(string, options = nil)
|
86
|
-
options ||= {}
|
87
|
-
preprocessor = options[:preprocessor]
|
88
|
-
separator = options[:separator]
|
89
|
-
allow_underscores = options[:allow_underscores]
|
90
|
-
regexp_escaped_sep = Regexp.escape(separator)
|
91
|
-
|
92
|
-
slug = preprocessor.call(string.to_s.dup)
|
93
|
-
# Turn unwanted chars into the separator
|
94
|
-
slug.gsub!(/[^\w#{regexp_escaped_sep}]+/, separator)
|
95
|
-
# Turn underscores into the separator, unless allowing
|
96
|
-
slug.gsub!(/_/, separator) unless allow_underscores
|
97
|
-
# No more than one of the separator in a row.
|
98
|
-
slug.gsub!(/#{regexp_escaped_sep}{2,}/, separator)
|
99
|
-
# Remove leading/trailing separator.
|
100
|
-
slug.gsub!(/\A#{regexp_escaped_sep}|#{regexp_escaped_sep}\z/, '')
|
101
|
-
slug
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'ardb'
|
2
|
-
|
3
|
-
module Ardb
|
4
|
-
|
5
|
-
module MigrationHelpers
|
6
|
-
module_function
|
7
|
-
|
8
|
-
def foreign_key(from_table, from_column, to_table, options={})
|
9
|
-
fk = ForeignKey.new(from_table, from_column, to_table, options)
|
10
|
-
execute(fk.add_sql)
|
11
|
-
end
|
12
|
-
|
13
|
-
def drop_foreign_key(*args)
|
14
|
-
from_table, from_column = args[0..1]
|
15
|
-
options = args.last.kind_of?(Hash) ? args.last : {}
|
16
|
-
fk = ForeignKey.new(from_table, from_column, nil, options)
|
17
|
-
execute(fk.drop_sql)
|
18
|
-
end
|
19
|
-
|
20
|
-
def remove_column_with_fk(table, column)
|
21
|
-
drop_foreign_key(table, column)
|
22
|
-
remove_column(table, column)
|
23
|
-
end
|
24
|
-
|
25
|
-
class ForeignKey
|
26
|
-
attr_reader :from_table, :from_column, :to_table, :to_column, :name, :adapter
|
27
|
-
|
28
|
-
def initialize(from_table, from_column, to_table, options=nil)
|
29
|
-
options ||= {}
|
30
|
-
@from_table = from_table.to_s
|
31
|
-
@from_column = from_column.to_s
|
32
|
-
@to_table = to_table.to_s
|
33
|
-
@to_column = (options[:to_column] || 'id').to_s
|
34
|
-
@name = (options[:name] || "fk_#{@from_table}_#{@from_column}").to_s
|
35
|
-
@adapter = Ardb::Adapter.new(Ardb.config)
|
36
|
-
end
|
37
|
-
|
38
|
-
def add_sql
|
39
|
-
apply_data(@adapter.foreign_key_add_sql)
|
40
|
-
end
|
41
|
-
|
42
|
-
def drop_sql
|
43
|
-
apply_data(@adapter.foreign_key_drop_sql)
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def apply_data(template_sql)
|
49
|
-
template_sql.
|
50
|
-
gsub(':from_table', @from_table).
|
51
|
-
gsub(':from_column', @from_column).
|
52
|
-
gsub(':to_table', @to_table).
|
53
|
-
gsub(':to_column', @to_column).
|
54
|
-
gsub(':name', @name)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# This file will setup the AR migration command recorder for being able to
|
59
|
-
# change our stuff, require it in an initializer
|
60
|
-
|
61
|
-
module RecorderMixin
|
62
|
-
|
63
|
-
def foreign_key(*args)
|
64
|
-
record(:foreign_key, args)
|
65
|
-
end
|
66
|
-
|
67
|
-
protected
|
68
|
-
|
69
|
-
def invert_foreign_key(args)
|
70
|
-
[ :drop_foreign_key, args ]
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
data/lib/ardb/pg_json.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_support'
|
3
|
-
|
4
|
-
# Allow ActiveRecord to work with PostgreSQL json/jsonb fields, which aren't
|
5
|
-
# supported with ActiveRecord 3.2
|
6
|
-
# https://github.com/romanbsd/activerecord-postgres-json/blob/master/lib/activerecord-postgres-json/activerecord.rb
|
7
|
-
require 'active_record/connection_adapters/postgresql_adapter'
|
8
|
-
|
9
|
-
module ActiveRecord
|
10
|
-
module ConnectionAdapters
|
11
|
-
PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:json] = { :name => 'json' }
|
12
|
-
PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:jsonb] = { :name => 'jsonb' }
|
13
|
-
|
14
|
-
class PostgreSQLColumn < Column
|
15
|
-
# Adds the json type for the column.
|
16
|
-
def simplified_type_with_json(field_type)
|
17
|
-
case field_type
|
18
|
-
when 'json'
|
19
|
-
:json
|
20
|
-
when 'jsonb'
|
21
|
-
:jsonb
|
22
|
-
else
|
23
|
-
simplified_type_without_json(field_type)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
alias_method_chain :simplified_type, :json
|
28
|
-
|
29
|
-
class << self
|
30
|
-
def extract_value_from_default_with_json(default)
|
31
|
-
case default
|
32
|
-
when "'{}'::json", "'{}'::jsonb"
|
33
|
-
'{}'
|
34
|
-
when "'[]'::json", "'[]'::jsonb"
|
35
|
-
'[]'
|
36
|
-
else
|
37
|
-
extract_value_from_default_without_json(default)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
alias_method_chain :extract_value_from_default, :json
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
class TableDefinition
|
47
|
-
# Adds json type for migrations. So you can add columns to a table like:
|
48
|
-
# create_table :people do |t|
|
49
|
-
# ...
|
50
|
-
# t.json :info
|
51
|
-
# ...
|
52
|
-
# end
|
53
|
-
def json(*args)
|
54
|
-
options = args.extract_options!
|
55
|
-
column_names = args
|
56
|
-
column_names.each { |name| column(name, 'json', options) }
|
57
|
-
end
|
58
|
-
|
59
|
-
def jsonb(*args)
|
60
|
-
options = args.extract_options!
|
61
|
-
column_names = args
|
62
|
-
column_names.each { |name| column(name, 'jsonb', options) }
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
class Table
|
68
|
-
# Adds json type for migrations. So you can add columns to a table like:
|
69
|
-
# change_table :people do |t|
|
70
|
-
# ...
|
71
|
-
# t.json :info
|
72
|
-
# ...
|
73
|
-
# end
|
74
|
-
def json(*args)
|
75
|
-
options = args.extract_options!
|
76
|
-
column_names = args
|
77
|
-
column_names.each { |name| column(name, 'json', options) }
|
78
|
-
end
|
79
|
-
|
80
|
-
def jsonb(*args)
|
81
|
-
options = args.extract_options!
|
82
|
-
column_names = args
|
83
|
-
column_names.each { |name| column(name, 'jsonb', options) }
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'ardb/migration_helpers'
|
2
|
-
|
3
|
-
class CreatePgJsonMigrateTest < ActiveRecord::Migration
|
4
|
-
include Ardb::MigrationHelpers
|
5
|
-
|
6
|
-
def change
|
7
|
-
create_table :pg_json_test_records do |t|
|
8
|
-
t.json :json_attribute
|
9
|
-
end
|
10
|
-
add_column :pg_json_test_records, :jsonb_attribute, :jsonb
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'ardb/pg_json'
|
3
|
-
|
4
|
-
require 'json'
|
5
|
-
require 'test/support/postgresql/setup_test_db'
|
6
|
-
|
7
|
-
module Ardb; end
|
8
|
-
module Ardb::PgJson
|
9
|
-
|
10
|
-
class SystemTests < PostgresqlDbTests
|
11
|
-
desc "Ardb postgresql json shim"
|
12
|
-
setup do
|
13
|
-
@ardb_config.migrations_path = 'pg_json_migrations'
|
14
|
-
end
|
15
|
-
|
16
|
-
should "add support for postgresql json columns to migrations" do
|
17
|
-
# this should migrate the db, adding a record that has json/jsonb columns
|
18
|
-
assert_nothing_raised do
|
19
|
-
silence_stdout{ Ardb.adapter.migrate_db }
|
20
|
-
end
|
21
|
-
|
22
|
-
results = ActiveRecord::Base.connection.execute(
|
23
|
-
"SELECT column_name, data_type " \
|
24
|
-
"FROM INFORMATION_SCHEMA.COLUMNS " \
|
25
|
-
"WHERE table_name = 'pg_json_test_records'"
|
26
|
-
).to_a
|
27
|
-
exp = {
|
28
|
-
'column_name' => 'json_attribute',
|
29
|
-
'data_type' => 'json',
|
30
|
-
}
|
31
|
-
assert_includes exp, results
|
32
|
-
exp = {
|
33
|
-
'column_name' => 'jsonb_attribute',
|
34
|
-
'data_type' => 'jsonb'
|
35
|
-
}
|
36
|
-
assert_includes exp, results
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
class WithMigratedTableTests < SystemTests
|
42
|
-
setup do
|
43
|
-
silence_stdout{ Ardb.adapter.migrate_db }
|
44
|
-
@record_class = Class.new(ActiveRecord::Base) do
|
45
|
-
self.table_name = 'pg_json_test_records'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
should "add support for postgresql 'json' attributes on records" do
|
50
|
-
values = [Factory.string, Factory.integer, nil]
|
51
|
-
|
52
|
-
record = @record_class.new
|
53
|
-
assert_nil record.json_attribute
|
54
|
-
assert_nil record.jsonb_attribute
|
55
|
-
|
56
|
-
hash = Factory.integer(3).times.inject({}) do |h, n|
|
57
|
-
h.merge!(Factory.string => values.sample)
|
58
|
-
end
|
59
|
-
record.json_attribute = JSON.dump(hash)
|
60
|
-
record.jsonb_attribute = JSON.dump(hash)
|
61
|
-
assert_nothing_raised{ record.save! }
|
62
|
-
record.reload
|
63
|
-
assert_equal hash, JSON.load(record.json_attribute)
|
64
|
-
assert_equal hash, JSON.load(record.jsonb_attribute)
|
65
|
-
|
66
|
-
array = Factory.integer(3).times.map{ values.sample }
|
67
|
-
record.json_attribute = JSON.dump(array)
|
68
|
-
record.jsonb_attribute = JSON.dump(array)
|
69
|
-
assert_nothing_raised{ record.save! }
|
70
|
-
record.reload
|
71
|
-
assert_equal array, JSON.load(record.json_attribute)
|
72
|
-
assert_equal array, JSON.load(record.jsonb_attribute)
|
73
|
-
|
74
|
-
value = values.sample
|
75
|
-
record.json_attribute = JSON.dump(value)
|
76
|
-
record.jsonb_attribute = JSON.dump(value)
|
77
|
-
assert_nothing_raised{ record.save! }
|
78
|
-
record.reload
|
79
|
-
assert_equal value, JSON.load(record.json_attribute)
|
80
|
-
assert_equal value, JSON.load(record.jsonb_attribute)
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|