Refactor stock cleanup logic in syncDatabase functions to remove orphaned stock entries with invalid branch_id and streamline logging for orphaned entries.
This commit is contained in:
@@ -6,8 +6,7 @@ class FalukantStock extends Model { }
|
|||||||
FalukantStock.init({
|
FalukantStock.init({
|
||||||
branchId: {
|
branchId: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
allowNull: false,
|
allowNull: false
|
||||||
defaultValue: 0
|
|
||||||
},
|
},
|
||||||
stockTypeId: {
|
stockTypeId: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
|
|||||||
@@ -54,24 +54,39 @@ const syncDatabase = async () => {
|
|||||||
console.warn('⚠️ Konnte traffic_light-Spalte nicht vorab sicherstellen:', e?.message || e);
|
console.warn('⚠️ Konnte traffic_light-Spalte nicht vorab sicherstellen:', e?.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup: Entferne verwaiste user_param_visibility Einträge vor Schema-Updates (nur wenn Schema-Updates aktiviert)
|
// Cleanup: Entferne verwaiste Einträge vor Schema-Updates (nur wenn Schema-Updates aktiviert)
|
||||||
if (currentStage === 'dev') {
|
if (currentStage === 'dev') {
|
||||||
console.log("Cleaning up orphaned user_param_visibility entries...");
|
console.log("Cleaning up orphaned entries...");
|
||||||
try {
|
try {
|
||||||
const result = await sequelize.query(`
|
// Cleanup user_param_visibility
|
||||||
|
const result1 = await sequelize.query(`
|
||||||
DELETE FROM community.user_param_visibility
|
DELETE FROM community.user_param_visibility
|
||||||
WHERE param_id NOT IN (
|
WHERE param_id NOT IN (
|
||||||
SELECT id FROM community.user_param
|
SELECT id FROM community.user_param
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
const deletedCount = result[1] || 0;
|
const deletedCount1 = result1[1] || 0;
|
||||||
if (deletedCount > 0) {
|
if (deletedCount1 > 0) {
|
||||||
console.log(`✅ ${deletedCount} verwaiste user_param_visibility Einträge entfernt`);
|
console.log(`✅ ${deletedCount1} verwaiste user_param_visibility Einträge entfernt`);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
// Cleanup stock mit ungültigen branch_id (0 oder nicht existierend)
|
||||||
|
const result2 = await sequelize.query(`
|
||||||
|
DELETE FROM falukant_data.stock
|
||||||
|
WHERE branch_id = 0 OR branch_id NOT IN (
|
||||||
|
SELECT id FROM falukant_data.branch
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
const deletedCount2 = result2[1] || 0;
|
||||||
|
if (deletedCount2 > 0) {
|
||||||
|
console.log(`✅ ${deletedCount2} verwaiste stock Einträge entfernt`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deletedCount1 === 0 && deletedCount2 === 0) {
|
||||||
console.log("✅ Keine verwaisten Einträge gefunden");
|
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('⚠️ Konnte verwaiste user_param_visibility Einträge nicht bereinigen:', e?.message || e);
|
console.warn('⚠️ Konnte verwaiste Einträge nicht bereinigen:', e?.message || e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,23 +208,38 @@ const syncDatabaseForDeployment = async () => {
|
|||||||
console.warn('⚠️ Konnte Transport-Spalten nicht nullable machen:', e?.message || e);
|
console.warn('⚠️ Konnte Transport-Spalten nicht nullable machen:', e?.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup: Entferne verwaiste user_param_visibility Einträge vor Schema-Updates
|
// Cleanup: Entferne verwaiste Einträge vor Schema-Updates
|
||||||
console.log("Cleaning up orphaned user_param_visibility entries...");
|
console.log("Cleaning up orphaned entries...");
|
||||||
try {
|
try {
|
||||||
const result = await sequelize.query(`
|
// Cleanup user_param_visibility
|
||||||
|
const result1 = await sequelize.query(`
|
||||||
DELETE FROM community.user_param_visibility
|
DELETE FROM community.user_param_visibility
|
||||||
WHERE param_id NOT IN (
|
WHERE param_id NOT IN (
|
||||||
SELECT id FROM community.user_param
|
SELECT id FROM community.user_param
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
const deletedCount = result[1] || 0;
|
const deletedCount1 = result1[1] || 0;
|
||||||
if (deletedCount > 0) {
|
if (deletedCount1 > 0) {
|
||||||
console.log(`✅ ${deletedCount} verwaiste user_param_visibility Einträge entfernt`);
|
console.log(`✅ ${deletedCount1} verwaiste user_param_visibility Einträge entfernt`);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
// Cleanup stock mit ungültigen branch_id (0 oder nicht existierend)
|
||||||
|
const result2 = await sequelize.query(`
|
||||||
|
DELETE FROM falukant_data.stock
|
||||||
|
WHERE branch_id = 0 OR branch_id NOT IN (
|
||||||
|
SELECT id FROM falukant_data.branch
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
const deletedCount2 = result2[1] || 0;
|
||||||
|
if (deletedCount2 > 0) {
|
||||||
|
console.log(`✅ ${deletedCount2} verwaiste stock Einträge entfernt`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deletedCount1 === 0 && deletedCount2 === 0) {
|
||||||
console.log("✅ Keine verwaisten Einträge gefunden");
|
console.log("✅ Keine verwaisten Einträge gefunden");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('⚠️ Konnte verwaiste user_param_visibility Einträge nicht bereinigen:', e?.message || e);
|
console.warn('⚠️ Konnte verwaiste Einträge nicht bereinigen:', e?.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Setting up associations...");
|
console.log("Setting up associations...");
|
||||||
|
|||||||
Reference in New Issue
Block a user