Repository: hasura/graphql-engine
File: console/src/components/Services/Data/TableModify/ModifyActions.js (JavaScript)

Code before:
const addPrimaryKey = (columnIndex, pkIndex) => ({
  type: ADD_PRIMARY_KEY,
  column: columnIndex,
  pk: pkIndex,
});

const removePrimaryKey = pkIndex => ({
  type: REMOVE_PRIMARY_KEY,
  pk: pkIndex,
});

const resetPrimaryKeys = () => ({
  type: RESET_PRIMARY_KEY,
});

export const removeCheckConstraint = constraintName => (dispatch, getState) => {
  const isOk = window.confirm('Are you absolutely sure?');
  if (!isOk) return;

  const { currentTable: tableName, currentSchema } = getState().tables;

  const tableSchema = getState().tables.allSchemas.find(s => {
    return s.table_name === tableName && s.table_schema === currentSchema;
  });

  const constraint = tableSchema.check_constraints.find(
    cc => cc.constraint_name === constraintName
  );
  const upQuery = {
    type: 'run_sql',
    args: {
      sql: `alter table "${currentSchema}"."${tableName}" drop constraint "${constraintName}"`,
    },
  };
  const downQuery = {
    type: 'run_sql',
    args: {
      sql: `alter table "${currentSchema}"."${tableName}" add constraint "${constraintName}" ${
        constraint.check
      };`,
    },
  };

  const migrationName = `drop_check_constraint_${currentSchema}_${tableName}_${constraintName}`;
  const requestMsg = 'Deleting check constraint...';
  const successMsg = 'Check constraint deleted';
  const errorMsg = 'Deleting check constraint failed';
  const customOnSuccess = () => {};
  const customOnError = err => {
    dispatch({ type: UPDATE_MIGRATION_STATUS_ERROR, data: err });
  };

Review comment (suggestion):
create util functions for this is `pgUtils.js` called  `getTableCheckConstraints(table)` and `getTableCheckConstraint(table, constraintName)`. 
Rule of thumb we do not deconstruct PG objects outside this file and we try to use the base fns to define functions on top of them. eg. `getTableConstraint` should use `getTableConstraints` internally. You can see this pattern being followed in the pgutils file already to avoid duplication of logic

Code after:

const setPrimaryKeys = pks => ({
  type: SET_PRIMARY_KEYS,
  pks,
});

const addPrimaryKey = (columnIndex, pkIndex) => ({
  type: ADD_PRIMARY_KEY,
  column: columnIndex,
  pk: pkIndex,
});

const removePrimaryKey = pkIndex => ({
  type: REMOVE_PRIMARY_KEY,
  pk: pkIndex,
});

const resetPrimaryKeys = () => ({
  type: RESET_PRIMARY_KEY,
});

export const removeCheckConstraint = constraintName => (dispatch, getState) => {
  const confirmMessage = `This will permanently delete the check constraint "${constraintName}" from this table`;
  const isOk = getConfirmation(confirmMessage, true, constraintName);
  if (!isOk) return;

  const { currentTable: tableName, currentSchema } = getState().tables;

  const table = findTable(
    getState().tables.allSchemas,
    generateTableDef(tableName, currentSchema)
  );

  const constraint = findTableCheckConstraint(
    getTableCheckConstraints(table),
    constraintName
  );

  const upQuery = {
    type: 'run_sql',
    args: {
      sql: `alter table "${currentSchema}"."${tableName}" drop constraint "${constraintName}"`,
    },
  };
  const downQuery = {
    type: 'run_sql',
    args: {
      sql: `alter table "${currentSchema}"."${tableName}" add constraint "${constraintName}" ${
        constraint.check
      };`,
    },