activerecord-pg-format-db-structure 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 0f52ec941c9e147d32ce581a4e5384263455ca48295539f0d3a2b518cc7e4e51
4
- data.tar.gz: 1ab211061fe2fca72d364dc8b92636e01c294ad4efddbd684a1a2b1b072d02a8
3
+ metadata.gz: 1b2d206493754a88a3913e3fb7e3532364f2adbd0dea689226d73d9544c73e98
4
+ data.tar.gz: df009fb052c07eb578b063785ee2186bfbda7ccd758643ae52285b99c784b49b
5
5
  SHA512:
6
- metadata.gz: 87322b447a211cce08c5beebf137ca7a5da4947f0a01f37192978ea6a873d6f990201450ae3764501cccaa04667089b595bce39829d544e417ee7a9426d8ca5d
7
- data.tar.gz: 27ebd693ed145add5112b35805c94e06b4f6f32e03419ffc92c62187116e3ec93d02694b0c67abf153e5a8582643989c2831e74b00f002b5758ba3fc7f6f7c5f
6
+ metadata.gz: 9e519793dec134c227413416d750ef8be1d3da7414c921369022fc663ae4ae75b789de632e18cd1f08cfa85e719cd87a0b2ed61336db7130e01094196fcb1823
7
+ data.tar.gz: 64558806595fdecbe27035f5c22b8f3c90b56909a5a2a19b20a1fe6cf2cdbd5dac153cf5d9d860e5ee372144cea5609751b5b1f52dd41072586830a1ae93109e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2025-01-30
4
+
5
+ - Get rid of `anbt-sql-formatter` dependency since it breaks queries with type casting
6
+
3
7
  ## [0.1.1] - 2025-01-29
4
8
 
5
9
  - Proper deparsing of all statements
data/README.md CHANGED
@@ -338,7 +338,7 @@ Should be run after other operations that inline alter statements.
338
338
 
339
339
  Returns an SQL string from raw PgQuery statements.
340
340
 
341
- Relying mostly on `PgQuery.deparse`, but applying some formatting using the [anbt-sql-formatter](https://github.com/sonota88/anbt-sql-formatter) gem on select & insert statements.
341
+ Relying mostly on `PgQuery.deparse`, and does a best effort to add some indentation where possible.
342
342
 
343
343
  ## Development
344
344
 
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "pg_query"
4
- require "anbt-sql-formatter/formatter"
5
4
 
6
5
  module ActiveRecordPgFormatDbStructure
7
6
  # Returns a list of SQL strings from a list of PgQuery::RawStmt.
@@ -23,7 +22,7 @@ module ActiveRecordPgFormatDbStructure
23
22
  in stmt: { alter_table_stmt: _ }
24
23
  deparse_alter_table_stmt(raw_statement.stmt.alter_table_stmt)
25
24
  in stmt: { select_stmt: _ }
26
- deparse_query_statement(raw_statement.stmt.select_stmt)
25
+ deparse_select_stmt(raw_statement.stmt.select_stmt)
27
26
  in stmt: { insert_stmt: _ }
28
27
  deparse_insert_statement(raw_statement.stmt.insert_stmt)
29
28
  in stmt: { create_table_as_stmt: _ }
@@ -41,9 +40,9 @@ module ActiveRecordPgFormatDbStructure
41
40
  "\n#{PgQuery.deparse_stmt(stmt)};"
42
41
  end
43
42
 
44
- def deparse_query_statement(stmt)
43
+ def deparse_select_stmt(select_stmt)
45
44
  generic_query_str = +"\n\n"
46
- generic_query_str << pretty_formt_sql_string(PgQuery.deparse_stmt(stmt))
45
+ generic_query_str << deparse_leaf_select_stmt(select_stmt)
47
46
  generic_query_str << ";"
48
47
  end
49
48
 
@@ -126,7 +125,7 @@ module ActiveRecordPgFormatDbStructure
126
125
  create_table_as_stmt_str << ";"
127
126
 
128
127
  query_str = +"(\n"
129
- query_str << pretty_formt_sql_string(PgQuery.deparse_stmt(stmt.query.inner)).gsub(/^/, PRETTY_INDENT_STRING)
128
+ query_str << deparse_leaf_select_stmt(stmt.query.select_stmt).gsub(/^/, PRETTY_INDENT_STRING)
130
129
  query_str << "\n)"
131
130
 
132
131
  create_table_as_stmt_str[placeholder_query_string] = query_str
@@ -144,7 +143,7 @@ module ActiveRecordPgFormatDbStructure
144
143
  view_stmt_str << ";"
145
144
 
146
145
  query_str = +"(\n"
147
- query_str << pretty_formt_sql_string(PgQuery.deparse_stmt(stmt.query.inner)).gsub(/^/, PRETTY_INDENT_STRING)
146
+ query_str << deparse_leaf_select_stmt(stmt.query.select_stmt).gsub(/^/, PRETTY_INDENT_STRING)
148
147
  query_str << "\n)"
149
148
 
150
149
  view_stmt_str[placeholder_query_string] = query_str
@@ -161,27 +160,86 @@ module ActiveRecordPgFormatDbStructure
161
160
  )
