Class: Pgerd::Database
- Inherits:
-
Object
- Object
- Pgerd::Database
- Defined in:
- lib/pgerd/database.rb
Constant Summary collapse
- TABLES_TO_IGNORE =
%w(ar_internal_metadata schema_migrations)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #all_table_names ⇒ Object
- #all_view_names ⇒ Object
- #connection ⇒ Object
- #foreign_keys ⇒ Object
-
#initialize(name) ⇒ Database
constructor
A new instance of Database.
- #raw_foreign_keys ⇒ Object
- #table_names ⇒ Object
- #tables ⇒ Object
- #views ⇒ Object
Constructor Details
#initialize(name) ⇒ Database
Returns a new instance of Database.
10 11 12 |
# File 'lib/pgerd/database.rb', line 10 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/pgerd/database.rb', line 7 def name @name end |
Instance Method Details
#all_table_names ⇒ Object
14 15 16 17 18 19 |
# File 'lib/pgerd/database.rb', line 14 def all_table_names connection .query("SELECT tablename FROM pg_catalog.pg_tables") .map { |table| table['tablename'] } .reject { |name| name.start_with?('pg_') || name.start_with?('sql_')} end |
#all_view_names ⇒ Object
21 22 23 24 25 26 |
# File 'lib/pgerd/database.rb', line 21 def all_view_names connection .query("SELECT viewname FROM pg_catalog.pg_views WHERE schemaname NOT IN ('pg_catalog', 'information_schema')") .map { |view| view['viewname'] } .reject { |name| name.start_with?('pg_') || name.start_with?('sql_')} end |
#connection ⇒ Object
71 72 73 |
# File 'lib/pgerd/database.rb', line 71 def connection @connection ||= PG.connect(dbname: name) end |
#foreign_keys ⇒ Object
28 29 30 |
# File 'lib/pgerd/database.rb', line 28 def foreign_keys @foreign_keys ||= raw_foreign_keys.map { |data| ForeignKey.new(data) } end |
#raw_foreign_keys ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pgerd/database.rb', line 32 def raw_foreign_keys connection .query " select c.constraint_name\n , x.table_schema as from_schema\n , x.table_name as from_table\n , x.column_name as from_column\n , y.table_schema as to_schema\n , y.table_name as to_table\n , y.column_name as to_column\n from information_schema.referential_constraints c\n join information_schema.key_column_usage x\n on x.constraint_name = c.constraint_name\n join information_schema.key_column_usage y\n on y.ordinal_position = x.position_in_unique_constraint\n and y.constraint_name = c.unique_constraint_name\n order by c.constraint_name, x.ordinal_position\n END_SQL\nend\n" |
#table_names ⇒ Object
52 53 54 |
# File 'lib/pgerd/database.rb', line 52 def table_names all_table_names - TABLES_TO_IGNORE end |
#tables ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/pgerd/database.rb', line 56 def tables @tables ||= table_names .map { |name| Table.new(self, name) } .sort_by { |table| table.foreign_keys.size } .reverse end |
#views ⇒ Object
64 65 66 67 68 69 |
# File 'lib/pgerd/database.rb', line 64 def views @views ||= all_view_names .map { |name| View.new(self, name) } .reverse end |