From a6b3c696c38ccb550c1b84a13c94f8244bbec64b Mon Sep 17 00:00:00 2001 From: yumin2002 <94658575+yumin2002@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:08:45 -0400 Subject: [PATCH] feat: added automatic schema creation to sequelize.sync() and model.create() --- packages/core/src/model.js | 7 ++++++ packages/core/src/sequelize.js | 7 ++++++ .../integration/model/create/include.test.js | 24 +++++++++++++++++++ .../core/test/integration/model/sync.test.js | 8 +++++++ 4 files changed, 46 insertions(+) diff --git a/packages/core/src/model.js b/packages/core/src/model.js index d333d20916b5..9047ec2068df 100644 --- a/packages/core/src/model.js +++ b/packages/core/src/model.js @@ -1916,6 +1916,13 @@ ${associationOwner._getAssociationDebugList()}`); static async create(values, options) { options = cloneDeep(options) ?? {}; + if (options.schema) { + const schemas = await this.queryInterface.listSchemas(); + if (!schemas.includes(options.schema)) { + await this.queryInterface.createSchema(options.schema); + } + } + return await this.build(values, { isNewRecord: true, attributes: options.fields, diff --git a/packages/core/src/sequelize.js b/packages/core/src/sequelize.js index ef2ea2756319..4fc194589b0a 100644 --- a/packages/core/src/sequelize.js +++ b/packages/core/src/sequelize.js @@ -456,6 +456,13 @@ Use Sequelize#query if you wish to use replacements.`); ); } + if (options.schema) { + const schemas = await this.queryInterface.listSchemas(); + if (!schemas.includes(options.schema)) { + await this.queryInterface.createSchema(options.schema); + } + } + if (options.hooks) { await this.hooks.runAsync('beforeBulkSync', options); } diff --git a/packages/core/test/integration/model/create/include.test.js b/packages/core/test/integration/model/create/include.test.js index 41c0dd9805f2..26a4bc16311b 100644 --- a/packages/core/test/integration/model/create/include.test.js +++ b/packages/core/test/integration/model/create/include.test.js @@ -533,6 +533,30 @@ describe(Support.getTestDialectTeaser('Model'), () => { expect(persistedUser.jobs).to.be.ok; expect(persistedUser.jobs.length).to.equal(2); }); + it('should create a schema if the schema does not exist when create', async function () { + try { + const before = await this.sequelize.queryInterface.listSchemas(); + const User = this.sequelize.define('User', { + username: DataTypes.STRING, + }); + await this.sequelize.sync({ force: true }); + await User.create( + { + username: 'John', + }, + { + schema: 'temp', + }, + ); + const after = await this.sequelize.queryInterface.listSchemas(); + const difference = after.length - before.length; + expect(difference).to.equal(1); + } catch (error) { + if (!error.message.includes('Schemas are not supported in')) { + throw error; + } + } + }); }); }); }); diff --git a/packages/core/test/integration/model/sync.test.js b/packages/core/test/integration/model/sync.test.js index ed41da98869f..7515cfee6f73 100644 --- a/packages/core/test/integration/model/sync.test.js +++ b/packages/core/test/integration/model/sync.test.js @@ -687,6 +687,14 @@ describe(getTestDialectTeaser('Model.sync & Sequelize#sync'), () => { }); expect(results).to.have.length(1); }); + + it('should create a schema if the schema does not exist when syncing', async () => { + const before = await this.sequelize.queryInterface.listSchemas(); + await sequelize.sync({ schema: 'temp' }); + const after = await this.sequelize.queryInterface.listSchemas(); + const difference = after.length - before.length; + expect(difference).to.equal(1); + }); } // TODO add support for db2 and mssql dialects