gadget 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/gadget +7 -1
- data/lib/gadget.rb +15 -12
- data/lib/gadget/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9eefefd9392371161169e66ec7c6ecfc9e855dd
|
4
|
+
data.tar.gz: e1b1b8b2cd5be52ca74dfad670e833176f41ac16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86a0ddd76729246d2e3e81043d1e47af8fefd760ba358f2a11b716870bb68468dbd1d1765f516275fbd0746df99ebf9188ee9aff9c9b22f478ae2fecc4ca3a31
|
7
|
+
data.tar.gz: bf10d81863eded6472abbb34f16b5284a5a99acbcf19142278d1852e8083b700dd7e7a565ecba94dfe69620762003e8fd426580d932db2eff2d9981644d0432e
|
data/bin/gadget
CHANGED
@@ -4,23 +4,29 @@ require 'gadget'
|
|
4
4
|
|
5
5
|
require 'pg'
|
6
6
|
|
7
|
-
(command,
|
7
|
+
(dbname, command, *cmdargs) = ARGV
|
8
8
|
|
9
9
|
conn = PG::Connection.open(:dbname => dbname, :host => 'localhost')
|
10
10
|
result = nil
|
11
11
|
case command
|
12
|
+
when 'columns'
|
13
|
+
result = Gadget.columns(conn, cmdargs.shift)
|
12
14
|
when 'dependencies'
|
13
15
|
result = Gadget.dependencies(conn)
|
14
16
|
when 'dependency-graph'
|
15
17
|
result = Gadget.dependency_graph(conn)
|
16
18
|
when 'dependency-order'
|
17
19
|
result = Gadget.tables_in_dependency_order(conn)
|
20
|
+
when 'foreign-keys', 'fks'
|
21
|
+
result = Gadget.foreign_keys(conn, cmdargs.shift)
|
18
22
|
when 'functions'
|
19
23
|
result = Gadget.functions(conn)
|
20
24
|
when 'tables'
|
21
25
|
result = Gadget.tables(conn)
|
22
26
|
when 'triggers'
|
23
27
|
result = Gadget.triggers(conn)
|
28
|
+
else
|
29
|
+
result = "unknown command '#{command}'"
|
24
30
|
end
|
25
31
|
conn.close
|
26
32
|
p result
|
data/lib/gadget.rb
CHANGED
@@ -26,14 +26,14 @@ module Gadget
|
|
26
26
|
sql = <<-END_OF_SQL
|
27
27
|
SELECT t.tablename, a.attname
|
28
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
|
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?
|
34
34
|
rs = conn.exec(sql)
|
35
35
|
else
|
36
|
-
sql += " AND t.tablename
|
36
|
+
sql += " AND t.tablename = $1"
|
37
37
|
rs = conn.exec_params(sql, [ tablename ])
|
38
38
|
end
|
39
39
|
tuples = rs.reduce({}) { | h, row | h[row['tablename']] ||= { :columns => [] }; h[row['tablename']][:columns] << row['attname']; h }
|
@@ -43,20 +43,22 @@ WHERE a.attnum >= 0
|
|
43
43
|
|
44
44
|
def self.foreign_keys(conn, tablename = nil)
|
45
45
|
sql = <<-END_OF_SQL
|
46
|
-
SELECT
|
46
|
+
SELECT pg_constraint.conname AS name,
|
47
|
+
t1.tablename AS tablename, pg_constraint.conkey AS cols,
|
48
|
+
t2.tablename AS refname, pg_constraint.confkey AS refcols
|
47
49
|
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
|
-
WHERE t1.schemaname='public'
|
53
|
-
AND t2.schemaname='public'
|
54
|
-
AND pg_constraint.contype='f'
|
50
|
+
INNER JOIN pg_catalog.pg_class c1 ON pg_constraint.conrelid = c1.oid
|
51
|
+
INNER JOIN pg_catalog.pg_tables t1 ON c1.relname = t1.tablename
|
52
|
+
INNER JOIN pg_catalog.pg_class c2 ON pg_constraint.confrelid = c2.oid
|
53
|
+
INNER JOIN pg_catalog.pg_tables t2 ON c2.relname = t2.tablename
|
54
|
+
WHERE t1.schemaname = 'public'
|
55
|
+
AND t2.schemaname = 'public'
|
56
|
+
AND pg_constraint.contype = 'f'
|
55
57
|
END_OF_SQL
|
56
58
|
if tablename.nil?
|
57
59
|
rs = conn.exec(sql)
|
58
60
|
else
|
59
|
-
sql += " AND t1.tablename
|
61
|
+
sql += " AND t1.tablename = $1"
|
60
62
|
rs = conn.exec_params(sql, [ tablename ])
|
61
63
|
end
|
62
64
|
tuples = rs.reduce({}) do | h, row |
|
@@ -65,6 +67,7 @@ AND pg_constraint.contype='f'
|
|
65
67
|
col_names = self.columns(conn, name)[name][:columns]
|
66
68
|
refcol_names = self.columns(conn, row['refname'])[row['refname']][:columns]
|
67
69
|
new_ref = {
|
70
|
+
:name => row['name'],
|
68
71
|
:cols => row['cols'].sub(/\A\{|\}\z/, '').split(',').map { | idx | col_names[idx.to_i - 1] },
|
69
72
|
:refname => row['refname'],
|
70
73
|
:refcols => row['refcols'].sub(/\A\{|\}\z/, '').split(',').map { | idx | refcol_names[idx.to_i - 1] },
|
data/lib/gadget/version.rb
CHANGED