From ea1d38da57c82b9d6fd5d469f64d293213e3d261 Mon Sep 17 00:00:00 2001 From: Kane York Date: Tue, 28 Jul 2015 09:59:26 -0700 Subject: [PATCH] Add foreign key info, column comment support --- .../explorer-schema-enuminfo.js.es6 | 3 +- .../components/explorer-schema.js.es6 | 19 +++- .../components/explorer-schema-enuminfo.hbs | 12 +-- .../components/explorer-schema-onetable.hbs | 13 ++- assets/stylesheets/explorer.scss | 45 ++++---- plugin.rb | 100 ++++++++++++++++-- 6 files changed, 148 insertions(+), 44 deletions(-) diff --git a/assets/javascripts/discourse/components/explorer-schema-enuminfo.js.es6 b/assets/javascripts/discourse/components/explorer-schema-enuminfo.js.es6 index 702abc0..bee8d2b 100644 --- a/assets/javascripts/discourse/components/explorer-schema-enuminfo.js.es6 +++ b/assets/javascripts/discourse/components/explorer-schema-enuminfo.js.es6 @@ -1,6 +1,5 @@ export default Ember.Component.extend({ - classNames: ['fa', 'fa-info', 'enum-info'], - tagName: 'i', + tagName: 'ol', enuminfo: function() { const hash = this.get('col.enum'); diff --git a/assets/javascripts/discourse/components/explorer-schema.js.es6 b/assets/javascripts/discourse/components/explorer-schema.js.es6 index 3ac383f..27b6dfc 100644 --- a/assets/javascripts/discourse/components/explorer-schema.js.es6 +++ b/assets/javascripts/discourse/components/explorer-schema.js.es6 @@ -29,15 +29,24 @@ export default Ember.Component.extend({ notes = "default " + col.column_default; } } - if (notes) { - col.notes = notes; - col.havetypeinfo = true; + if (col.fkey_info) { + if (notes) { + notes += ", fkey " + col.fkey_info; + } else { + notes = "fkey " + col.fkey_info; + } } - if (col.enum) { - col.havetypeinfo = true; + if (notes) { + col.notes = notes; } + if (col.enum || col.column_desc) { + col.havepopup = true; + } + + col.havetypeinfo = !!(col.notes || col.enum || col.column_desc); + }); } return schema; diff --git a/assets/javascripts/discourse/templates/components/explorer-schema-enuminfo.hbs b/assets/javascripts/discourse/templates/components/explorer-schema-enuminfo.hbs index 8166832..b5e98b3 100644 --- a/assets/javascripts/discourse/templates/components/explorer-schema-enuminfo.hbs +++ b/assets/javascripts/discourse/templates/components/explorer-schema-enuminfo.hbs @@ -1,7 +1,5 @@ -
    - {{#each enuminfo as |enum|}} -
  1. - {{enum.name}} -
  2. - {{/each}} -
+{{#each enuminfo as |enum|}} +
  • + {{enum.name}} +
  • +{{/each}} diff --git a/assets/javascripts/discourse/templates/components/explorer-schema-onetable.hbs b/assets/javascripts/discourse/templates/components/explorer-schema-onetable.hbs index b54e34d..e349dd1 100644 --- a/assets/javascripts/discourse/templates/components/explorer-schema-onetable.hbs +++ b/assets/javascripts/discourse/templates/components/explorer-schema-onetable.hbs @@ -21,11 +21,18 @@ {{col.data_type}} {{#if col.havetypeinfo}}
    + {{#if col.havepopup}} + + + + {{/if}} {{col.notes}} - {{#if col.enum}} - {{explorer-schema-enuminfo col=col}} - {{/if}} {{/if}} diff --git a/assets/stylesheets/explorer.scss b/assets/stylesheets/explorer.scss index 36516ea..ad74aa1 100644 --- a/assets/stylesheets/explorer.scss +++ b/assets/stylesheets/explorer.scss @@ -92,7 +92,7 @@ dd { display: inline-block; vertical-align: text-top; - width: 70px; + width: 90px; color: $tertiary; margin: 0; padding-left: 7px; @@ -101,28 +101,31 @@ .schema-typenotes { color: dark-light-diff($primary, $secondary, 50%, -20%); font-style: italic; + } + .popup-info { + color: dark-light-diff($primary, $secondary, 50%, -20%); - ol { display: none; } - &:hover ol { display: block; } - &:focus ol { display: block; } - .enum-info { + .popup { display: none; } + &:hover .popup { display: block; } + &:focus .popup { display: block; } - ol { - position: absolute; - padding: 5px; - padding-right: calc(5px + 2em); - border: 1px solid; - background: white; - list-style: none; - left: -12em; - top: 6px; - z-index: 10; - > li { - width: 150%; - } - > li:before { - content: attr(value) ": "; - } + .popup { + position: absolute; + padding: 5px; + padding-right: calc(5px + 2em); + border: 1px solid; + background: white; + left: -120px; + width: 180px; + top: 6px; + z-index: 10; + } + + .popup ol { + margin: 0; + list-style: none; + > li:before { + content: attr(value) ": "; } } } diff --git a/plugin.rb b/plugin.rb index 9627735..63fbba7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -177,9 +177,10 @@ select c.table_name table_name, pgd.description column_desc from INFORMATION_SCHEMA.COLUMNS c -inner join pg_catalog.pg_statio_all_tables st on (c.table_schema=st.schemaname and c.table_name=st.relname) -left outer join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) +inner join pg_catalog.pg_statio_all_tables st on (c.table_schema = st.schemaname and c.table_name = st.relname) +left outer join pg_catalog.pg_description pgd on (pgd.objoid = st.relid and pgd.objsubid = c.ordinal_position) where c.table_schema = 'public' +ORDER BY c.table_name, c.ordinal_position SQL by_table = {} # Massage the results into a nicer form @@ -214,10 +215,14 @@ SQL if enum_info.include? full_col_name hash['enum'] = enum_info[full_col_name] end + fkey = fkey_info(hash['table_name'], hash['column_name']) + if fkey + hash['fkey_info'] = fkey + end - tname = hash.delete('table_name') - by_table[tname] ||= [] - by_table[tname] << hash + table_name = hash.delete('table_name') + by_table[table_name] ||= [] + by_table[table_name] << hash end # this works for now, but no big loss if the tables aren't quite sorted @@ -234,9 +239,9 @@ SQL end end - def self.enums @enums ||= { + :'badges.badge_type_id' => BadgeType.all_types, :'category_groups.permission_type' => CategoryGroup.permission_types, :'directory_items.period_type' => DirectoryItem.period_types, :'groups.alias_level' => Group::ALIAS_LEVELS, @@ -266,6 +271,83 @@ SQL enum_info end end + + def self.fkey_info(table, column) + full_name = "#{table}.#{column}" + + if fkey_defaults[column] + fkey_defaults[column] + elsif column =~ /_by_id$/ || column =~ /_user_id$/ + :users + elsif foreign_keys[full_name] + foreign_keys[full_name] + else + nil + end + end + + def self.foreign_keys + @fkey_columns ||= { + :'posts.last_editor_id' => :users, + + :'topics.featured_user1_id' => :users, + :'topics.featured_user2_id' => :users, + :'topics.featured_user3_id' => :users, + :'topics.featured_user4_id' => :users, + :'topics.featured_user5_id' => :users, + + :'users.seen_notification_id' => :notifications, + :'users.uploaded_avatar_id' => :uploads, + :'users.primary_group_id' => :groups, + + :'categories.latest_post_id' => :posts, + :'categories.latest_topic_id' => :topics, + :'categories.parent_category_id' => :categories, + + :'badges.badge_grouping_id' => :badge_groupings, + + :'post_actions.related_post_id' => :posts, + + :'color_scheme_colors.color_scheme_id' => :color_schemes, + + :'incoming_links.incoming_referer_id' => :incoming_referers, + :'incoming_referers.incoming_domain_id' => :incoming_domains, + + :'post_replies.reply_id' => :posts, + + :'quoted_posts.quoted_post_id' => :posts, + + :'topic_link_clicks.topic_link_id' => :topic_links, + :'topic_link_clicks.link_topic_id' => :topics, + :'topic_link_clicks.link_post_id' => :posts, + + :'user_actions.target_topic_id' => :topics, + :'user_actions.target_post_id' => :posts, + + :'user_avatars.custom_upload_id' => :uploads, + :'user_avatars.gravatar_upload_id' => :uploads, + + :'user_badges.notification_id' => :notifications, + + :'user_profiles.card_image_badge_id' => :badges, + }.with_indifferent_access + end + + def self.fkey_defaults + @fkey_defaults ||= { + :user_id => :users, + # :*_by_id => :users, + # :*_user_id => :users, + + :category_id => :categories, + :group_id => :groups, + :post_id => :posts, + :post_action_id => :post_actions, + :topic_id => :topics, + :upload_id => :uploads, + + }.with_indifferent_access + end end # Reimplement a couple ActiveRecord methods, but use PluginStore for storage instead @@ -784,3 +866,9 @@ SQL end +# polyfill +class ::BadgeType + def self.all_types + @all_types ||= Enum.new(:gold, :silver, :bronze, start: 1) + end +end