pg_meta 0.1.2 → 0.2.0

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: 8553533c4f97ee625c2796b354d0ab5124786853f3a64f1b1a6b0d126ba8ac35
4
- data.tar.gz: 5e80ab9a799e73c21061fc78275affe48706208246b749a198223f7b8f8a0dbf
3
+ metadata.gz: 06b01cd5716ff1dbc04209a9b7792f59808b8d23f633b1810528123d62b19540
4
+ data.tar.gz: 2b6faf8696583d6c46893c6454dd114d9b83aedb452fa636560da255598d3f24
5
5
  SHA512:
6
- metadata.gz: 80ad81f85a6acb805971f5522cd3189ba553c0f4ab0a6d09c9f152084ac099d3aff1399da10bc71222f18551e3ac2ed9b2b28ea7b9304a233c943cfbde9dac0e
7
- data.tar.gz: 548de1800985a70250558ce8611ca08f81c472caa5f0d9da5345ace172d5ef37103e1fa6475325b47eb6b9dc83719e9f5c65495610e15d88916717d3dfd37f46
6
+ metadata.gz: bc2a8a658757b86f89926bac1654c27a8ef00b4e3f7028b1c65726f684e8cbb58ed86c2b87ca540b5fb48b2739fdc15b396fe7cc8fb58ec720f252ba95b2de97
7
+ data.tar.gz: 7bf6217586f5d96e3ec7c6471da2472c36c0beaadf7d8f114d51adb242bd473af9bf559e525e354531580fdc6b5ecf501be5e8087a18986ee82177abf59fca55
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.7.1
1
+ ruby-3.1.2
data/exe/pg_meta CHANGED
@@ -6,6 +6,9 @@ require 'pg_meta'
6
6
  include ShellOpts
7
7
 
8
8
  SPEC = %(
9
+ +e,exclude=SCHEMA
10
+ Exclude SCHEMA. This option can be repeated
11
+
9
12
  -f,format=FORMAT:yaml,marshal,dump
10
13
  Control format of output. FORMAT can be one of 'yaml', 'marshal' or the
11
14
  human-readable 'dump' format (the default)
@@ -13,13 +16,15 @@ SPEC = %(
13
16
  -- DATABASE
14
17
  )
15
18
 
16
- opts, args = ShellOpts.process(SPEC, ARGV, version: PgMeta::VERSION, exception: false)
19
+ opts, args = ShellOpts.process(SPEC, ARGV, exception: false)
17
20
  database = args.expect(1)
18
21
 
19
22
  conn = PgConn.new(database)
20
- meta = PgMeta.new(conn)
23
+ meta = PgMeta.new(conn, exclude_schemas: opts.exclude)
24
+
21
25
  case opts.format
22
26
  when "yaml"; puts meta.to_yaml
23
27
  when "marshal"; puts meta.to_marshal
24
28
  else meta.dump
25
29
  end
30
+
@@ -5,16 +5,25 @@ module PgMeta
5
5
 
6
6
  # Load the database from the given PgConn::Connection object. This method is a
7
7
  # transaction wrapper around #do_load_conn
8
- def self.load_conn(pg_conn)
8
+ def self.load_conn(pg_conn, exclude_schemas: [])
9
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)
11
+ do_load_from_connection(conn, exclude_schemas: exclude_schemas)
12
12
  }
13
13
  end
14
14
 
15
15
  private
16
+ # Generate SQL to exclude schemas
17
+ def self.exclude_sql(field, exclude_schemas)
18
+ sql = "#{field} !~ '^pg_' and #{field} <> 'information_schema'"
19
+ if !exclude_schemas.empty?
20
+ sql += " and #{field} not in ('#{exclude_schemas.join("', '")}')"
21
+ end
22
+ sql
23
+ end
24
+
16
25
  # Load the given database into self. Note that +conn+ is a PG::Connection object
17
- def self.do_load_from_connection(conn)
26
+ def self.do_load_from_connection(conn, exclude_schemas: [])
18
27
  # Get database owner
