Skip to content

Commit

Permalink
option to reload remote schemas in 'reload metadata' (fix #3792, #4117)…
Browse files Browse the repository at this point in the history
… (#4141)

* option to reload remote schemas in 'reload_metadata' API, fix #3792, #4117

* add tests

* update changelog

* update docs api reference for 'reload_metadata'

* send reload_remote_schemas: true with the reload_metadata query

* add reload remote schemas checkbox; minor refactor

* Add a Note about cache invalidation and inconsistent metadata objects

* Small pluralization agreement tweak in docs

* Remove duplicated line in CHANGELOG

* no-op refactor

Suggested by Alexis @lexi-lambda

* Update server/src-lib/Hasura/RQL/DDL/RemoteSchema.hs

As suggested by @lexi-lambda

Co-Authored-By: Alexis King <lexi.lambda@gmail.com>

* fix tests

* requested changes

* comment 'replaceMetadataToOrdJson' unit tests

Co-authored-by: Rishichandra Wawhal <rishi@hasura.io>
Co-authored-by: Alexis King <lexi.lambda@gmail.com>
Co-authored-by: Tirumarai Selvan <tiru@hasura.io>
  • Loading branch information
4 people authored Mar 26, 2020
1 parent 877000b commit fd6535b
Show file tree
Hide file tree
Showing 24 changed files with 345 additions and 190 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
- console: add dropdown for enum fields in insert/edit rows page (close #3748) (#3810)

If a table has a field referencing an enum table via a foreign key, then there will be a select dropdown with all possible enum values for that field on `Insert Row` and `Edit Row` views.

- console: generate unique exported metadata filenames (close #1772) (#4106)

Exporting metadata from the console will now generate metadata files of the form `hasura_metadata_<timestamp>.json`.
Exporting metadata from the console will now generate metadata files of the form `hasura_metadata_<timestamp>.json`.

- cli(migrations-docker): add support for v2 config (close #3969)

A new CLI migrations image is introduced to account for the new CLI workflow. If you're have a project with `version: 2` in `config.yaml`, you should use the new image: `hasura/graphql-engine:v1.2.0-cli-migrations-v2`. Mount the migrations at `/hasura-migrations` and metadata at `/hasura-metadata`.

### Other changes

- cli: fix init command to generate correct config.yaml (close #4036)
Expand Down Expand Up @@ -101,3 +101,4 @@
- server: add 'ID' to default scalars in custom types (fix #4061)
- console: add design system base components (#3866)
- docs: add docs for redeliver_event API
- option to reload remote schemas in 'reload_metadata' API (fix #3792, #4117)
41 changes: 41 additions & 0 deletions console/src/components/Common/utils/v1QueryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,44 @@ export const getEnumOptionsQuery = (request, currentSchema) => {
},
};
};

export const inconsistentObjectsQuery = {
type: 'get_inconsistent_metadata',
args: {},
};

export const dropInconsistentObjectsQuery = {
type: 'drop_inconsistent_metadata',
args: {},
};

export const getReloadMetadataQuery = shouldReloadRemoteSchemas => ({
type: 'reload_metadata',
args: {
reload_remote_schemas: shouldReloadRemoteSchemas,
},
});

export const getReloadRemoteSchemaCacheQuery = remoteSchemaName => {
return {
type: 'reload_remote_schema',
args: {
name: remoteSchemaName,
},
};
};

export const exportMetadataQuery = {
type: 'export_metadata',
args: {},
};

export const generateReplaceMetadataQuery = metadataJson => ({
type: 'replace_metadata',
args: metadataJson,
});

export const resetMetadataQuery = {
type: 'clear_metadata',
args: {},
};
18 changes: 10 additions & 8 deletions console/src/components/Error/ErrorBoundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ class ErrorBoundary extends React.Component {
registerRunTimeError({ message: error.message, stack: error.stack })
);

dispatch(loadInconsistentObjects(true)).then(() => {
if (this.props.metadata.inconsistentObjects.length > 0) {
if (!isMetadataStatusPage()) {
this.resetState();
this.props.dispatch(redirectToMetadataStatus());
dispatch(loadInconsistentObjects({ shouldReloadMetadata: true })).then(
() => {
if (this.props.metadata.inconsistentObjects.length > 0) {
if (!isMetadataStatusPage()) {
this.resetState();
this.props.dispatch(redirectToMetadataStatus());
}
} else {
console.error(error);
}
} else {
console.error(error);
}
});
);
}

render() {
Expand Down
8 changes: 5 additions & 3 deletions console/src/components/Main/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class Main extends React.Component {
dispatch(loadServerVersion()).then(() => {
dispatch(featureCompatibilityInit());

dispatch(loadInconsistentObjects()).then(() => {
this.handleMetadataRedirect();
});
dispatch(loadInconsistentObjects({ shouldReloadMetadata: false })).then(
() => {
this.handleMetadataRedirect();
}
);

dispatch(loadLatestServerVersion()).then(() => {
this.setShowUpdateNotification();
Expand Down
2 changes: 1 addition & 1 deletion console/src/components/Services/Data/DataActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ const loadSchema = configOptions => {
allSchemas: consistentSchemas || maybeInconsistentSchemas,
});

dispatch(loadInconsistentObjects());
dispatch(loadInconsistentObjects({ shouldReloadMetadata: false }));
},
error => {
console.error('loadSchema error: ' + JSON.stringify(error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const loadTriggers = triggerNames => (dispatch, getState) => {
type: LOAD_TRIGGER_LIST,
triggerList: consistentTriggers,
});
dispatch(loadInconsistentObjects(false));
dispatch(loadInconsistentObjects({ shouldReloadMetadata: false }));
},
error => {
console.error('Failed to load triggers' + JSON.stringify(error));
Expand Down
3 changes: 3 additions & 0 deletions console/src/components/Services/RemoteSchema/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const getAllRemoteSchemas = getState => {
return getState().remoteSchemas.listData.remoteSchemas;
};
132 changes: 78 additions & 54 deletions console/src/components/Services/Settings/Actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requestAction from '../../../utils/requestAction';
import { push } from 'react-router-redux';
import globals from '../../../Globals';
import endpoints from '../../../Endpoints';
import endpoints, { globalCookiePolicy } from '../../../Endpoints';
import defaultState from './State';
import { filterInconsistentMetadataObjects } from './utils';
import {
Expand All @@ -15,6 +15,15 @@ import {
showSuccessNotification,
showErrorNotification,
} from '../Common/Notification';
import {
inconsistentObjectsQuery,
getReloadMetadataQuery,
getReloadRemoteSchemaCacheQuery,
dropInconsistentObjectsQuery,
exportMetadataQuery,
generateReplaceMetadataQuery,
resetMetadataQuery,
} from '../../Common/utils/v1QueryUtils';

const LOAD_INCONSISTENT_OBJECTS = 'Metadata/LOAD_INCONSISTENT_OBJECTS';
const LOADING_METADATA = 'Metadata/LOADING_METADATA';
Expand All @@ -30,52 +39,28 @@ const UPDATE_ALLOWED_QUERY = 'Metadata/UPDATE_ALLOWED_QUERY';
const DELETE_ALLOWED_QUERY = 'Metadata/DELETE_ALLOWED_QUERY';
const DELETE_ALLOW_LIST = 'Metadata/DELETE_ALLOW_LIST';

const getInconsistentObjectsQuery = {
type: 'get_inconsistent_metadata',
args: {},
};

const reloadCacheQuery = {
type: 'reload_metadata',
args: {},
};

const reloadRemoteSchemaCacheQuery = remoteSchemaName => {
return {
type: 'reload_remote_schema',
args: {
name: remoteSchemaName,
},
};
};

const reloadRemoteSchemaCacheAndGetInconsistentObjectsQuery = remoteSchemaName => {
return {
type: 'bulk',
args: [
reloadRemoteSchemaCacheQuery(remoteSchemaName),
getInconsistentObjectsQuery,
getReloadRemoteSchemaCacheQuery(remoteSchemaName),
inconsistentObjectsQuery,
],
};
};

const reloadCacheAndGetInconsistentObjectsQuery = {
const getReloadCacheAndGetInconsistentObjectsQuery = shouldReloadRemoteSchemas => ({
type: 'bulk',
args: [reloadCacheQuery, getInconsistentObjectsQuery],
};

const dropInconsistentObjectsQuery = {
type: 'drop_inconsistent_metadata',
args: {},
};
args: [
getReloadMetadataQuery(shouldReloadRemoteSchemas),
inconsistentObjectsQuery,
],
});

export const exportMetadata = (successCb, errorCb) => (dispatch, getState) => {
const { dataHeaders } = getState().tables;

const query = {
type: 'export_metadata',
args: {},
};
const query = exportMetadataQuery;

const options = {
method: 'POST',
Expand All @@ -99,13 +84,6 @@ export const replaceMetadata = (newMetadata, successCb, errorCb) => (
getState
) => {
const exportSuccessCb = oldMetadata => {
const generateReplaceMetadataQuery = metadataJson => {
return {
type: 'replace_metadata',
args: metadataJson,
};
};

const upQuery = generateReplaceMetadataQuery(newMetadata);
const downQuery = generateReplaceMetadataQuery(oldMetadata);

Expand Down Expand Up @@ -150,6 +128,33 @@ export const replaceMetadata = (newMetadata, successCb, errorCb) => (
dispatch(exportMetadata(exportSuccessCb, exportErrorCb));
};

export const resetMetadata = (successCb, errorCb) => (dispatch, getState) => {
const headers = getState().tables.dataHeaders;

const options = {
method: 'POST',
credentials: globalCookiePolicy,
headers: headers || {},
body: JSON.stringify(resetMetadataQuery),
};

return dispatch(requestAction(endpoints.query, options)).then(
() => {
if (successCb) {
successCb();
}
dispatch(showSuccessNotification('Metadata reset successfully!'));
},
error => {
console.error(error);
dispatch(showErrorNotification('Metadata reset failed', null, error));
if (errorCb) {
errorCb(error);
}
}
);
};

export const replaceMetadataFromFile = (
fileContent,
successCb,
Expand Down Expand Up @@ -221,17 +226,17 @@ const handleInconsistentObjects = inconsistentObjects => {
};
};

export const loadInconsistentObjects = (
shouldReloadCache,
successCb,
failureCb
) => {
export const loadInconsistentObjects = (reloadConfig, successCb, failureCb) => {
return (dispatch, getState) => {
const headers = getState().tables.dataHeaders;

const loadQuery = shouldReloadCache
? reloadCacheAndGetInconsistentObjectsQuery
: getInconsistentObjectsQuery;
const { shouldReloadMetadata, shouldReloadRemoteSchemas } = reloadConfig;

const loadQuery = shouldReloadMetadata
? getReloadCacheAndGetInconsistentObjectsQuery(
shouldReloadRemoteSchemas === false ? false : true
)
: inconsistentObjectsQuery;

dispatch({ type: LOADING_METADATA });
return dispatch(
Expand All @@ -242,7 +247,7 @@ export const loadInconsistentObjects = (
})
).then(
data => {
const inconsistentObjects = shouldReloadCache
const inconsistentObjects = shouldReloadMetadata
? data[1].inconsistent_objects
: data.inconsistent_objects;

Expand Down Expand Up @@ -301,13 +306,26 @@ export const reloadRemoteSchema = (remoteSchemaName, successCb, failureCb) => {
};
};

export const reloadMetadata = (successCb, failureCb) => {
export const reloadMetadata = (
shouldReloadRemoteSchemas,
successCb,
failureCb
) => {
return dispatch => {
return dispatch(loadInconsistentObjects(true, successCb, failureCb));
return dispatch(
loadInconsistentObjects(
{
shouldReloadMetadata: true,
shouldReloadRemoteSchemas: shouldReloadRemoteSchemas,
},
successCb,
failureCb
)
);
};
};

export const dropInconsistentObjects = () => {
export const dropInconsistentObjects = (successCb, failureCb) => {
return (dispatch, getState) => {
const headers = getState().tables.dataHeaders;
dispatch({ type: DROP_INCONSISTENT_METADATA });
Expand All @@ -321,7 +339,10 @@ export const dropInconsistentObjects = () => {
() => {
dispatch({ type: DROPPED_INCONSISTENT_METADATA });
dispatch(showSuccessNotification('Dropped inconsistent metadata'));
dispatch(loadInconsistentObjects(false));
dispatch(loadInconsistentObjects({ shouldReloadRemoteSchemas: false }));
if (successCb) {
successCb();
}
},
error => {
console.error(error);
Expand All @@ -333,6 +354,9 @@ export const dropInconsistentObjects = () => {
error
)
);
if (failureCb) {
failureCb();
}
}
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const MetadataOptions = props => {
<h4>Reload metadata</h4>
<div className={styles.content_width}>
Refresh Hasura metadata, typically required if you have changed the
underlying postgres.
underlying postgres or if you have updated your remote schemas.
</div>
</div>

Expand All @@ -61,9 +61,7 @@ const MetadataOptions = props => {

return (
<div
className={`${styles.clear_fix} ${styles.padd_left} ${styles.padd_top} ${
styles.metadata_wrapper
} container-fluid`}
className={`${styles.clear_fix} ${styles.padd_left} ${styles.padd_top} ${styles.metadata_wrapper} container-fluid`}
>
<div className={styles.subHeader}>
<h2 className={`${styles.heading_text} ${styles.remove_pad_bottom}`}>
Expand Down
Loading

0 comments on commit fd6535b

Please sign in to comment.