pg_meta 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58339598c12ed8e286ecdd941ab1497947e5de3001c1dd1434888c05056551ba
4
- data.tar.gz: e4b3bcf44fe2286760fd83841f8e42d5ca98a09cf08a8f3406f78a1d76c25671
3
+ metadata.gz: 11de6eb1003d273e1ab21fa858a61db0a92c38d033b9e21b731d6e58e76b32dc
4
+ data.tar.gz: d5104b86eb79c567d20d2d352d3c8a05c9abe6605205c91c91aca5326ee2e541
5
5
  SHA512:
6
- metadata.gz: a6918852294bd4207e93dc1730a64e6b4036db0807c2158de808da264de7200a34e08c8f40b7b59a07fbc454e098fdd9a9fbc436a2dd8bf5972e51a8aa7d8beb
7
- data.tar.gz: 7c7c1166566190064ef0f45c24c2eee1b5a23fb89272963951203e915fd0a23cc8be5fc37c7499920684d754e0ee543e897bd20961667b31ab0536c9b651ce3a
6
+ metadata.gz: e3d7d7e1651f022d7965c193eb8ce91ea1ab529708299aeb5994ab6ede3119e8fd2f2bbac1b251b8c8a6a71ca44899023faa505470c29f94b64c1cb50b23bb5d
7
+ data.tar.gz: 8c6df9dc133a01171cf7e53954f63f948a0bb4a1b9d1e0c3e21f2195241932b4e734a0a0431315bf7ebaf4ec8085484e59533acde1a94be490576cd67882c700
@@ -6,9 +6,9 @@ module PgMeta
6
6
  # Load the database from the given PgConn::Connection object. This method is a
7
7
  # transaction wrapper around #do_load_conn
8
8
  def self.load_conn(pg_conn, exclude_schemas: [])
