gadget 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/bin/gadget +18 -4
- data/lib/gadget.rb +35 -8
- data/lib/gadget/version.rb +1 -1
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 974283a8788c4a40b2a75f8d8350f6c316836ff5
|
4
|
+
data.tar.gz: 66abb23005b88002c0fae59554f7695b6f452bf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95e4383e87f251b97eb861e406d6452c3a7ca684d42e384f30311ff6e85a53b2a1ce72bcbd673191fb6fabfb8dd4d6512cd97b1e3372e2c680649b5e163df165
|
7
|
+
data.tar.gz: 167996bc9821904dd2e036de143b02d522aae6d2415536a8508b8059e5bb3c2fdbb209c209bb37d228ba8aaf97faa65697dd3e53178004c1cd7b76cedd6a0c82
|
data/README.md
CHANGED
@@ -32,6 +32,14 @@ If `tablename` is given, returns the columns in only that table.
|
|
32
32
|
Returns a list of all foreign keys in the schema reachable through `conn`.
|
33
33
|
If `tablename` is given, returns the foreign keys in only that table.
|
34
34
|
|
35
|
+
`#functions(conn)`
|
36
|
+
|
37
|
+
Returns a list of all functions in the schema reachable through `conn`.
|
38
|
+
|
39
|
+
`#triggers(conn)`
|
40
|
+
|
41
|
+
Returns a list of all triggers in the schema reachable through `conn`.
|
42
|
+
|
35
43
|
`#dependencies(conn)`
|
36
44
|
|
37
45
|
Returns a structure representing the dependencies between tables in the schema reachable through `conn`.
|
data/bin/gadget
CHANGED
@@ -4,9 +4,23 @@ require 'gadget'
|
|
4
4
|
|
5
5
|
require 'pg'
|
6
6
|
|
7
|
-
dbname = ARGV
|
7
|
+
(command, dbname) = ARGV
|
8
8
|
|
9
|
-
conn = PG::Connection.open(:dbname => dbname)
|
10
|
-
|
9
|
+
conn = PG::Connection.open(:dbname => dbname, :host => 'localhost')
|
10
|
+
result = nil
|
11
|
+
case command
|
12
|
+
when 'dependencies'
|
13
|
+
result = Gadget.dependencies(conn)
|
14
|
+
when 'dependency-graph'
|
15
|
+
result = Gadget.dependency_graph(conn)
|
16
|
+
when 'dependency-order'
|
17
|
+
result = Gadget.tables_in_dependency_order(conn)
|
18
|
+
when 'functions'
|
19
|
+
result = Gadget.functions(conn)
|
20
|
+
when 'tables'
|
21
|
+
result = Gadget.tables(conn)
|
22
|
+
when 'triggers'
|
23
|
+
result = Gadget.triggers(conn)
|
24
|
+
end
|
11
25
|
conn.close
|
12
|
-
p
|
26
|
+
p result
|
data/lib/gadget.rb
CHANGED
@@ -25,9 +25,9 @@ module Gadget
|
|
25
25
|
def self.columns(conn, tablename = nil)
|
26
26
|
sql = <<-END_OF_SQL
|
27
27
|
SELECT t.tablename, a.attname
|
28
|
-
FROM pg_attribute a
|
29
|
-
INNER JOIN pg_class c ON a.attrelid=c.oid
|
30
|
-
INNER JOIN pg_tables t ON c.relname=t.tablename
|
28
|
+
FROM pg_catalog.pg_attribute a
|
29
|
+
INNER JOIN pg_catalog.pg_class c ON a.attrelid=c.oid
|
30
|
+
INNER JOIN pg_catalog.pg_tables t ON c.relname=t.tablename
|
31
31
|
WHERE a.attnum >= 0
|
32
32
|
END_OF_SQL
|
33
33
|
if tablename.nil?
|
@@ -44,11 +44,11 @@ WHERE a.attnum >= 0
|
|
44
44
|
def self.foreign_keys(conn, tablename = nil)
|
45
45
|
sql = <<-END_OF_SQL
|
46
46
|
SELECT t1.tablename AS tablename, pg_constraint.conkey as cols, t2.tablename AS refname, pg_constraint.confkey as refcols
|
47
|
-
FROM pg_constraint
|
48
|
-
INNER JOIN pg_class c1 ON pg_constraint.conrelid=c1.oid
|
49
|
-
INNER JOIN pg_tables t1 ON c1.relname=t1.tablename
|
50
|
-
INNER JOIN pg_class c2 ON pg_constraint.confrelid=c2.oid
|
51
|
-
INNER JOIN pg_tables t2 ON c2.relname=t2.tablename
|
47
|
+
FROM pg_catalog.pg_constraint
|
48
|
+
INNER JOIN pg_catalog.pg_class c1 ON pg_constraint.conrelid=c1.oid
|
49
|
+
INNER JOIN pg_catalog.pg_tables t1 ON c1.relname=t1.tablename
|
50
|
+
INNER JOIN pg_catalog.pg_class c2 ON pg_constraint.confrelid=c2.oid
|
51
|
+
INNER JOIN pg_catalog.pg_tables t2 ON c2.relname=t2.tablename
|
52
52
|
WHERE t1.schemaname='public'
|
53
53
|
AND t2.schemaname='public'
|
54
54
|
AND pg_constraint.contype='f'
|
@@ -105,4 +105,31 @@ AND pg_constraint.contype='f'
|
|
105
105
|
puts "}"
|
106
106
|
end
|
107
107
|
|
108
|
+
def self.functions(conn)
|
109
|
+
rs = conn.exec(<<-END_OF_SQL)
|
110
|
+
SELECT p.oid, p.proname
|
111
|
+
FROM pg_catalog.pg_proc p
|
112
|
+
INNER JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
|
113
|
+
WHERE n.nspname = 'public'
|
114
|
+
END_OF_SQL
|
115
|
+
|
116
|
+
tuples = rs.reduce({}) { | h, row | h[row['proname']] = { :oid => row['oid'] }; h }
|
117
|
+
rs.clear
|
118
|
+
tuples
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.triggers(conn)
|
122
|
+
rs = conn.exec(<<-END_OF_SQL)
|
123
|
+
SELECT tg.oid, tg.tgname, t.tablename
|
124
|
+
FROM pg_catalog.pg_trigger tg
|
125
|
+
INNER JOIN pg_catalog.pg_class c ON tg.tgrelid = c.oid
|
126
|
+
INNER JOIN pg_catalog.pg_tables t ON c.relname = t.tablename
|
127
|
+
WHERE tg.tgconstrrelid = 0
|
128
|
+
END_OF_SQL
|
129
|
+
|
130
|
+
tuples = rs.reduce({}) { | h, row | h[row['tgname']] = { :oid => row['oid'], :tablename => row['tablename'] }; h }
|
131
|
+
rs.clear
|
132
|
+
tuples
|
133
|
+
end
|
134
|
+
|
108
135
|
end
|
data/lib/gadget/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gadget
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig S. Cottingham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -87,6 +87,14 @@ description: |
|
|
87
87
|
Returns a list of all foreign keys in the schema reachable through `conn`.
|
88
88
|
If `tablename` is given, returns the foreign keys in only that table.
|
89
89
|
|
90
|
+
`#functions(conn)`
|
91
|
+
|
92
|
+
Returns a list of all functions in the schema reachable through `conn`.
|
93
|
+
|
94
|
+
`#triggers(conn)`
|
95
|
+
|
96
|
+
Returns a list of all triggers in the schema reachable through `conn`.
|
97
|
+
|
90
98
|
`#dependencies(conn)`
|
91
99
|
|
92
100
|
Returns a structure representing the dependencies between tables in the schema reachable through `conn`.
|