Add foreign key info, column comment support

This commit is contained in:
Kane York 2015-07-28 09:59:26 -07:00
parent ddf88fc994
commit ea1d38da57
6 changed files with 148 additions and 44 deletions

View File

@ -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');

View File

@ -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;

View File

@ -1,7 +1,5 @@
<ol>
{{#each enuminfo as |enum|}}
<li value="{{enum.value}}">
{{enum.name}}
</li>
{{/each}}
</ol>
{{#each enuminfo as |enum|}}
<li value="{{enum.value}}">
{{enum.name}}
</li>
{{/each}}

View File

@ -21,11 +21,18 @@
{{col.data_type}}
{{#if col.havetypeinfo}}
<br>
{{#if col.havepopup}}
<i class="fa fa-info popup-info">
<div class="popup">
{{col.column_desc}}
{{#if col.enum}}
{{explorer-schema-enuminfo col=col}}
{{/if}}
</div>
</i>
{{/if}}
<span class="schema-typenotes">
{{col.notes}}
{{#if col.enum}}
{{explorer-schema-enuminfo col=col}}
{{/if}}
</span>
{{/if}}
</dd>

View File

@ -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) ": ";
}
}
}

100
plugin.rb
View File

@ -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