9
- pg_conn.pg_connection.transaction { |conn|
9
+ pg_conn.pg_connection.transaction { |conn|
10
10
  conn.exec "set transaction isolation level serializable read only deferrable"
11
- do_load_from_connection(conn, exclude_schemas: exclude_schemas)
11
+ do_load_from_connection(conn, exclude_schemas: exclude_schemas)
12
12
  }
13
13
  end
14
14
 
@@ -37,7 +37,7 @@ module PgMeta
37
37
  db = Database.new(conn.db, owner)
38
38
 
39
39
  # Schema excludes
40
- exclude_sql =
40
+ exclude_sql =
41
41
 
42
42
  # Build schemas
43
43
  conn.exec(%(
@@ -65,7 +65,7 @@ module PgMeta
65
65
  and c.relkind in ('r', 'v', 'm')
66
66
  )).each { |row|
67
67
  schema = db.schemas[row[:schema]]
68
- klass =
68
+ klass =
69
69
  case row[:relkind]
70
70
  when 'r'; Table
71
71
  when 'v'; View
@@ -95,9 +95,9 @@ module PgMeta
95
95
  join pg_constraint c on c.oid = d.objid
96
96
  join chain ch on ch.that = c.conrelid
97
97
  )
98
- select n1.nspname as this_schema,
99
- t1.relname as this_table,
100
- n2.nspname as that_schema,
98
+ select n1.nspname as this_schema,
99
+ t1.relname as this_table,
100
+ n2.nspname as that_schema,
101
101
  t2.relname as that_table
102
102
  from chain c
103
103
  join pg_class t1 on t1.oid = c.this
@@ -152,12 +152,12 @@ module PgMeta
152
152
  coalesce(i.is_generated, 'NO') = 'ALWAYS' as "generated?",
153
153
  coalesce(i.is_nullable, 'NO') = 'YES' as "nullable?",
154
154
  coalesce(i.is_updatable, 'NO') = 'YES' as "updatable?"
155
- from pg_namespace n
155
+ from pg_namespace n
156
156
  join pg_class c on c.relnamespace = n.oid
157
157
  join pg_attribute a on a.attrelid = c.oid
158
158
  join pg_type t on t.oid = a.atttypid
159
159
  left join pg_type t2 on t2.oid = t.typelem
160
- left join information_schema.columns i on
160
+ left join information_schema.columns i on
161
161
  i.table_schema = n.nspname
162
162
  and i.table_name = c.relname
163
163
  and i.column_name = a.attname
@@ -232,7 +232,7 @@ module PgMeta
232
232
  "table",
233
233
  "index",
234
234
  "unique"
235
- order by
235
+ order by
236
236
  "schema",
237
237
  "table",
238
238
  "index"
@@ -252,42 +252,111 @@ module PgMeta
252
252
  # the referenced table and that yields a row for each column in the unique
253
253
  # key (TODO: Can this be omitted?)
254
254
  #
255
+ # conn.exec(%(
256
+ # select rc.constraint_schema::text as schema,
257
+ # rc.constraint_name::text as name,
258
+ # cu_refing.table_schema::text as "referencing_schema",
259
+ # cu_refing.table_name::text as "referencing_table",
260
+ # (
261
+ # select array_agg(column_name::text order by ordinal_position)
262
+ # from information_schema.key_column_usage kcu
263
+ # where kcu.constraint_schema = rc.constraint_schema
264
+ # and kcu.constraint_name = rc.constraint_name
265
+ # ) as "referencing_columns",
266
+ # cu_refed.table_schema::text as referenced_schema,
267
+ # cu_refed.table_name::text as referenced_table,
268
+ # cu_refed.constraint_name::text as referenced_constraint
269
+ # from information_schema.referential_constraints rc
270
+ # join information_schema.key_column_usage cu_refing
271
+ # on cu_refing.constraint_schema = rc.constraint_schema
272
+ # and cu_refing.constraint_name = rc.constraint_name
273
+ # join information_schema.key_column_usage cu_refed
274
+ # on cu_refed.constraint_schema = rc.unique_constraint_schema
275
+ # and cu_refed.constraint_name = rc.unique_constraint_name
276
+ # where #{exclude_sql("cu_refing.table_schema", exclude_schemas)}
277
+ # group by
278
+ # rc.constraint_schema,
279
+ # rc.constraint_name,
280
+ # cu_refing.table_schema,
281
+ # cu_refing.table_name,
282
+ # cu_refed.table_schema,
283
+ # cu_refed.table_name,
284
+ # cu_refed.constraint_name
285
+ # )).each { |row|
286
+ # schema = db.schemas[row[:schema]]
287
+ # name = row[:name]
288
+ # referencing_table = schema.tables[row[:referencing_table]]
289
+ # referencing_columns = lookup_columns(referencing_table, row[:referencing_columns])
290
+ # referenced_constraint =
291
+ # db.schemas[row[:referenced_schema]] \
292
+ # .tables[row[:referenced_table]] \
293
+ # .constraints[row[:referenced_constraint]]
294
+ # ref = ReferentialConstraint.new(referencing_table, name, referencing_columns, referenced_constraint)
295
+ # ref.referenced_table.send(:add_depending_table, referencing_table)
296
+ # }
297
+ # p conn.duration
255
298
  conn.exec(%(
256
- select rc.constraint_schema::text as schema,
257
- rc.constraint_name::text as name,
258
- cu_refing.table_schema::text as "referencing_schema",
259
- cu_refing.table_name::text as "referencing_table",
260
- (
261
- select array_agg(column_name::text order by ordinal_position)
262
- from information_schema.key_column_usage kcu
263
- where kcu.constraint_schema = rc.constraint_schema
264
- and kcu.constraint_name = rc.constraint_name
265
- ) as "referencing_columns",
266
- cu_refed.table_schema::text as referenced_schema,
267
- cu_refed.table_name::text as referenced_table,
268
- cu_refed.constraint_name::text as referenced_constraint
269
- from information_schema.referential_constraints rc
270
- join information_schema.key_column_usage cu_refing
271
- on cu_refing.constraint_schema = rc.constraint_schema
272
- and cu_refing.constraint_name = rc.constraint_name
273
- join information_schema.key_column_usage cu_refed
274
- on cu_refed.constraint_schema = rc.unique_constraint_schema
275
- and cu_refed.constraint_name = rc.unique_constraint_name
276
- where #{exclude_sql("cu_refing.table_schema", exclude_schemas)}
299
+ with
300
+ constraints as (
301
+ select
302
+ c.*,
303
+ num,
304
+ fnum
305
+ from
306
+ pg_constraint as c,
307
+ unnest(c.conkey) with ordinality as ff(num, index), -- from field
308
+ unnest(c.confkey) with ordinality as tf(fnum, index) -- to field
309
+ where
310
+ c.contype = 'f'
311
+ and ff.index = tf.index
312
+ ),
313
+ name_constraints as (
314
+ select
315
+ c.connamespace::regnamespace::text as "schema",
316
+ c.conname::text as "name",
317
+ cf.relnamespace::regnamespace::text as "referencing_schema",
318
+ cf.relname::text as "referencing_table",
319
+ af.attname::text as "referencing_column",
320
+ ct.relnamespace::regnamespace::text as "referenced_schema",
321
+ ct.relname::text as "referenced_table",
322
+ at.attname::text as "referenced_column",
323
+ idx.relname::text as "referenced_constraint"
324
+ from
325
+ constraints c
326
+ join pg_attribute as af on af.attnum = c.num and af.attrelid = c.conrelid -- attribute-from
327
+ join pg_attribute as at on at.attnum = c.fnum and at.attrelid = c.confrelid
328
+ join pg_class as cf on cf.oid = c.conrelid
329
+ join pg_class as ct on ct.oid = c.confrelid
330
+ join pg_class as idx on idx.oid = c.conindid
331
+ where
332
+ #{exclude_sql("cf.relnamespace::regnamespace::text", exclude_schemas)}
333
+ )
334
+ select
335
+ "schema",
336
+ name,
337
+ referencing_schema,
338
+ referencing_table,
339
+ array_agg(referencing_column) as "referencing_columns",
340
+ referenced_schema,
341
+ referenced_table,
342
+ -- array_agg(referenced_columnn) as "referenced_columns",
343
+ referenced_constraint
344
+ from
345
+ name_constraints
277
346
  group by
278
- rc.constraint_schema,
279
- rc.constraint_name,
280
- cu_refing.table_schema,
281
- cu_refing.table_name,
282
- cu_refed.table_schema,
283
- cu_refed.table_name,
284
- cu_refed.constraint_name
347
+ "schema",
348
+ name,
349
+ referencing_schema,
350
+ referencing_table,
351
+ referenced_schema,
352
+ referenced_table,
353
+ referenced_constraint
285
354
  )).each { |row|
286
355
  schema = db.schemas[row[:schema]]
287
356
  name = row[:name]
288
357
  referencing_table = schema.tables[row[:referencing_table]]
289
358
  referencing_columns = lookup_columns(referencing_table, row[:referencing_columns])
290
- referenced_constraint =
359
+ referenced_constraint =
291
360
  db.schemas[row[:referenced_schema]] \
292
361
  .tables[row[:referenced_table]] \
293
362
  .constraints[row[:referenced_constraint]]
@@ -299,16 +368,16 @@ module PgMeta
299
368
  conn.exec(%(
300
369
  select s.nspname::text as "schema",
301
370
  pg_get_userbyid(p.proowner)::text as "owner",
302
- format('%I(%s)', p.proname, oidvectortypes(p.proargtypes))
303
- || ': '
371
+ format('%I(%s)', p.proname, oidvectortypes(p.proargtypes))
372
+ || ': '
304
373
  || format_type(p.prorettype, null) as "name",
305
- case format_type(p.prorettype, null)
306
- when 'void' then 'procedure'
307
- else 'function'
374
+ case format_type(p.prorettype, null)
375
+ when 'void' then 'procedure'
376
+ else 'function'
308
377
  end as "kind",
309
- case
310
- when prosecdef then 'definer'
311
- else 'invoker'
378
+ case
379
+ when prosecdef then 'definer'
380
+ else 'invoker'
312
381
  end as "security"
313
382
  from pg_proc p
314
383
  join pg_namespace s on (p.pronamespace = s.oid)
@@ -321,12 +390,12 @@ module PgMeta
321
390
 
322
391
  # Build user-defined triggers
323
392
  conn.exec(%(
324
- select n.nspname::text as "schema",
325
- c.relname::text as "table",
393
+ select n.nspname::text as "schema",
394
+ c.relname::text as "table",
326
395
  t.tgname::text as "name",
327
396
  fn.nspname::text as "function_schema",
328
- format('%I(%s)', p.proname::text, oidvectortypes(p.proargtypes))
329
- || ': '
397
+ format('%I(%s)', p.proname::text, oidvectortypes(p.proargtypes))
398
+ || ': '
330
399
  || format_type(p.prorettype, null) as "function_name",
331
400
  case when (tgtype::int::bit(7) & b'0000001')::int = 0 then 'stmt' else 'row' end as "level",
332
401
  coalesce(
@@ -334,7 +403,7 @@ module PgMeta
334
403
  case when (tgtype::int::bit(7) & b'0000010')::int = 0 then 'after' else null end,
335
404
  case when (tgtype::int::bit(7) & b'1000000')::int = 0 then null else 'instead' end,
336
405
  ''
337
- )::text as "timing",
406
+ )::text as "timing",
338
407
  (case when (tgtype::int::bit(7) & b'0000100')::int = 0 then '' else ' insert' end) ||
339
408
  (case when (tgtype::int::bit(7) & b'0001000')::int = 0 then '' else ' delete' end) ||
340
409
  (case when (tgtype::int::bit(7) & b'0010000')::int = 0 then '' else ' update' end) ||
data/lib/pg_meta/meta.rb CHANGED
@@ -46,7 +46,7 @@ module PgMeta
46
46
  h = {}
47
47
  Array(attrs).flatten.each { |attr|
48
48
  value = self.send(attr)
49
- h[attr] =
49
+ h[attr] =
50
50
  case value
51
51
  when Array
52
52
  if value.first.is_a?(Symbol)
@@ -281,7 +281,7 @@ module PgMeta
281
281
  class View < Table
282
282
  # List of views and tables used directly in the definition of this view
283
283
  attr_reader :defining_relations
284
-
284
+
285
285
  # List of tables used directly or indirectly in the definition of this view
286
286
  attr_reader :defining_tables
287
287
 
@@ -293,9 +293,9 @@ module PgMeta
293
293
  @defining_tables = []
294
294
  end
295
295
 
296
- def to_h()
296
+ def to_h()
297
297
  h = super
298
- h[:defining_relations] = defining_relations.map(&:uid)
298
+ h[:defining_relations] = defining_relations.map(&:uid)
299
299
  h[:defining_tables] = defining_tables.map(&:uid)
300
300
  h
301
301
  end
@@ -374,9 +374,9 @@ module PgMeta
374
374
 
375
375
  # True if column is a single-column reference to another record and is of
376
376
  # type varchar or text
377
- def kind?()
378
- @kind ||=
379
- references.size == 1 &&
377
+ def kind?()
378
+ @kind ||=
379
+ references.size == 1 &&
380
380
  references.first.referencing_columns.size == 1 &&
381
381
  !references.first.referenced_columns.first.primary_key?
382
382
  end
@@ -391,12 +391,12 @@ module PgMeta
391
391
  end
392
392
 
393
393
  def initialize(
394
- table, name, ordinal, type, element_type, dimensions, default,
394
+ table, name, ordinal, type, element_type, dimensions, default,
395
395
  is_identity, is_generated, is_nullable, is_updatable)
396
396
  super(table, name)
397
- @type, @element_type, @dimensions, @ordinal, @default, @is_identity,
397
+ @type, @element_type, @dimensions, @ordinal, @default, @is_identity,
398
398
  @is_generated, @is_nullable, @is_updatable =
399
- type, element_type, dimensions, ordinal, default, is_identity,
399
+ type, element_type, dimensions, ordinal, default, is_identity,
400
400
  is_generated, is_nullable, is_updatable
401
401
  table.columns[name] = self
402
402
  end
@@ -541,7 +541,7 @@ module PgMeta
541
541
  def initialize(schema, name, owner, security)
542
542
  super(schema, name)
543
543
  @owner = owner
544
- @security = security.to_sym
544
+ @security = security.to_sym
545
545
  if function?
546
546
  schema.functions[name] = self
547
547
  else
@@ -569,7 +569,7 @@ module PgMeta
569
569
  # When trigger is fired (:before, :after, or :instead)
570
570
  attr_reader :timing
571
571
 
572
- # Array of events (:insert, :update, :delete, or :truncate) causing the trigger to fire
572
+ # Array of events (:insert, :update, :delete, or :truncate) causing the trigger to fire
573
573
  attr_reader :events
574
574
 
575
575
  # Note that trigger names have a '()' suffixed. This avoid namespace
@@ -1,3 +1,3 @@
1
1
  module PgMeta
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_meta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-10 00:00:00.000000000 Z
11
+ date: 2024-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: indented_io