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

Commit c933bed7 by Sushant Committed by GitHub

fix(postgres/createSchema): support for create schema if not exists w… (#8484)

1 parent 88622127
...@@ -17,6 +17,12 @@ const QueryGenerator = { ...@@ -17,6 +17,12 @@ const QueryGenerator = {
}, },
createSchema(schema) { createSchema(schema) {
const databaseVersion = _.get(this, 'sequelize.options.databaseVersion', 0);
if (databaseVersion && semver.gte(databaseVersion, '9.2.0')) {
return `CREATE SCHEMA IF NOT EXISTS ${schema};`;
}
return `CREATE SCHEMA ${schema};`; return `CREATE SCHEMA ${schema};`;
}, },
......
...@@ -34,10 +34,17 @@ if (dialect.match(/^postgres/)) { ...@@ -34,10 +34,17 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('errors if the schema exists', function() { it('works even when schema exists', function() {
return this.queryInterface.createSchema('testschema') return this.queryInterface.createSchema('testschema')
.catch(err => { .then(() => this.queryInterface.createSchema('testschema'))
expect(err.message).to.be.equal('schema "testschema" already exists'); .then(() => this.sequelize.query(`
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'testschema';
`, { type: this.sequelize.QueryTypes.SELECT }))
.then(res => {
expect(res, 'query results').to.not.be.empty;
expect(res[0].schema_name).to.be.equal('testschema');
}); });
}); });
}); });
......
'use strict'; 'use strict';
const chai = require('chai'), const chai = require('chai');
expect = chai.expect, const expect = chai.expect;
Support = require(__dirname + '/../support'), const Support = require(__dirname + '/../support');
DataTypes = require(__dirname + '/../../../lib/data-types'); const DataTypes = require(__dirname + '/../../../lib/data-types');
const SEARCH_PATH_ONE = 'schema_one,public'; const SEARCH_PATH_ONE = 'schema_one,public';
const SEARCH_PATH_TWO = 'schema_two,public'; const SEARCH_PATH_TWO = 'schema_two,public';
......
'use strict'; 'use strict';
const Support = require(__dirname + '/../support'), const Support = require(__dirname + '/../support');
expectsql = Support.expectsql, const expectsql = Support.expectsql;
current = Support.sequelize, const current = Support.sequelize;
sql = current.dialect.QueryGenerator; const sql = current.dialect.QueryGenerator;
describe(Support.getTestDialectTeaser('SQL'), () => { describe(Support.getTestDialectTeaser('SQL'), () => {
if (current.dialect.name === 'postgres') { if (current.dialect.name === 'postgres') {
describe('dropSchema', () => { describe('dropSchema', () => {
test('IF EXISTS', () => { it('IF EXISTS', () => {
expectsql(sql.dropSchema('foo'), { expectsql(sql.dropSchema('foo'), {
postgres: 'DROP SCHEMA IF EXISTS foo CASCADE;' postgres: 'DROP SCHEMA IF EXISTS foo CASCADE;'
}); });
}); });
}); });
describe('createSchema', () => {
before(function () {
this.version = current.options.databaseVersion;
});
after(function () {
current.options.databaseVersion = this.version;
});
it('9.2.0 or above', () => {
current.options.databaseVersion = '9.2.0';
expectsql(sql.createSchema('foo'), {
postgres: 'CREATE SCHEMA IF NOT EXISTS foo;'
});
});
it('below 9.2.0', () => {
current.options.databaseVersion = '9.0.0';
expectsql(sql.createSchema('foo'), {
postgres: 'CREATE SCHEMA foo;'
});
});
});
} }
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!