Change default sequence assignment for planned lines.

If there are other lines in the planner, we increment the
highest numbered sequence in the planner by one.

If there are no planned lines, we take the highest numbered
raw sequence and increment by one.
This commit is contained in:
D. Berge
2020-10-15 19:05:14 +02:00
parent e0cd52f21a
commit 80b463fbb7

View File

@@ -19,14 +19,20 @@ async function getDistance (client, payload) {
}
async function getSequence (client) {
// Get the next free sequence from planned data
// if there is any planned lines, if not get the
// next available from raw lines
const text = `
SELECT max(sequence)+1 AS sequence
FROM (
SELECT sequence
FROM raw_lines
UNION SELECT sequence
FROM planned_lines
) t;
WITH p AS (
SELECT max(sequence) AS sequence
FROM planned_lines
),
r AS (
SELECT max(sequence) AS sequence
FROM raw_lines
)
SELECT COALESCE(p.sequence, r.sequence)+1 AS sequence
FROM p, r;
`;
const res = await client.query(text);