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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 974283a8788c4a40b2a75f8d8350f6c316836ff5
4
- data.tar.gz: 66abb23005b88002c0fae59554f7695b6f452bf0
3
+ metadata.gz: a9eefefd9392371161169e66ec7c6ecfc9e855dd
4
+ data.tar.gz: e1b1b8b2cd5be52ca74dfad670e833176f41ac16
5
5
  SHA512:
6
- metadata.gz: 95e4383e87f251b97eb861e406d6452c3a7ca684d42e384f30311ff6e85a53b2a1ce72bcbd673191fb6fabfb8dd4d6512cd97b1e3372e2c680649b5e163df165
7
- data.tar.gz: 167996bc9821904dd2e036de143b02d522aae6d2415536a8508b8059e5bb3c2fdbb209c209bb37d228ba8aaf97faa65697dd3e53178004c1cd7b76cedd6a0c82
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, dbname) = ARGV
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
@@ -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=$1"
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 t1.tablename AS tablename, pg_constraint.conkey as cols, t2.tablename AS refname, pg_constraint.confkey as refcols
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=$1"
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] },
@@ -1,3 +1,3 @@
1
1
  module Gadget
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gadget
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig S. Cottingham