Class: PgDiff::Database
- Inherits:
-
Object
- Object
- PgDiff::Database
- Defined in:
- lib/database.rb
Instance Attribute Summary collapse
-
#domains ⇒ Object
Returns the value of attribute domains.
-
#functions ⇒ Object
Returns the value of attribute functions.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#schemas ⇒ Object
Returns the value of attribute schemas.
-
#sequences ⇒ Object
Returns the value of attribute sequences.
-
#tables ⇒ Object
Returns the value of attribute tables.
-
#triggers ⇒ Object
Returns the value of attribute triggers.
-
#views ⇒ Object
Returns the value of attribute views.
Instance Method Summary collapse
-
#initialize(conn) ⇒ Database
constructor
A new instance of Database.
Constructor Details
#initialize(conn) ⇒ Database
Returns a new instance of Database.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/database.rb', line 5 def initialize(conn) cls_query = " SELECT n.nspname, c.relname, c.relkind\n FROM pg_catalog.pg_class c\n LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n WHERE c.relkind IN ('r','S','v')\n AND n.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')\n ORDER BY 1,2;\n EOT\n @views = {}\n @tables = {}\n @sequences = {}\n @schemas = {}\n @domains = {}\n @functions = {}\n @rules = {}\n @triggers = {}\n\n conn.query(cls_query).each do |tuple|\n schema = tuple['nspname']\n relname = tuple['relname']\n relkind = tuple['relkind']\n case relkind\n when 'r'\n @tables[\"\#{schema}.\#{relname}\"] = Table.new(conn, schema, relname)\n when 'v'\n @views[\"\#{schema}.\#{relname}\"] = View.new(conn, schema, relname)\n when 'S'\n @sequences[\"\#{schema}.\#{relname}\"] = Sequence.new(conn, schema, relname)\n end\n end\n\n domain_qry = <<-EOT\n SELECT n.nspname, t.typname, pg_catalog.format_type(t.typbasetype, t.typtypmod) || ' ' ||\n CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '|| t.typdefault\n ELSE ''\n END AS def\n FROM pg_catalog.pg_type t\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n WHERE t.typtype = 'd'\n ORDER BY 1, 2\n EOT\n conn.query(domain_qry).each do |tuple|\n schema = tuple['nspname']\n typename = tuple['typname']\n value = tuple['def']\n @domains[\"\#{schema}.\#{typename}\"] = value\n end\n\n schema_qry = <<-EOT\n select nspname from pg_namespace\n EOT\n conn.query(schema_qry).each do |tuple|\n schema = tuple['nspname']\n @schemas[schema] = schema\n end\n\n func_query = <<-EOT\n SELECT proname AS function_name\n , nspname AS namespace\n , lanname AS language_name\n , pg_catalog.obj_description(pg_proc.oid, 'pg_proc') AS comment\n , proargtypes AS function_args\n , proargnames AS function_arg_names\n , prosrc AS source_code\n , proretset AS returns_set\n , prorettype AS return_type,\n provolatile, proisstrict, prosecdef\n FROM pg_catalog.pg_proc\n JOIN pg_catalog.pg_language ON (pg_language.oid = prolang)\n JOIN pg_catalog.pg_namespace ON (pronamespace = pg_namespace.oid)\n JOIN pg_catalog.pg_type ON (prorettype = pg_type.oid)\n WHERE pg_namespace.nspname !~ 'pg_catalog|information_schema'\n AND proname != 'plpgsql_call_handler'\n AND proname != 'plpgsql_validator'\n EOT\n\n conn.exec(func_query).each_with_index do |tuple, i|\n func = Function.new(conn, tuple)\n @functions[func.signature] = func\n end\n\n rule_query = <<-EOT\n select schemaname || '.' || tablename || '.' || rulename as rule_name,\n schemaname || '.' || tablename as tab_name,\n rulename, definition\n from pg_rules\n where schemaname !~ 'pg_catalog|information_schema'\n EOT\n conn.exec(rule_query).each do |tuple|\n @rules[tuple['rule_name']] = Rule.new(tuple['tab_name'], tuple['rulename'], tuple['definition'])\n end\n\n trigger_query = <<-EOT\n select nspname || '.' || relname as tgtable, tgname, pg_get_triggerdef(t.oid) as tg_def\n from pg_trigger t join pg_class c ON (tgrelid = c.oid ) JOIN pg_namespace n ON (c.relnamespace = n.oid)\n where not tgisinternal\n and nspname !~ 'pg_catalog|information_schema'\n EOT\n conn.exec(trigger_query).each do |tuple|\n @triggers[tuple['tgtable'] + \".\" + tuple['tgname']] = Trigger.new(tuple['tgtable'], tuple['tgname'], tuple['tg_def'])\n end\nend\n" |
Instance Attribute Details
#domains ⇒ Object
Returns the value of attribute domains.
3 4 5 |
# File 'lib/database.rb', line 3 def domains @domains end |
#functions ⇒ Object
Returns the value of attribute functions.
3 4 5 |
# File 'lib/database.rb', line 3 def functions @functions end |
#rules ⇒ Object
Returns the value of attribute rules.
3 4 5 |
# File 'lib/database.rb', line 3 def rules @rules end |
#schemas ⇒ Object
Returns the value of attribute schemas.
3 4 5 |
# File 'lib/database.rb', line 3 def schemas @schemas end |
#sequences ⇒ Object
Returns the value of attribute sequences.
3 4 5 |
# File 'lib/database.rb', line 3 def sequences @sequences end |
#tables ⇒ Object
Returns the value of attribute tables.
3 4 5 |
# File 'lib/database.rb', line 3 def tables @tables end |
#triggers ⇒ Object
Returns the value of attribute triggers.
3 4 5 |
# File 'lib/database.rb', line 3 def triggers @triggers end |
#views ⇒ Object
Returns the value of attribute views.
3 4 5 |
# File 'lib/database.rb', line 3 def views @views end |