torque-postgresql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +31 -0
  4. data/lib/torque/postgresql/adapter/database_statements.rb +103 -0
  5. data/lib/torque/postgresql/adapter/oid/array.rb +19 -0
  6. data/lib/torque/postgresql/adapter/oid/enum.rb +46 -0
  7. data/lib/torque/postgresql/adapter/oid/interval.rb +94 -0
  8. data/lib/torque/postgresql/adapter/oid.rb +15 -0
  9. data/lib/torque/postgresql/adapter/quoting.rb +23 -0
  10. data/lib/torque/postgresql/adapter/schema_definitions.rb +28 -0
  11. data/lib/torque/postgresql/adapter/schema_dumper.rb +31 -0
  12. data/lib/torque/postgresql/adapter/schema_statements.rb +89 -0
  13. data/lib/torque/postgresql/adapter.rb +29 -0
  14. data/lib/torque/postgresql/attributes/builder/enum.rb +151 -0
  15. data/lib/torque/postgresql/attributes/builder.rb +1 -0
  16. data/lib/torque/postgresql/attributes/enum.rb +231 -0
  17. data/lib/torque/postgresql/attributes/lazy.rb +33 -0
  18. data/lib/torque/postgresql/attributes/type_map.rb +46 -0
  19. data/lib/torque/postgresql/attributes.rb +32 -0
  20. data/lib/torque/postgresql/auxiliary_statement.rb +192 -0
  21. data/lib/torque/postgresql/base.rb +28 -0
  22. data/lib/torque/postgresql/collector.rb +31 -0
  23. data/lib/torque/postgresql/config.rb +50 -0
  24. data/lib/torque/postgresql/migration/command_recorder.rb +31 -0
  25. data/lib/torque/postgresql/migration.rb +1 -0
  26. data/lib/torque/postgresql/relation/auxiliary_statement.rb +65 -0
  27. data/lib/torque/postgresql/relation/distinct_on.rb +43 -0
  28. data/lib/torque/postgresql/relation.rb +62 -0
  29. data/lib/torque/postgresql/schema_dumper.rb +37 -0
  30. data/lib/torque/postgresql/version.rb +5 -0
  31. data/lib/torque/postgresql.rb +18 -0
  32. data/lib/torque-postgresql.rb +1 -0
  33. metadata +236 -0
