Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow sequelize.sync() to create schema automatically if not exist #17296

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/model.js
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/sequelize.js
Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/integration/model/create/include.test.js
Expand Up @@ -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;
}
}
});
});
});
});
8 changes: 8 additions & 0 deletions packages/core/test/integration/model/sync.test.js
Expand Up @@ -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 sequelize.queryInterface.listSchemas();
await sequelize.sync({ schema: 'temp' });
const after = await sequelize.queryInterface.listSchemas();
const difference = after.length - before.length;
expect(difference).to.equal(1);
});
}

// TODO add support for db2 and mssql dialects
Expand Down