pg_meta 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/pg_meta +4 -1
- data/lib/pg_meta/load_conn.rb +23 -11
- data/lib/pg_meta/version.rb +1 -1
- data/lib/pg_meta.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2862a980ca3d5715c3d60daacd06e78d4b0ae1baccb22e78d5cc97441d303839
|
4
|
+
data.tar.gz: 3619f169ae0641575b8b0e268f2db4f1f1da0655b88f19b0ba4767e814004416
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 671e634a25906f96b1b723ae1e92d2d531256c0cd3c35b9f8b5dda98aed755f16816950e50df05d0dc13ab9ff52bc30182b7e0077ea37b73ec47df322774c764
|
7
|
+
data.tar.gz: 9d767775656d5515e9d38b63e4e6234a806a2f90947a5916b3074d26c6510ddbd69116f8682144f1bb1a83fff8146e5acdffa3bcc9b7909afda9c8bc44a983c3
|
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)
|
@@ -17,7 +20,7 @@ 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)
|
21
24
|
|
22
25
|
case opts.format
|
23
26
|
when "yaml"; puts meta.to_yaml
|
data/lib/pg_meta/load_conn.rb
CHANGED
@@ -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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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)
|
data/lib/pg_meta/version.rb
CHANGED
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
|
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.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: indented_io
|