162
161
  insert_stmt_str << "\n;"
163
162
 
164
- query_str = pretty_formt_sql_string(PgQuery.deparse_stmt(insert_stmt.select_stmt.inner))
165
- query_str.gsub!(/\AVALUES /, "VALUES\n ")
163
+ query_str = if insert_stmt.select_stmt.inner.values_lists.any?
164
+ deparse_values_list_select_stmt(insert_stmt.select_stmt.inner)
165
+ else
166
+ deparse_leaf_select_stmt(insert_stmt.select_stmt.inner)
167
+ end
166
168
 
167
169
  insert_stmt_str[placeholder_query_string] = query_str
168
170
  insert_stmt_str
169
171
  end
170
172
 
171
- def pretty_formt_sql_string(sql)
172
- rule = AnbtSql::Rule.new
173
- rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
174
- rule.indent_string = PRETTY_INDENT_STRING
175
- formatter = AnbtSql::Formatter.new(rule)
176
- formatter.format(sql)
173
+ def deparse_values_list_select_stmt(select_stmt)
174
+ values_str = +"VALUES\n "
175
+ values_str << select_stmt.values_lists.map do |values_list|
176
+ PgQuery.deparse_stmt(PgQuery::SelectStmt.new(values_lists: [values_list])).gsub(/\AVALUES /, "")
177
+ end.join("\n,")
178
+ values_str
177
179
  end
178
180
 
179
- def placeholder_query_string
180
- @placeholder_query_string ||= PgQuery.deparse_stmt(placeholder_query_stmt)
181
+ def deparse_leaf_select_stmt(select_stmt) # rubocop:disable Metrics/PerceivedComplexity
182
+ target_list_placeholder = PgQuery::ResTarget.new(
183
+ val: { a_const: { sval: { sval: "target_list_placeholder" } } }
184
+ )
185
+
186
+ if select_stmt.with_clause
187
+ placeholder_with_clause = PgQuery::WithClause.new(
188
+ **select_stmt.with_clause.to_h,
189
+ ctes: select_stmt.with_clause.ctes.map do |cte|
190
+ PgQuery::Node.from(
191
+ PgQuery::CommonTableExpr.new(
192
+ **cte.inner.to_h,
193
+ ctequery: PgQuery::Node.from(placeholder_query_stmt("placeholder_for_#{cte.inner.ctename}_cte"))
194
+ )
195
+ )
196
+ end
197
+ )
198
+ end
199
+
200
+ select_stmt_str = PgQuery.deparse_stmt(
201
+ PgQuery::SelectStmt.new(
202
+ **select_stmt.to_h,
203
+ with_clause: placeholder_with_clause,
204
+ target_list: ([PgQuery::Node.from(target_list_placeholder)] if select_stmt.target_list.any?)
205
+ )
206
+ )
207
+
208
+ if select_stmt.target_list.any?
209
+ target_list_str = +"\n"
210
+ target_list_str << select_stmt.target_list.map do |target|
211
+ deparse_res_target(target.inner).gsub(/^/, PRETTY_INDENT_STRING)
212
+ end.join(",\n")
213
+ target_list_str << "\n"
214
+
215
+ select_stmt_str[deparse_res_target(target_list_placeholder)] = target_list_str
216
+ end
217
+
218
+ select_stmt.with_clause&.ctes&.each do |cte|
219
+ cte_str = +"\n"
220
+ cte_str << deparse_leaf_select_stmt(cte.inner.ctequery.inner).gsub(/^/, PRETTY_INDENT_STRING)
221
+ cte_str << "\n"
222
+
223
+ select_stmt_str["SELECT placeholder_for_#{cte.inner.ctename}_cte"] = cte_str
224
+ end
225
+
226
+ select_stmt_str.gsub!(/ +$/, "")
227
+
228
+ select_stmt_str
229
+ end
230
+
231
+ def deparse_res_target(res_target)
232
+ PgQuery.deparse_stmt(
233
+ PgQuery::SelectStmt.new(target_list: [PgQuery::Node.from(res_target)])
234
+ ).gsub(/\ASELECT /, "")
235
+ end
236
+
237
+ def placeholder_query_string(placeholder_name = "placeholder")
238
+ PgQuery.deparse_stmt(placeholder_query_stmt(placeholder_name))
181
239
  end
182
240
 
183
- def placeholder_query_stmt
184
- @placeholder_query_stmt ||= PgQuery.parse("SELECT placeholder").tree.stmts.first.stmt.select_stmt
241
+ def placeholder_query_stmt(placeholder_name = "placeholder")
242
+ PgQuery.parse("SELECT #{placeholder_name}").tree.stmts.first.stmt.select_stmt
185
243
  end
186
244
  end
187
245
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordPgFormatDbStructure
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,28 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-pg-format-db-structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jell
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-01-29 00:00:00.000000000 Z
10
+ date: 2025-01-30 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: anbt-sql-formatter
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '0.1'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '0.1'
26
12
  - !ruby/object:Gem::Dependency
27
13
  name: pg_query
28
14
  requirement: !ruby/object:Gem::Requirement