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

Use existing transaction if provided instead of creating a new one #29

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 96 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,109 @@ export default (args) => {
enumName = `enum_${tableName}_${columnName}`
} = args;

const newEnumName = `${enumName}_new`;

return queryInterface.sequelize.transaction(sequelizeOptions, (t) => {
const newSequelizeOptions = {
...(sequelizeOptions || {}),
transaction: t
};

// Create a copy of the type
return createEnum({
if (sequelizeOptions.transaction) {
return replaceEnum({
tableName,
columnName,
defaultValue,
newValues,
queryInterface,
name: newEnumName,
values: newValues,
sequelizeOptions: newSequelizeOptions
})
// Drop default value (ALTER COLUMN cannot cast default values)
.then(() => defaultValue && unsetDefaultValueFromEnum({
queryInterface,
tableName,
columnName,
sequelizeOptions: newSequelizeOptions
}))
// Change column type to the new ENUM TYPE
.then(() => setColumnTypeToEnum({
tableName,
columnName,
enumName: newEnumName,
queryInterface,
sequelizeOptions: newSequelizeOptions
}))
// Drop old ENUM
.then(() => dropEnum(
{
name: enumName,
sequelizeOptions: newSequelizeOptions,
queryInterface
}
))
// Rename new ENUM name
.then(() => renameEnum({
oldEnumName: newEnumName,
newEnumName: enumName,
queryInterface,
sequelizeOptions: newSequelizeOptions
}))
.then(() => defaultValue && setColumnDefault({
newSequelizeOptions: sequelizeOptions,
enumName
});
} else {
return queryInterface.sequelize.transaction(sequelizeOptions, (t) => {
const newSequelizeOptions = {
...(sequelizeOptions || {}),
transaction: t
};
return replaceEnum({
tableName,
columnName,
defaultValue,
defaultValueType: enumName,
newValues,
queryInterface,
sequelizeOptions: newSequelizeOptions
}));
});
newSequelizeOptions,
enumName
});
});
}
};

/**
* Implementation of enum replacement.
*
* @param {Object} args
* @param {String} args.tableName
* @param {String} args.columnName
* @param {String} args.defaultValue
* @param {Array} args.newValues
* @param {Object} args.queryInterface
* @param {Object} args.newSequelizeOptions
* @param {String} args.enumName
*
* @return {Promise}
*/
function replaceEnum(args) {
const {
tableName,
columnName,
defaultValue,
newValues,
queryInterface,
newSequelizeOptions,
enumName
} = args;

const newEnumName = `${enumName}_new`;

// Create a copy of the type
return createEnum({
queryInterface,
name: newEnumName,
values: newValues,
sequelizeOptions: newSequelizeOptions
})
// Drop default value (ALTER COLUMN cannot cast default values)
.then(() => defaultValue && unsetDefaultValueFromEnum({
queryInterface,
tableName,
columnName,
sequelizeOptions: newSequelizeOptions
}))
// Change column type to the new ENUM TYPE
.then(() => setColumnTypeToEnum({
tableName,
columnName,
enumName: newEnumName,
queryInterface,
sequelizeOptions: newSequelizeOptions
}))
// Drop old ENUM
.then(() => dropEnum(
{
name: enumName,
sequelizeOptions: newSequelizeOptions,
queryInterface
}
))
// Rename new ENUM name
.then(() => renameEnum({
oldEnumName: newEnumName,
newEnumName: enumName,
queryInterface,
sequelizeOptions: newSequelizeOptions
}))
.then(() => defaultValue && setColumnDefault({
tableName,
columnName,
defaultValue,
defaultValueType: enumName,
queryInterface,
sequelizeOptions: newSequelizeOptions
}));
}

/**
* Create a new ENUM.
*
Expand Down