Emergency fix: Prevention of accidental data loss during sync
Deploy TrassenPlaner / deploy (push) Waiting to run Details

This commit is contained in:
Johannes Baumeister 2026-04-20 10:20:13 +02:00
parent 1185a5b5e0
commit 6c6eb92dbb
2 changed files with 33 additions and 16 deletions

View File

@ -2731,10 +2731,12 @@
const indicator = document.getElementById('db-status-indicator');
if (indicator) indicator.classList.add('active');
try {
try {
// Ensure we have a valid variant object and points
const v = variantDef.id ? variantDef : state.variants.find(vx => vx.id === variantDef.id);
if (!v) {
if (!variantDef.id && !variantDef.name) return;
const v = variantDef.id ? variantDef : state.variants.find(vx => vx.id === variantDef.id || vx.name === variantDef.name);
if (!v || !v.routes || v.routes.length < 2) {
if (indicator) indicator.classList.remove('active');
return;
}

View File

@ -226,22 +226,37 @@ app.post('/api/variants', async (req, res) => {
const routeName = properties.name || 'Neue Trasse';
const varianteValue = properties.Variante || (properties.name ? properties.name.replace('Variante ', '') : 'A');
const geoJsonStr = JSON.stringify(geometry);
// Safety Check: DON'T delete/save if geometry is empty or invalid
if (!geometry || !geometry.coordinates || geometry.coordinates.length < 2) {
console.log(`Aborting save for ${varianteValue}: Geometry is too short or empty.`);
return res.json({ success: true, message: 'Skipped empty geometry' });
}
// Delete existing variant if it exists (Manual UPSERT to bypass missing index permissions)
await client.query('DELETE FROM kabeltrasse WHERE "Variante" = $1 OR name = $2', [varianteValue, routeName]);
try {
await client.query('BEGIN');
// Delete existing variant if it exists
await client.query('DELETE FROM kabeltrasse WHERE "Variante" = $1 OR name = $2', [varianteValue, routeName]);
const insertQuery = `
INSERT INTO kabeltrasse (geom, name, "Variante")
VALUES (
ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832)),
$2,
$3
)
RETURNING id
`;
const insertRes = await client.query(insertQuery, [geoJsonStr, routeName, varianteValue]);
res.json({ success: true, id: insertRes.rows[0].id });
const insertQuery = `
INSERT INTO kabeltrasse (geom, name, "Variante")
VALUES (
ST_Multi(ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832))),
$2,
$3
)
RETURNING id
`;
const insertRes = await client.query(insertQuery, [geoJsonStr, routeName, varianteValue]);
await client.query('COMMIT');
res.json({ success: true, id: insertRes.rows[0].id });
} catch (sqlErr) {
await client.query('ROLLBACK');
throw sqlErr;
}
} catch (err) {
console.error("SQL-FEHLER (SAVE):", err.message);
res.status(500).json({ error: 'Failed to save variant' });