@@ -0,0 +1,43 @@
1
+ module Torque
2
+ module PostgreSQL
3
+ module Relation
4
+ module DistinctOn
5
+
6
+ attr_accessor :distinct_on_value
7
+
8
+ # Specifies whether the records should be unique or not by a given set of fields.
9
+ # For example:
10
+ #
11
+ # User.distinct_on(:name)
12
+ # # Returns 1 record per distinct name
13
+ #
14
+ # User.distinct_on(:name, :email)
15
+ # # Returns 1 record per distinct name and email
16
+ #
17
+ # User.distinct_on(false)
18
+ # # You can also remove the uniqueness
19
+ def distinct_on(*value)
20
+ spawn.distinct_on!(*value)
21
+ end
22
+
23
+ # Like #distinct_on, but modifies relation in place.
24
+ def distinct_on!(*value)
25
+ self.distinct_on_value = value
26
+ self
27
+ end
28
+
29
+ private
30
+
31
+ # Hook arel build to add the distinct on clause
32
+ def build_arel
33
+ arel = super
34
+
35
+ value = self.distinct_on_value
36
+ arel.distinct_on(resolve_column(value)) unless value.nil?
37
+ arel
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,62 @@
1
+
2
+ require_relative 'relation/distinct_on'
3
+ require_relative 'relation/auxiliary_statement'
4
+
5
+ module Torque
6
+ module PostgreSQL
7
+ module Relation
8
+
9
+ include DistinctOn
10
+ include AuxiliaryStatement
11
+
12
+ # Resolve column definition up to second value.
13
+ # For example, based on Post model:
14
+ #
15
+ # resolve_column(['name', :title])
16
+ # # Returns ['name', '"posts"."title"']
17
+ #
18
+ # resolve_column([:title, {authors: :name}])
19
+ # # Returns ['"posts"."title"', '"authors"."name"']
20
+ #
21
+ # resolve_column([{authors: [:name, :age]}])
22
+ # # Returns ['"authors"."name"', '"authors"."age"']
23
+ def resolve_column(list, base = false)
24
+ base = resolve_base_table(base)
25
+
26
+ list.map do |item|
27
+ case item
28
+ when String
29
+ Arel::Nodes::SqlLiteral.new(klass.send(:sanitize_sql, item.to_s))
30
+ when Symbol
31
+ base ? base.arel_attribute(item) : klass.arel_attribute(item)
32
+ when Array
33
+ resolve_column(item, base)
34
+ when Hash
35
+ raise ArgumentError, "Unsupported Hash for attributes on third level" if base
36
+ item.map do |key, other_list|
37
+ other_list = [other_list] unless other_list.kind_of? Enumerable
38
+ resolve_column(other_list, key)
39
+ end
40
+ else
41
+ raise ArgumentError, "Unsupported argument type: #{value} (#{value.class})"
42
+ end
43
+ end.flatten
44
+ end
45
+
46
+ # Get the TableMetadata from a relation
47
+ def resolve_base_table(relation)
48
+ return unless relation
49
+
50
+ table = predicate_builder.send(:table)
51
+ if table.associated_with?(relation)
52
+ table.associated_table(relation).send(:klass)
53
+ else
54
+ raise ArgumentError, "Relation for #{relation} not found on #{klass}"
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ ActiveRecord::Relation.include Relation
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ module Torque
2
+ module PostgreSQL
3
+ module SchemaDumper
4
+
5
+ def extensions(stream)
6
+ super
7
+ user_defined_types(stream)
8
+ end
9
+
10
+ private
11
+
12
+ def user_defined_types(stream)
13
+ types = @connection.user_defined_types
14
+ return unless types.any?
15
+
16
+ stream.puts " # These are user-defined types used on this database"
17
+ types.each do |name, type|
18
+ raise StandardError, "User-defined type '#{name}' cannot be dumped!" if type.blank?
19
+ send(type.to_sym, name, stream)
20
+ end
21
+ stream.puts
22
+ rescue => e
23
+ stream.puts "# Could not dump user-defined types because of following #{e.class}"
24
+ stream.puts "# #{e.message}"
25
+ stream.puts
26
+ end
27
+
28
+ def enum(name, stream)
29
+ values = @connection.enum_values(name).map { |v| "\"#{v}\"" }
30
+ stream.puts " create_enum \"#{name}\", [#{values.join(', ')}], force: :cascade"
31
+ end
32
+
33
+ end
34
+
35
+ ActiveRecord::SchemaDumper.prepend SchemaDumper
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Torque
2
+ module PostgreSQL
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_model'
2
+ require 'active_record'
3
+ require 'active_support'
4
+
5
+ require 'active_support/core_ext/hash/compact'
6
+ require 'active_record/connection_adapters/postgresql_adapter'
7
+
8
+ require 'torque/postgresql/config'
9
+ require 'torque/postgresql/version'
10
+ require 'torque/postgresql/collector'
11
+
12
+ require 'torque/postgresql/adapter'
13
+ require 'torque/postgresql/attributes'
14
+ require 'torque/postgresql/auxiliary_statement'
15
+ require 'torque/postgresql/base'
16
+ require 'torque/postgresql/migration'
17
+ require 'torque/postgresql/relation'
18
+ require 'torque/postgresql/schema_dumper'
@@ -0,0 +1 @@
1
+ require 'torque/postgresql'
metadata ADDED
@@ -0,0 +1,236 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: torque-postgresql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: pg
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.19'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.19.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.19'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.19.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '10.1'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 10.1.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.1'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 10.1.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: database_cleaner
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.5'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.5.3
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.5'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.5.3
93
+ - !ruby/object:Gem::Dependency
94
+ name: dotenv
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '2.1'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.1.1
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.1'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 2.1.1
113
+ - !ruby/object:Gem::Dependency
114
+ name: rspec
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '3.5'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 3.5.0
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '3.5'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 3.5.0
133
+ - !ruby/object:Gem::Dependency
134
+ name: factory_girl
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '4.5'
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 4.5.0
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '4.5'
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 4.5.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: faker
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.5'
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 1.5.0
163
+ type: :development
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '1.5'
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 1.5.0
173
+ description: Add support to complex resources of PostgreSQL, like data types, user-defined
174
+ types and CTE with extensions for Arel and ActiveRecord
175
+ email:
176
+ - carlinhus.fsilva@gmail.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - MIT-LICENSE
182
+ - Rakefile
183
+ - lib/torque-postgresql.rb
184
+ - lib/torque/postgresql.rb
185
+ - lib/torque/postgresql/adapter.rb
186
+ - lib/torque/postgresql/adapter/database_statements.rb
187
+ - lib/torque/postgresql/adapter/oid.rb
188
+ - lib/torque/postgresql/adapter/oid/array.rb
189
+ - lib/torque/postgresql/adapter/oid/enum.rb
190
+ - lib/torque/postgresql/adapter/oid/interval.rb
191
+ - lib/torque/postgresql/adapter/quoting.rb
192
+ - lib/torque/postgresql/adapter/schema_definitions.rb
193
+ - lib/torque/postgresql/adapter/schema_dumper.rb
194
+ - lib/torque/postgresql/adapter/schema_statements.rb
195
+ - lib/torque/postgresql/attributes.rb
196
+ - lib/torque/postgresql/attributes/builder.rb
197
+ - lib/torque/postgresql/attributes/builder/enum.rb
198
+ - lib/torque/postgresql/attributes/enum.rb
199
+ - lib/torque/postgresql/attributes/lazy.rb
200
+ - lib/torque/postgresql/attributes/type_map.rb
201
+ - lib/torque/postgresql/auxiliary_statement.rb
202
+ - lib/torque/postgresql/base.rb
203
+ - lib/torque/postgresql/collector.rb
204
+ - lib/torque/postgresql/config.rb
205
+ - lib/torque/postgresql/migration.rb
206
+ - lib/torque/postgresql/migration/command_recorder.rb
207
+ - lib/torque/postgresql/relation.rb
208
+ - lib/torque/postgresql/relation/auxiliary_statement.rb
209
+ - lib/torque/postgresql/relation/distinct_on.rb
210
+ - lib/torque/postgresql/schema_dumper.rb
211
+ - lib/torque/postgresql/version.rb
212
+ homepage: https://github.com/crashtech/torque-postgresql
213
+ licenses:
214
+ - MIT
215
+ metadata: {}
216
+ post_install_message:
217
+ rdoc_options: []
218
+ require_paths:
219
+ - lib
220
+ required_ruby_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ required_rubygems_version: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ requirements: []
231
+ rubyforge_project:
232
+ rubygems_version: 2.5.1
233
+ signing_key:
234
+ specification_version: 4
235
+ summary: ActiveRecord extension to reach PostgreSQL native resources
236
+ test_files: []