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

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 = {
},
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};`;
},
......
......@@ -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')
.catch(err => {
expect(err.message).to.be.equal('schema "testschema" already exists');
.then(() => this.queryInterface.createSchema('testschema'))
.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';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types');
const chai = require('chai');
const expect = chai.expect;
const Support = require(__dirname + '/../support');
const DataTypes = require(__dirname + '/../../../lib/data-types');
const SEARCH_PATH_ONE = 'schema_one,public';
const SEARCH_PATH_TWO = 'schema_two,public';
......
'use strict';
const Support = require(__dirname + '/../support'),
expectsql = Support.expectsql,
current = Support.sequelize,
sql = current.dialect.QueryGenerator;
const Support = require(__dirname + '/../support');
const expectsql = Support.expectsql;
const current = Support.sequelize;
const sql = current.dialect.QueryGenerator;
describe(Support.getTestDialectTeaser('SQL'), () => {
if (current.dialect.name === 'postgres') {
describe('dropSchema', () => {
test('IF EXISTS', () => {
it('IF EXISTS', () => {
expectsql(sql.dropSchema('foo'), {
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!