mirror of
https://github.com/Fabio286/antares.git
synced 2025-06-05 21:59:22 +02:00
Merge pull request #872 from mirrorb/master
feat(PostgreSQL): table and field comments
This commit is contained in:
@ -293,6 +293,15 @@
|
|||||||
"contributions": [
|
"contributions": [
|
||||||
"code"
|
"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,
|
"contributorsPerLine": 7,
|
||||||
|
@ -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/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://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://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>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
@ -62,6 +62,7 @@ export const customizations: Customizations = {
|
|||||||
indexes: true,
|
indexes: true,
|
||||||
foreigns: true,
|
foreigns: true,
|
||||||
nullable: true,
|
nullable: true,
|
||||||
|
comment: true,
|
||||||
tableArray: true,
|
tableArray: true,
|
||||||
procedureSql: '$procedure$\r\n\r\n$procedure$',
|
procedureSql: '$procedure$\r\n\r\n$procedure$',
|
||||||
procedureContext: true,
|
procedureContext: true,
|
||||||
|
@ -498,16 +498,27 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
column_default: string;
|
column_default: string;
|
||||||
character_set_name: string;
|
character_set_name: string;
|
||||||
collation_name: string;
|
collation_name: string;
|
||||||
|
column_comment: string;
|
||||||
}
|
}
|
||||||
/* eslint-enable camelcase */
|
/* eslint-enable camelcase */
|
||||||
|
|
||||||
const { rows } = await this
|
// Table columns
|
||||||
.select('*')
|
const { rows } = await this.raw<antares.QueryResult<TableColumnsResult>>(`
|
||||||
.schema('information_schema')
|
WITH comments AS (
|
||||||
.from('columns')
|
SELECT attr.attname AS column, des.description AS comment, pgc.relname
|
||||||
.where({ table_schema: `= '${schema}'`, table_name: `= '${table}'` })
|
FROM pg_attribute AS attr, pg_description AS des, pg_class AS pgc
|
||||||
.orderBy({ ordinal_position: 'ASC' })
|
WHERE pgc.oid = attr.attrelid
|
||||||
.run<TableColumnsResult>();
|
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 => {
|
return rows.map(field => {
|
||||||
let type = field.data_type;
|
let type = field.data_type;
|
||||||
@ -536,7 +547,7 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
collation: field.collation_name,
|
collation: field.collation_name,
|
||||||
autoIncrement: false,
|
autoIncrement: false,
|
||||||
onUpdate: null,
|
onUpdate: null,
|
||||||
comment: ''
|
comment: field.column_comment
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -873,6 +884,7 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
const newIndexes: string[] = [];
|
const newIndexes: string[] = [];
|
||||||
const manageIndexes: string[] = [];
|
const manageIndexes: string[] = [];
|
||||||
const newForeigns: string[] = [];
|
const newForeigns: string[] = [];
|
||||||
|
const modifyComment: string[] = [];
|
||||||
|
|
||||||
let sql = `CREATE TABLE "${schema}"."${options.name}"`;
|
let sql = `CREATE TABLE "${schema}"."${options.name}"`;
|
||||||
|
|
||||||
@ -888,6 +900,8 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
${field.nullable ? 'NULL' : 'NOT NULL'}
|
${field.nullable ? 'NULL' : 'NOT NULL'}
|
||||||
${field.default !== null ? `DEFAULT ${field.default || '\'\''}` : ''}
|
${field.default !== null ? `DEFAULT ${field.default || '\'\''}` : ''}
|
||||||
${field.onUpdate ? `ON UPDATE ${field.onUpdate}` : ''}`);
|
${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
|
// ADD INDEX
|
||||||
@ -908,8 +922,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}`);
|
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(', ')})`;
|
sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')}); `;
|
||||||
if (manageIndexes.length) sql = `${sql}; ${manageIndexes.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);
|
return await this.raw(sql);
|
||||||
}
|
}
|
||||||
@ -934,6 +952,7 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
const renameColumns: string[] = [];
|
const renameColumns: string[] = [];
|
||||||
const createSequences: string[] = [];
|
const createSequences: string[] = [];
|
||||||
const manageIndexes: string[] = [];
|
const manageIndexes: string[] = [];
|
||||||
|
const modifyComment: string[] = [];
|
||||||
|
|
||||||
// ADD FIELDS
|
// ADD FIELDS
|
||||||
additions.forEach(addition => {
|
additions.forEach(addition => {
|
||||||
@ -947,6 +966,8 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
${addition.nullable ? 'NULL' : 'NOT NULL'}
|
${addition.nullable ? 'NULL' : 'NOT NULL'}
|
||||||
${addition.default !== null ? `DEFAULT ${addition.default || '\'\''}` : ''}
|
${addition.default !== null ? `DEFAULT ${addition.default || '\'\''}` : ''}
|
||||||
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`);
|
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`);
|
||||||
|
if (addition.comment != null)
|
||||||
|
modifyComment.push(`COMMENT ON COLUMN "${schema}"."${table}"."${addition.name}" IS '${addition.comment}'`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// ADD INDEX
|
// ADD INDEX
|
||||||
@ -999,6 +1020,8 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
|
|
||||||
if (change.orgName !== change.name)
|
if (change.orgName !== change.name)
|
||||||
renameColumns.push(`ALTER TABLE "${schema}"."${table}" RENAME COLUMN "${change.orgName}" TO "${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
|
// CHANGE INDEX
|
||||||
@ -1046,8 +1069,11 @@ export class PostgreSQLClient extends BaseClient {
|
|||||||
if (alterColumns.length) sql += `ALTER TABLE "${schema}"."${table}" ${alterColumns.join(', ')}; `;
|
if (alterColumns.length) sql += `ALTER TABLE "${schema}"."${table}" ${alterColumns.join(', ')}; `;
|
||||||
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
|
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
|
||||||
if (manageIndexes.length) sql = `${manageIndexes.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}"; `;
|
if (options.name) sql += `ALTER TABLE "${schema}"."${table}" RENAME TO "${options.name}"; `;
|
||||||
|
|
||||||
// RENAME
|
// RENAME
|
||||||
if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`;
|
if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user