不要怂,就是干,撸起袖子干!

Commit 56208b2c by mannol Committed by Sushant

feat(query-interface): add force option to createFunction (#11172)

1 parent 003aabcc
......@@ -600,13 +600,15 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return `ALTER TRIGGER ${this.quoteIdentifier(oldTriggerName)} ON ${this.quoteTable(tableName)} RENAME TO ${this.quoteIdentifier(newTriggerName)};`;
}
createFunction(functionName, params, returnType, language, body, options) {
createFunction(functionName, params, returnType, language, body, optionsArray, options) {
if (!functionName || !returnType || !language || !body) throw new Error('createFunction missing some parameters. Did you pass functionName, returnType, language and body?');
const paramList = this.expandFunctionParamList(params);
const expandedOptions = this.expandOptions(options);
const expandedOptionsArray = this.expandOptions(optionsArray);
return `CREATE FUNCTION ${functionName}(${paramList}) RETURNS ${returnType} AS $func$ BEGIN ${body} END; $func$ language '${language}'${expandedOptions};`;
const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION';
return `${statement} ${functionName}(${paramList}) RETURNS ${returnType} AS $func$ BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;
}
dropFunction(functionName, params) {
......
......@@ -1243,11 +1243,12 @@ class QueryInterface {
* @param {string} body Source code of function
* @param {Array} optionsArray Extra-options for creation
* @param {Object} [options] query options
* @param {boolean} options.force If force is true, any existing functions with the same parameters will be replaced. For postgres, this means using `CREATE OR REPLACE FUNCTION` instead of `CREATE FUNCTION`. Default is false
*
* @returns {Promise}
*/
createFunction(functionName, params, returnType, language, body, optionsArray, options) {
const sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, optionsArray);
const sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, optionsArray, options);
options = options || {};
if (sql) {
......
......@@ -158,6 +158,21 @@ if (dialect.match(/^postgres/)) {
}).to.throw(/createFunction missing some parameters. Did you pass functionName, returnType, language and body/)
]);
});
it('overrides a function', function() {
const first_body = 'return \'first\';';
const second_body = 'return \'second\';';
// create function
return this.queryInterface.createFunction('my_func', [], 'varchar', 'plpgsql', first_body, null)
// override
.then(() => this.queryInterface.createFunction('my_func', [], 'varchar', 'plpgsql', second_body, null, { force: true }))
// validate
.then(() => this.sequelize.query('select my_func();', { type: this.sequelize.QueryTypes.SELECT }))
.then(res => {
expect(res[0].my_func).to.be.eql('second');
});
});
});
describe('dropFunction', () => {
......
......@@ -83,6 +83,10 @@ export interface QueryOptionsWithType<T extends QueryTypes> extends QueryOptions
type: T;
}
export interface QueryOptionsWithForce extends QueryOptions {
force?: boolean;
}
/**
* Most of the methods accept options and use only the logger property of the options. That's why the most used
* interface type for options in a method is separated here as another interface.
......@@ -553,7 +557,8 @@ export class QueryInterface {
returnType: string,
language: string,
body: string,
options?: QueryOptions
optionsArray?: string[],
options?: QueryOptionsWithForce
): Promise<void>;
/**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!