19
28
  r = conn.exec %(
20
29
  select rolname::varchar
@@ -27,12 +36,15 @@ module PgMeta
27
36
  # Create database object
28
37
  db = Database.new(conn.db, owner)
29
38
 
39
+ # Schema excludes
40
+ exclude_sql =
41
+
30
42
  # Build schemas
31
43
  conn.exec(%(
32
44
  select schema_name::text as name,
33
45
  schema_owner::text as owner
34
46
  from information_schema.schemata
35
- where schema_name !~ '^pg_' and schema_name <> 'information_schema'
47
+ where #{exclude_sql("schema_name", exclude_schemas)}
36
48
  )).each { |row|
37
49
  Schema.new(db, *row.project(:name, :owner))
38
50
  }
@@ -49,7 +61,7 @@ module PgMeta
49
61
  left join information_schema.tables i on
50
62
  i.table_schema = n.nspname
51
63
  and i.table_name = c.relname
52
- where n.nspname !~ '^pg_' and n.nspname <> 'information_schema'
64
+ where #{exclude_sql("n.nspname", exclude_schemas)}
53
65
  and c.relkind in ('r', 'v', 'm')
54
66
  )).each { |row|
55
67
  schema = db.schemas[row[:schema]]
@@ -111,7 +123,7 @@ module PgMeta
111
123
  join pg_rewrite r on r.oid = d.objid
112
124
  join pg_class c2 on c2.oid = r.ev_class
113
125
  join pg_namespace n2 on n2.oid = c2.relnamespace
114
- where n.nspname !~ '^pg_' and n.nspname <> 'information_schema'
126
+ where #{exclude_sql("n.nspname", exclude_schemas)}
115
127
  and c.relkind in ('r', 'v', 'm')
116
128
  and d.classid = 'pg_rewrite'::regclass
117
129
  and d.refclassid = 'pg_class'::regclass
@@ -149,7 +161,7 @@ module PgMeta
149
161
  i.table_schema = n.nspname
150
162
  and i.table_name = c.relname
151
163
  and i.column_name = a.attname
152
- where n.nspname !~ '^pg_' and n.nspname <> 'information_schema'
164
+ where #{exclude_sql("n.nspname", exclude_schemas)}
153
165
  and c.relkind in ('r', 'v', 'm')
154
166
  and a.attnum > 0
155
167
  )).each { |row|
@@ -178,7 +190,7 @@ module PgMeta
178
190
  left join information_schema.check_constraints cc
179
191
  on cc.constraint_schema = c.table_schema and
180
192
  cc.constraint_name = c.constraint_name
181
- where c.table_schema !~ '^pg_' and c.table_schema <> 'information_schema'
193
+ where #{exclude_sql("c.table_schema", exclude_schemas)}
182
194
  and c.constraint_type in ('PRIMARY KEY', 'UNIQUE', 'CHECK')
183
195
  )).each { |row|
184
196
  table = db.schemas[row[:schema]].tables[row[:table]]
@@ -212,7 +224,7 @@ module PgMeta
212
224
  join pg_index x on x.indrelid = c.oid
213
225
  join pg_class i on i.oid = x.indexrelid
214
226
  join ( select attrelid, attname from pg_attribute order by attnum ) a on a.attrelid = i.oid
215
- where n.nspname !~ '^pg_' and n.nspname <> 'information_schema'
227
+ where #{exclude_sql("n.nspname", exclude_schemas)}
216
228
  and i.oid not in (select conindid from pg_constraint)
217
229
  and x.indisunique
218
230
  group by
@@ -261,7 +273,7 @@ module PgMeta
261
273
  join information_schema.key_column_usage cu_refed
262
274
  on cu_refed.constraint_schema = rc.unique_constraint_schema
263
275
  and cu_refed.constraint_name = rc.unique_constraint_name
264
- where cu_refing.table_schema !~ '^pg_' and cu_refing.table_schema <> 'information_schema'
276
+ where #{exclude_sql("cu_refing.table_schema", exclude_schemas)}
265
277
  group by
266
278
  rc.constraint_schema,
267
279
  rc.constraint_name,
@@ -300,7 +312,7 @@ module PgMeta
300
312
  end as "security"
301
313
  from pg_proc p
302
314
  join pg_namespace s on (p.pronamespace = s.oid)
303
- where s.nspname !~ '^pg_' and s.nspname <> 'information_schema'
315
+ where #{exclude_sql("s.nspname", exclude_schemas)}
304
316
  )).each { |row|
305
317
  schema = db.schemas[row[:schema]]
306
318
  klass = (row[:kind] == 'function' ? Function : Procedure)
@@ -1,3 +1,3 @@
1
1
  module PgMeta
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/pg_meta.rb CHANGED
@@ -19,8 +19,8 @@ module PgMeta
19
19
  #
20
20
  # Initialize a Database object
21
21
  #
22
- def self.new(*args)
23
- Database.load_conn(PgConn.ensure(*args))
22
+ def self.new(*args, exclude_schemas: [])
23
+ Database.load_conn(PgConn.ensure(*args), exclude_schemas: exclude_schemas)
24
24
  end
25
25
 
26
26
  # Load data from a YAML object
data/pg_meta.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "pg"
26
26
 
27
27
  spec.add_dependency "pg_conn"
28
- spec.add_dependency "shellopts", "~> 2.0.0"
28
+ spec.add_dependency "shellopts", "~> 2.1"
29
29
 
30
30
  spec.add_development_dependency "bundler"
31
31
  spec.add_development_dependency "rake"
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.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-24 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: indented_io
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 2.0.0
61
+ version: '2.1'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 2.0.0
68
+ version: '2.1'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -152,7 +152,7 @@ files:
152
152
  homepage: http://www.nowhere.com/
153
153
  licenses: []
154
154
  metadata: {}
155
- post_install_message:
155
+ post_install_message:
156
156
  rdoc_options: []
157
157
  require_paths:
158
158
  - lib
@@ -167,8 +167,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  - !ruby/object:Gem::Version
168
168
  version: '0'
169
169
  requirements: []
170
- rubygems_version: 3.1.4
171
- signing_key:
170
+ rubygems_version: 3.3.18
171
+ signing_key:
172
172
  specification_version: 4
173
173
  summary: pg_meta gem
174
174
  test_files: []