mirror of
https://github.com/Fabio286/antares.git
synced 2025-06-05 21:59:22 +02:00
feat(MySQL): check constraints management support
This commit is contained in:
@@ -161,6 +161,8 @@ export class MySQLClient extends BaseClient {
|
||||
|
||||
this._ssh = new SSH2Promise({
|
||||
...this._params.ssh,
|
||||
reconnect: true,
|
||||
reconnectTries: 3,
|
||||
debug: process.env.NODE_ENV !== 'production' ? (s) => console.log(s) : null
|
||||
});
|
||||
|
||||
@@ -689,6 +691,34 @@ export class MySQLClient extends BaseClient {
|
||||
return rows.length ? rows[0].count : 0;
|
||||
}
|
||||
|
||||
async getTableChecks ({ schema, table }: { schema: string; table: string }): Promise<antares.TableCheck[]> {
|
||||
const { rows } = await this.raw(`
|
||||
SELECT
|
||||
CONSTRAINT_NAME as name,
|
||||
CHECK_CLAUSE as clausole
|
||||
FROM information_schema.CHECK_CONSTRAINTS
|
||||
WHERE CONSTRAINT_SCHEMA = "${schema}"
|
||||
AND CONSTRAINT_NAME IN (
|
||||
SELECT
|
||||
CONSTRAINT_NAME
|
||||
FROM
|
||||
information_schema.TABLE_CONSTRAINTS
|
||||
WHERE
|
||||
TABLE_SCHEMA = "${schema}"
|
||||
AND TABLE_NAME = "${table}"
|
||||
AND CONSTRAINT_TYPE = 'CHECK'
|
||||
)
|
||||
`);
|
||||
|
||||
if (rows.length) {
|
||||
return rows.map(row => ({
|
||||
name: row.name,
|
||||
clause: row.clausole
|
||||
}));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
async getTableOptions ({ schema, table }: { schema: string; table: string }) {
|
||||
/* eslint-disable camelcase */
|
||||
interface TableOptionsResult {
|
||||
@@ -865,11 +895,13 @@ export class MySQLClient extends BaseClient {
|
||||
fields,
|
||||
foreigns,
|
||||
indexes,
|
||||
checks,
|
||||
options
|
||||
} = params;
|
||||
const newColumns: string[] = [];
|
||||
const newIndexes: string[] = [];
|
||||
const newForeigns: string[] = [];
|
||||
const newChecks: string[] = [];
|
||||
|
||||
let sql = `CREATE TABLE \`${schema}\`.\`${options.name}\``;
|
||||
|
||||
@@ -910,7 +942,13 @@ export class MySQLClient extends BaseClient {
|
||||
newForeigns.push(`CONSTRAINT \`${foreign.constraintName}\` FOREIGN KEY (\`${foreign.field}\`) REFERENCES \`${foreign.refTable}\` (\`${foreign.refField}\`) ON UPDATE ${foreign.onUpdate} ON DELETE ${foreign.onDelete}`);
|
||||
});
|
||||
|
||||
sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')}) COMMENT='${options.comment}', COLLATE='${options.collation}', ENGINE=${options.engine}`;
|
||||
// ADD TABLE CHECKS
|
||||
checks.forEach(check => {
|
||||
if (!check.clause.trim().length) return;
|
||||
newChecks.push(`${check.name ? `CONSTRAINT \`${check.name}\` ` : ''}CHECK (${check.clause})`);
|
||||
});
|
||||
|
||||
sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns, ...newChecks].join(', ')}) COMMENT='${options.comment}', COLLATE='${options.collation}', ENGINE=${options.engine}`;
|
||||
|
||||
return await this.raw(sql);
|
||||
}
|
||||
@@ -924,6 +962,7 @@ export class MySQLClient extends BaseClient {
|
||||
changes,
|
||||
indexChanges,
|
||||
foreignChanges,
|
||||
checkChanges,
|
||||
options
|
||||
} = params;
|
||||
|
||||
@@ -931,6 +970,7 @@ export class MySQLClient extends BaseClient {
|
||||
const alterColumnsAdd: string[] = [];
|
||||
const alterColumnsChange: string[] = [];
|
||||
const alterColumnsDrop: string[] = [];
|
||||
const alterQueryes: string[] = [];
|
||||
|
||||
// OPTIONS
|
||||
if ('comment' in options) alterColumnsChange.push(`COMMENT='${options.comment}'`);
|
||||
@@ -976,6 +1016,12 @@ export class MySQLClient extends BaseClient {
|
||||
alterColumnsAdd.push(`ADD CONSTRAINT \`${addition.constraintName}\` FOREIGN KEY (\`${addition.field}\`) REFERENCES \`${addition.refTable}\` (\`${addition.refField}\`) ON UPDATE ${addition.onUpdate} ON DELETE ${addition.onDelete}`);
|
||||
});
|
||||
|
||||
// ADD TABLE CHECKS
|
||||
checkChanges.additions.forEach(addition => {
|
||||
if (!addition.clause.trim().length) return;
|
||||
alterColumnsAdd.push(`ADD ${addition.name ? `CONSTRAINT \`${addition.name}\` ` : ''}CHECK (${addition.clause})`);
|
||||
});
|
||||
|
||||
// CHANGE FIELDS
|
||||
changes.forEach(change => {
|
||||
const typeInfo = this.getTypeInfo(change.type);
|
||||
@@ -987,9 +1033,9 @@ export class MySQLClient extends BaseClient {
|
||||
${change.zerofill ? 'ZEROFILL' : ''}
|
||||
${change.nullable ? 'NULL' : 'NOT NULL'}
|
||||
${change.autoIncrement ? 'AUTO_INCREMENT' : ''}
|
||||
${change.collation ? `COLLATE ${change.collation}` : ''}
|
||||
${change.default !== null ? `DEFAULT ${change.default || '\'\''}` : ''}
|
||||
${change.comment ? `COMMENT '${change.comment}'` : ''}
|
||||
${change.collation ? `COLLATE ${change.collation}` : ''}
|
||||
${change.onUpdate ? `ON UPDATE ${change.onUpdate}` : ''}
|
||||
${change.after ? `AFTER \`${change.after}\`` : 'FIRST'}`);
|
||||
});
|
||||
@@ -1020,6 +1066,13 @@ export class MySQLClient extends BaseClient {
|
||||
alterColumnsChange.push(`ADD CONSTRAINT \`${change.constraintName}\` FOREIGN KEY (\`${change.field}\`) REFERENCES \`${change.refTable}\` (\`${change.refField}\`) ON UPDATE ${change.onUpdate} ON DELETE ${change.onDelete}`);
|
||||
});
|
||||
|
||||
// CHANGE CHECK TABLE
|
||||
checkChanges.changes.forEach(change => {
|
||||
if (!change.clause.trim().length) return;
|
||||
alterQueryes.push(`${sql} DROP CONSTRAINT \`${change.name}\``);
|
||||
alterQueryes.push(`${sql} ADD ${change.name ? `CONSTRAINT \`${change.name}\` ` : ''}CHECK (${change.clause})`);
|
||||
});
|
||||
|
||||
// DROP FIELDS
|
||||
deletions.forEach(deletion => {
|
||||
alterColumnsDrop.push(`DROP COLUMN \`${deletion.name}\``);
|
||||
@@ -1038,7 +1091,11 @@ export class MySQLClient extends BaseClient {
|
||||
alterColumnsDrop.push(`DROP FOREIGN KEY \`${deletion.constraintName}\``);
|
||||
});
|
||||
|
||||
const alterQueryes = [];
|
||||
// DROP CHECK TABLE
|
||||
checkChanges.deletions.forEach(deletion => {
|
||||
alterQueryes.push(`${sql} DROP CONSTRAINT \`${deletion.name}\``);
|
||||
});
|
||||
|
||||
if (alterColumnsAdd.length) alterQueryes.push(sql+alterColumnsAdd.join(', '));
|
||||
if (alterColumnsChange.length) alterQueryes.push(sql+alterColumnsChange.join(', '));
|
||||
if (alterColumnsDrop.length) alterQueryes.push(sql+alterColumnsDrop.join(', '));
|
||||
|
Reference in New Issue
Block a user