Repository: hasura/graphql-engine
File: console/src/components/Common/utils/sqlUtils.ts (TypeScript)

Code before:
    return check.substring(7, check.length - 1);
  }

  return check;
};

/* queries */

export const getCreateCheckConstraintSql = (
  tableName: string,
  schemaName: string,
  constraintName: string,
  check: string
) => {
  return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`;
};

export const getDropConstraintSql = (
  tableName: string,
  schemaName: string,
  constraintName: string
) => {
  return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`;
};

export const getCreatePkSql = (options: SqlUtilsOptions) => {
  const { schemaName, tableName, selectedPkColumns, constraintName } = options;

  // if no primary key columns provided, return empty query
  if (!selectedPkColumns || selectedPkColumns.length === 0) {
    return '';
  }

  return `alter table "${schemaName}"."${tableName}"
    add constraint "${constraintName}"
    primary key ( ${selectedPkColumns
      .map((pkc: string) => `"${pkc}"`)
      .join(', ')} );`;
};

export const getDropPkSql = (options: SqlUtilsOptions) => {
  const { schemaName, tableName, constraintName } = options;

  return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`;
};

export const terminateSql = (sql: string) => {
  const sqlTerminated = sql.trim();

  return sqlTerminated[sqlTerminated.length - 1] !== ';'
    ? `${sqlTerminated};`

Review comment (suggestion):
Let's destruct it here:
```suggestion
export const getCreatePkSql = ({ schemaName, tableName, selectedPkColumns, constraintName }: SqlUtilsOptions) => {
```

The main reason is that name `options` is not self-explanatory enough, whereas `schemaName`, `tableName`, etc are. And with having it destructed I can hover the function and see what are the keys of the object.

<img width="908" alt="Screenshot 2020-04-23 at 11 59 17" src="https://user-images.githubusercontent.com/9019397/80086357-ddeb7280-8559-11ea-8a34-f07b94fe4e81.png">


Code after:

  return check;
};

/* queries */

export const getCreateCheckConstraintSql = (
  tableName: string,
  schemaName: string,
  constraintName: string,
  check: string
) => {
  return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`;
};

export const getDropConstraintSql = (
  tableName: string,
  schemaName: string,
  constraintName: string
) => {
  return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`;
};

export const getCreatePkSql = ({
  schemaName,
  tableName,
  selectedPkColumns,
  constraintName,
}: SqlUtilsOptions) => {
  // if no primary key columns provided, return empty query
  if (!selectedPkColumns || selectedPkColumns.length === 0) {
    return '';
  }

  return `alter table "${schemaName}"."${tableName}"
    add constraint "${constraintName}"
    primary key ( ${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')} );`;
};

export const getDropPkSql = ({
  schemaName,
  tableName,
  constraintName,
}: SqlUtilsOptions) => {
  return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`;
};

export const terminateSql = (sql: string) => {
  const sqlTerminated = sql.trim();

  return sqlTerminated[sqlTerminated.length - 1] !== ';'