98 lines
3.0 KiB
JavaScript
98 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const businessColumns = {
|
|
shopify_store_reference: { type: 'TEXT' },
|
|
shopify_connected: { type: 'BOOLEAN', defaultValue: false, allowNull: false },
|
|
shopify_connected_at: { type: 'DATE' },
|
|
shopify_webhook_token: { type: 'TEXT' },
|
|
woocommerce_store_reference: { type: 'TEXT' },
|
|
woocommerce_connected: { type: 'BOOLEAN', defaultValue: false, allowNull: false },
|
|
woocommerce_connected_at: { type: 'DATE' },
|
|
woocommerce_webhook_token: { type: 'TEXT' },
|
|
};
|
|
|
|
const customerColumns = {
|
|
shopify_customer_reference: { type: 'TEXT' },
|
|
woocommerce_customer_reference: { type: 'TEXT' },
|
|
};
|
|
|
|
const transactionColumns = {
|
|
shopify_order_reference: { type: 'TEXT' },
|
|
woocommerce_order_reference: { type: 'TEXT' },
|
|
};
|
|
|
|
function normalizeColumnDefinition(Sequelize, definition) {
|
|
const normalized = { ...definition };
|
|
|
|
if (definition.type === 'TEXT') {
|
|
normalized.type = Sequelize.DataTypes.TEXT;
|
|
}
|
|
|
|
if (definition.type === 'BOOLEAN') {
|
|
normalized.type = Sequelize.DataTypes.BOOLEAN;
|
|
}
|
|
|
|
if (definition.type === 'DATE') {
|
|
normalized.type = Sequelize.DataTypes.DATE;
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
|
|
async function addColumnsIfMissing(queryInterface, Sequelize, transaction, tableName, columns) {
|
|
const table = await queryInterface.describeTable(tableName);
|
|
|
|
for (const [columnName, definition] of Object.entries(columns)) {
|
|
if (!table[columnName]) {
|
|
await queryInterface.addColumn(
|
|
tableName,
|
|
columnName,
|
|
normalizeColumnDefinition(Sequelize, definition),
|
|
{ transaction },
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function removeColumnsIfPresent(queryInterface, transaction, tableName, columns) {
|
|
const table = await queryInterface.describeTable(tableName);
|
|
|
|
for (const columnName of Object.keys(columns).reverse()) {
|
|
if (table[columnName]) {
|
|
await queryInterface.removeColumn(tableName, columnName, { transaction });
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
async up(queryInterface, Sequelize) {
|
|
const transaction = await queryInterface.sequelize.transaction();
|
|
|
|
try {
|
|
await addColumnsIfMissing(queryInterface, Sequelize, transaction, 'businesses', businessColumns);
|
|
await addColumnsIfMissing(queryInterface, Sequelize, transaction, 'customers', customerColumns);
|
|
await addColumnsIfMissing(queryInterface, Sequelize, transaction, 'transactions', transactionColumns);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
async down(queryInterface) {
|
|
const transaction = await queryInterface.sequelize.transaction();
|
|
|
|
try {
|
|
await removeColumnsIfPresent(queryInterface, transaction, 'transactions', transactionColumns);
|
|
await removeColumnsIfPresent(queryInterface, transaction, 'customers', customerColumns);
|
|
await removeColumnsIfPresent(queryInterface, transaction, 'businesses', businessColumns);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
},
|
|
};
|