Fix month close migration constraints
This commit is contained in:
1 parent
a760f423a4
commit
4f0020a61d
2 files changed
+19
-3
No files matched your search
@@ -2962,13 +2962,13 @@ app.post("/api/periods/:periodType/:period/close", requireUser, async (req, res)
|
|||||||
false AS ticket_closed
|
false AS ticket_closed
|
||||||
FROM tickets t
|
FROM tickets t
|
||||||
JOIN sessions s ON s.ticket_id = t.id
|
JOIN sessions s ON s.ticket_id = t.id
|
||||||
WHERE s.started_at >= $2::timestamptz AND s.started_at < $3::timestamptz
|
WHERE s.started_at >= $1::timestamptz AND s.started_at < $2::timestamptz
|
||||||
AND s.user_id = $4
|
AND s.user_id = $3
|
||||||
GROUP BY t.id, t.ticket_number
|
GROUP BY t.id, t.ticket_number
|
||||||
HAVING COUNT(*) FILTER (WHERE s.billing_status IS NULL) > 0
|
HAVING COUNT(*) FILTER (WHERE s.billing_status IS NULL) > 0
|
||||||
ORDER BY t.ticket_number ASC;
|
ORDER BY t.ticket_number ASC;
|
||||||
`,
|
`,
|
||||||
[period.start, period.startIso, period.endIso, userId]
|
[period.startIso, period.endIso, userId]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (blockersResult.rows.length > 0) {
|
if (blockersResult.rows.length > 0) {
|
||||||
|
|||||||
@@ -7,9 +7,25 @@ async function findMigrationAdmin() {
|
|||||||
|
|
||||||
async function ensureClosureOwnership(table: string, column: string, primaryKeyColumns: string[], adminId: string | null) {
|
async function ensureClosureOwnership(table: string, column: string, primaryKeyColumns: string[], adminId: string | null) {
|
||||||
await query(`ALTER TABLE ${table} ADD COLUMN IF NOT EXISTS user_id BIGINT REFERENCES users(id);`);
|
await query(`ALTER TABLE ${table} ADD COLUMN IF NOT EXISTS user_id BIGINT REFERENCES users(id);`);
|
||||||
|
|
||||||
if (adminId) {
|
if (adminId) {
|
||||||
await query(`UPDATE ${table} SET user_id = $1 WHERE user_id IS NULL;`, [adminId]);
|
await query(`UPDATE ${table} SET user_id = $1 WHERE user_id IS NULL;`, [adminId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const duplicateMatchers = [...primaryKeyColumns, "user_id"]
|
||||||
|
.map((key) => `older.${key} IS NOT DISTINCT FROM newer.${key}`)
|
||||||
|
.join(" AND ");
|
||||||
|
|
||||||
|
await query(`
|
||||||
|
DELETE FROM ${table} older
|
||||||
|
USING ${table} newer
|
||||||
|
WHERE ${duplicateMatchers}
|
||||||
|
AND (
|
||||||
|
older.closed_at < newer.closed_at
|
||||||
|
OR (older.closed_at = newer.closed_at AND older.ctid < newer.ctid)
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
await query(`
|
await query(`
|
||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|||||||
Reference in new issue
Block a user