fetch postgres version

This commit is contained in:
Aleksandra Sikora 2020-08-05 21:20:41 +02:00
parent 672351add5
commit add325934d
8 changed files with 61 additions and 4 deletions

View File

@ -28,7 +28,7 @@ export const requireAsyncGlobals = ({ dispatch }) => {
return (nextState, finalState, callback) => {
Promise.all([
dispatch(loadConsoleOpts()),
dispatch(fetchServerConfig()),
dispatch(fetchServerConfig),
]).finally(callback);
};
};

View File

@ -4,6 +4,7 @@ import requestAction from '../../utils/requestAction';
import requestActionPlain from '../../utils/requestActionPlain';
import Endpoints, { globalCookiePolicy } from '../../Endpoints';
import { getFeaturesCompatibility } from '../../helpers/versionUtils';
import { getRunSqlQuery } from '../Common/utils/v1QueryUtils';
const SET_MIGRATION_STATUS_SUCCESS = 'Main/SET_MIGRATION_STATUS_SUCCESS';
const SET_MIGRATION_STATUS_ERROR = 'Main/SET_MIGRATION_STATUS_ERROR';
@ -22,6 +23,8 @@ const EXPORT_METADATA_ERROR = 'Main/EXPORT_METADATA_ERROR';
const UPDATE_ADMIN_SECRET_INPUT = 'Main/UPDATE_ADMIN_SECRET_INPUT';
const LOGIN_IN_PROGRESS = 'Main/LOGIN_IN_PROGRESS';
const LOGIN_ERROR = 'Main/LOGIN_ERROR';
const POSTGRES_VERSION_SUCCESS = 'Main/POSTGRES_VERSION_SUCCESS';
const POSTGRES_VERSION_ERROR = 'Main/POSTGRES_VERSION_ERROR';
const RUN_TIME_ERROR = 'Main/RUN_TIME_ERROR';
const registerRunTimeError = data => ({
@ -52,6 +55,28 @@ const setReadOnlyMode = data => ({
data,
});
export const fetchPostgresVersion = dispatch => {
const req = getRunSqlQuery('SELECT version()');
const options = {
method: 'POST',
credentials: globalCookiePolicy,
body: JSON.stringify(req),
};
return dispatch(requestAction(Endpoints.query, options)).then(
({ result }) => {
if (result.length > 1 && result[1].length) {
const matchRes = result[1][0].match(/[0-9]{1,}(\.[0-9]{1,})?/);
if (matchRes.length) {
dispatch({ type: POSTGRES_VERSION_SUCCESS, payload: matchRes[0] });
return;
}
}
dispatch({ type: POSTGRES_VERSION_ERROR });
}
);
};
const featureCompatibilityInit = () => {
return (dispatch, getState) => {
const { serverVersion } = getState().main;
@ -110,7 +135,7 @@ const loadServerVersion = () => dispatch => {
);
};
const fetchServerConfig = () => (dispatch, getState) => {
const fetchServerConfig = (dispatch, getState) => {
const url = Endpoints.serverConfig;
const options = {
method: 'GET',
@ -318,6 +343,16 @@ const mainReducer = (state = defaultState, action) => {
...state,
featuresCompatibility: { ...action.data },
};
case POSTGRES_VERSION_SUCCESS:
return {
...state,
postgresVersion: action.payload,
};
case POSTGRES_VERSION_ERROR:
return {
...state,
postgresVersion: null,
};
default:
return state;
}

View File

@ -21,6 +21,7 @@ import {
loadLatestServerVersion,
featureCompatibilityInit,
emitProClickedEvent,
fetchPostgresVersion,
} from './Actions';
import {
@ -82,7 +83,9 @@ class Main extends React.Component {
});
});
dispatch(fetchServerConfig());
dispatch(fetchPostgresVersion);
dispatch(fetchServerConfig);
}
toggleProPopup = () => {

View File

@ -26,6 +26,7 @@ export interface MainState {
isFetching: boolean;
};
featuresCompatibility: object;
postgresVersion: string | null;
}
const defaultState: MainState = {
@ -56,6 +57,7 @@ const defaultState: MainState = {
isFetching: false,
},
featuresCompatibility: {},
postgresVersion: null,
};
export default defaultState;

View File

@ -443,6 +443,7 @@ class AddTable extends Component {
columnDefaultFunctions,
columnTypeCasts,
checkConstraints,
postgresVersion,
} = this.props;
const getCreateBtnText = () => {
@ -493,6 +494,7 @@ class AddTable extends Component {
onSelect={setFreqUsedColumn}
action={'add'}
dispatch={dispatch}
postgresVersion={postgresVersion}
/>
</div>
<hr />
@ -586,6 +588,7 @@ const mapStateToProps = state => ({
columnTypeCasts: state.tables.columnTypeCasts,
columnDataTypeFetchErr: state.tables.columnDataTypeFetchErr,
schemaList: state.tables.schemaList,
postgresVersion: state.main.postgresVersion,
});
const addTableConnector = connect => connect(mapStateToProps)(AddTable);

View File

@ -18,6 +18,7 @@ interface FrequentlyUsedColumn {
tableName: string,
columnName: string
) => { upSql: string; downSql: string };
minPGVersion?: number;
}
const frequentlyUsedColumns: FrequentlyUsedColumn[] = [
@ -39,8 +40,9 @@ const frequentlyUsedColumns: FrequentlyUsedColumn[] = [
name: 'id',
validFor: ['add'],
type: 'int GENERATED BY DEFAULT AS IDENTITY',
typeText: 'identity (auto-increment, PG +10 only)',
typeText: 'identity (auto-increment)',
primary: true,
minPGVersion: 10,
},
{
name: 'id',
@ -116,15 +118,22 @@ interface FrequentlyUsedColumnSelectorProps {
onSelect: Function;
action: ColumnAction | null;
dispatch: Dispatch | null;
postgresVersion: string | null;
}
const FrequentlyUsedColumnSelector = ({
onSelect,
action = null,
dispatch = null,
postgresVersion,
}: FrequentlyUsedColumnSelectorProps) => {
const frequentlyUsedColumnsOptions = frequentlyUsedColumns
.filter(fuc => !action || fuc.validFor.includes(action))
.filter(col =>
postgresVersion && col.minPGVersion
? parseFloat(postgresVersion) >= col.minPGVersion
: true
)
.map(fuc => {
const { title, subTitle } = getFreqUsedColDisplayInfo(fuc);
return {

View File

@ -126,6 +126,7 @@ const ColumnCreator = ({
dataTypes: restTypes = [],
validTypeCasts,
columnDefaultFunctions,
postgresVersion,
}) => {
const {
colName,
@ -256,6 +257,7 @@ const ColumnCreator = ({
<FrequentlyUsedColumnSelector
onSelect={frequentlyUsedColumn.onSelect}
action={'modify'}
postgresVersion={postgresVersion}
/>
);
};

View File

@ -89,6 +89,7 @@ class ModifyTable extends React.Component {
schemaList,
tableEnum,
rootFieldsEdit,
postgresVersion,
} = this.props;
const dataTypeIndexMap = getAllDataTypeMap(dataTypes);
@ -249,6 +250,7 @@ class ModifyTable extends React.Component {
dataTypes={dataTypes}
validTypeCasts={validTypeCasts}
columnDefaultFunctions={columnDefaultFunctions}
postgresVersion={postgresVersion}
/>
<hr />
{getComputedFieldsSection()}
@ -352,6 +354,7 @@ const mapStateToProps = (state, ownProps) => ({
validTypeCasts: state.tables.columnTypeCasts,
columnDataTypeFetchErr: state.tables.columnDataTypeFetchErr,
schemaList: state.tables.schemaList,
postgresVersion: state.main.postgresVersion,
...state.tables.modify,
});