Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL Lab] Adding indexes to table metadata #1160

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,27 @@ class SqlEditorTopToolbar extends React.Component {
const tableName = tableOpt.value;
const qe = this.props.queryEditor;
const url = `/caravel/table/${qe.dbId}/${tableName}/${qe.schema}/`;

this.setState({ tableLoading: true });
Copy link
Member

@bkyryliuk bkyryliuk Sep 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find the usage of the tableLoading

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I could find the place in code where tableLoading is read from the state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you are right, sorry i missed that too. good catch!
@mistercrunch do you have a loading state you are adding to use with this.state.tableLoading?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state.tableLoading is used at line 164. It's the Select's inline loading spinner. I used it only for when the content was loading before, now I also use it while the table metadata is loading.

$.get(url, (data) => {
this.props.actions.addTable({
id: shortid.generate(),
dbId: this.props.queryEditor.dbId,
queryEditorId: this.props.queryEditor.id,
name: data.name,
indexes: data.indexes,
schema: qe.schema,
columns: data.columns,
expanded: true,
});
this.setState({ tableLoading: false });
})
.fail(() => {
this.props.actions.addAlert({
msg: 'Error occurred while fetching metadata',
bsStyle: 'danger',
});
this.setState({ tableLoading: false });
});
}
render() {
Expand Down
23 changes: 23 additions & 0 deletions caravel/assets/javascripts/SqlLab/components/TableElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as Actions from '../actions';
import shortid from 'shortid';
import ModalTrigger from '../../components/ModalTrigger.jsx';

class TableElement extends React.Component {
setSelectStar() {
Expand Down Expand Up @@ -86,6 +87,27 @@ class TableElement extends React.Component {
</a>
);
}
let keyLink;
if (this.props.table.indexes && this.props.table.indexes.length > 0) {
keyLink = (
<ModalTrigger
modalTitle={
<div>
Keys for table <strong>{this.props.table.name}</strong>
</div>
}
modalBody={
<pre>{JSON.stringify(this.props.table.indexes, null, 4)}</pre>
}
triggerNode={
<Link
className="fa fa-key pull-left m-l-2"
tooltip={`View indexes (${this.props.table.indexes.length})`}
/>
}
/>
);
}
return (
<div>
<div className="clearfix">
Expand All @@ -94,6 +116,7 @@ class TableElement extends React.Component {
</div>
<div className="pull-right">
<ButtonGroup className="ws-el-controls pull-right">
{keyLink}
<Link
className="fa fa-pencil pull-left m-l-2"
onClick={this.setSelectStar.bind(this)}
Expand Down
3 changes: 3 additions & 0 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,9 @@ def get_table(self, table_name, schema=None):
def get_columns(self, table_name, schema=None):
return self.inspector.get_columns(table_name, schema)

def get_indexes(self, table_name, schema=None):
return self.inspector.get_indexes(table_name, schema)

@property
def sqlalchemy_uri_decrypted(self):
conn = sqla.engine.url.make_url(self.sqlalchemy_uri)
Expand Down
3 changes: 3 additions & 0 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,9 +1535,11 @@ def table(self, database_id, table_name, schema):
schema = None if schema in ('null', 'undefined') else schema
mydb = db.session.query(models.Database).filter_by(id=database_id).one()
cols = []
indexes = []
t = mydb.get_columns(table_name, schema)
try:
t = mydb.get_columns(table_name, schema)
indexes = mydb.get_indexes(table_name, schema)
except Exception as e:
return Response(
json.dumps({'error': utils.error_msg_from_exception(e)}),
Expand All @@ -1556,6 +1558,7 @@ def table(self, database_id, table_name, schema):
tbl = {
'name': table_name,
'columns': cols,
'indexes': indexes,
}
return Response(json.dumps(tbl), mimetype="application/json")

Expand Down