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

feat(PostgreSQL): table and field comments #872

Merged
merged 4 commits into from
Sep 28, 2024
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@
"contributions": [
"code"
]
},
{
"login": "zwei-c",
"name": "CHANG, CHIH WEI",
"avatar_url": "https://avatars.githubusercontent.com/u/55912811?v=4",
"profile": "https://github.com/zwei-c",
"contributions": [
"translation"
]
}
],
"contributorsPerLine": 7,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/bagusindrayana"><img src="https://avatars.githubusercontent.com/u/36830534?v=4?s=100" width="100px;" alt="Bagus Indrayana"/><br /><sub><b>Bagus Indrayana</b></sub></a><br /><a href="https://github.com/antares-sql/antares/commits?author=bagusindrayana" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/penguinlab"><img src="https://avatars.githubusercontent.com/u/10959317?v=4?s=100" width="100px;" alt="Naoki Ishikawa"/><br /><sub><b>Naoki Ishikawa</b></sub></a><br /><a href="#translation-penguinlab" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://fazevedo.dev"><img src="https://avatars.githubusercontent.com/u/1640325?v=4?s=100" width="100px;" alt="Filipe Azevedo"/><br /><sub><b>Filipe Azevedo</b></sub></a><br /><a href="https://github.com/antares-sql/antares/commits?author=mangas" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/zwei-c"><img src="https://avatars.githubusercontent.com/u/55912811?v=4?s=100" width="100px;" alt="CHANG, CHIH WEI"/><br /><sub><b>CHANG, CHIH WEI</b></sub></a><br /><a href="#translation-zwei-c" title="Translation">🌍</a></td>
</tr>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions src/common/customizations/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const customizations: Customizations = {
indexes: true,
foreigns: true,
nullable: true,
comment: true,
tableArray: true,
procedureSql: '$procedure$\r\n\r\n$procedure$',
procedureContext: true,
Expand Down
48 changes: 37 additions & 11 deletions src/main/libs/clients/PostgreSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,27 @@ export class PostgreSQLClient extends BaseClient {
column_default: string;
character_set_name: string;
collation_name: string;
column_comment: string;
}
/* eslint-enable camelcase */

const { rows } = await this
.select('*')
.schema('information_schema')
.from('columns')
.where({ table_schema: `= '${schema}'`, table_name: `= '${table}'` })
.orderBy({ ordinal_position: 'ASC' })
.run<TableColumnsResult>();
// Table columns
const { rows } = await this.raw<antares.QueryResult<TableColumnsResult>>(`
WITH comments AS (
SELECT attr.attname AS column, des.description AS comment, pgc.relname
FROM pg_attribute AS attr, pg_description AS des, pg_class AS pgc
WHERE pgc.oid = attr.attrelid
AND des.objoid = pgc.oid
AND pg_table_is_visible(pgc.oid)
AND attr.attnum = des.objsubid
)
SELECT cols.*, comments.comment AS column_comment
FROM "information_schema"."columns" AS cols
LEFT JOIN comments ON comments.column = cols.column_name AND comments.relname = cols.table_name
WHERE cols.table_schema = '${schema}'
AND cols.table_name = '${table}'
ORDER BY "ordinal_position" ASC
`);

return rows.map(field => {
let type = field.data_type;
Expand Down Expand Up @@ -532,7 +543,7 @@ export class PostgreSQLClient extends BaseClient {
collation: field.collation_name,
autoIncrement: false,
onUpdate: null,
comment: ''
comment: field.column_comment
};
});
}
Expand Down Expand Up @@ -869,6 +880,7 @@ export class PostgreSQLClient extends BaseClient {
const newIndexes: string[] = [];
const manageIndexes: string[] = [];
const newForeigns: string[] = [];
const modifyComment: string[] = [];

let sql = `CREATE TABLE "${schema}"."${options.name}"`;

Expand All @@ -884,6 +896,8 @@ export class PostgreSQLClient extends BaseClient {
${field.nullable ? 'NULL' : 'NOT NULL'}
${field.default !== null ? `DEFAULT ${field.default || '\'\''}` : ''}
${field.onUpdate ? `ON UPDATE ${field.onUpdate}` : ''}`);
if (field.comment != null)
modifyComment.push(`COMMENT ON COLUMN "${schema}"."${options.name}"."${field.name}" IS '${field.comment}'`);
});

// ADD INDEX
Expand All @@ -904,8 +918,12 @@ export class PostgreSQLClient extends BaseClient {
newForeigns.push(`CONSTRAINT "${foreign.constraintName}" FOREIGN KEY ("${foreign.field}") REFERENCES "${schema}"."${foreign.refTable}" ("${foreign.refField}") ON UPDATE ${foreign.onUpdate} ON DELETE ${foreign.onDelete}`);
});

sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')})`;
if (manageIndexes.length) sql = `${sql}; ${manageIndexes.join(';')}`;
sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')}); `;
if (manageIndexes.length) sql = `${sql} ${manageIndexes.join(';')}; `;
// TABLE COMMENT
if (options.comment) sql = `${sql} COMMENT ON TABLE "${schema}"."${options.name}" IS '${options.comment}'; `;
// FIELDS COMMENT
if (modifyComment.length) sql = `${sql} ${modifyComment.join(';')}; `;

return await this.raw(sql);
}
Expand All @@ -930,6 +948,7 @@ export class PostgreSQLClient extends BaseClient {
const renameColumns: string[] = [];
const createSequences: string[] = [];
const manageIndexes: string[] = [];
const modifyComment: string[] = [];

// ADD FIELDS
additions.forEach(addition => {
Expand All @@ -943,6 +962,8 @@ export class PostgreSQLClient extends BaseClient {
${addition.nullable ? 'NULL' : 'NOT NULL'}
${addition.default !== null ? `DEFAULT ${addition.default || '\'\''}` : ''}
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`);
if (addition.comment != null)
modifyComment.push(`COMMENT ON COLUMN "${schema}"."${table}"."${addition.name}" IS '${addition.comment}'`);
});

// ADD INDEX
Expand Down Expand Up @@ -995,6 +1016,8 @@ export class PostgreSQLClient extends BaseClient {

if (change.orgName !== change.name)
renameColumns.push(`ALTER TABLE "${schema}"."${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`);
if (change.comment != null)
modifyComment.push(`COMMENT ON COLUMN "${schema}"."${table}"."${change.name}" IS '${change.comment}'`);
});

// CHANGE INDEX
Expand Down Expand Up @@ -1042,8 +1065,11 @@ export class PostgreSQLClient extends BaseClient {
if (alterColumns.length) sql += `ALTER TABLE "${schema}"."${table}" ${alterColumns.join(', ')}; `;
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`;
// TABLE COMMENT
if (options.comment) sql = `${sql} COMMENT ON TABLE ${schema}.${table} IS '${options.comment}'; `;
// FIELDS COMMENT
if (modifyComment.length) sql = `${sql} ${modifyComment.join(';')}; `;
if (options.name) sql += `ALTER TABLE "${schema}"."${table}" RENAME TO "${options.name}"; `;

// RENAME
if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`;

Expand Down
Loading