diff --git a/server/src/db/entities/VacayUserYears.entity.ts b/server/src/db/entities/VacayUserYears.entity.ts index 2575c2c1d..900446dff 100644 --- a/server/src/db/entities/VacayUserYears.entity.ts +++ b/server/src/db/entities/VacayUserYears.entity.ts @@ -12,7 +12,7 @@ export class VacayUserYears { plan_id!: number; year!: number; vacation_days?: number | null = 30; - carried_over?: number | null = 0; + carried_over?: number | null; } export const VacayUserYearsSchema = defineEntity({ @@ -27,6 +27,6 @@ export const VacayUserYearsSchema = defineEntity({ plan_id: p.integer().persist(false).index('idx_vacay_user_years_plan_id'), year: p.integer(), vacation_days: p.integer().nullable(), - carried_over: p.integer().nullable(), + carried_over: p.double().nullable().defaultRaw(`0`), }, }); diff --git a/server/src/db/migrations/Migration20200101046450_a_carried_over_half_day_stays_half.ts b/server/src/db/migrations/Migration20200101046450_a_carried_over_half_day_stays_half.ts new file mode 100644 index 000000000..faa8f7360 --- /dev/null +++ b/server/src/db/migrations/Migration20200101046450_a_carried_over_half_day_stays_half.ts @@ -0,0 +1,55 @@ +import { engineDdl } from '../migration-utils'; +import { Migration } from '@mikro-orm/migrations'; + +/** + * A vacation day can be taken as a half day (`vacay_entries.fraction` 0.5), so + * what is left of a year and carried into the next one can end in a half: + * 30 days less one half day carries 29.5. `vacay_user_years.carried_over` was + * declared INTEGER. SQLite stored 29.5 in it anyway, as a REAL, while Postgres, + * where the column was `bigint`, rounded it to 30, so the two engines disagreed + * about every balance with a half day in it, and a SQLite install holding such a + * value could not be moved to Postgres. + * + * The column becomes a REAL on SQLite and `double precision` on Postgres, the + * type `vacay_entries.fraction` already has. `vacation_days`, the allowance a + * member sets, stays a whole number. + * + * SQLite cannot change a column's type in place, so its table is rebuilt: the + * same columns, keys, index and AUTOINCREMENT counter, every row with its id. + * No other table points at `vacay_user_years`. + */ +export class Migration20200101046450_a_carried_over_half_day_stays_half extends Migration { + override name = 'Migration20200101046450_a_carried_over_half_day_stays_half'; + + override up(): void { + engineDdl(this, { + sqlite: [ + `CREATE TABLE vacay_user_years_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + plan_id INTEGER NOT NULL REFERENCES vacay_plans(id) ON DELETE CASCADE, + year INTEGER NOT NULL, + vacation_days INTEGER DEFAULT 30, + carried_over REAL DEFAULT 0, + UNIQUE(user_id, plan_id, year) + )`, + `INSERT INTO vacay_user_years_new (id, user_id, plan_id, year, vacation_days, carried_over) + SELECT id, user_id, plan_id, year, vacation_days, carried_over FROM vacay_user_years`, + // The copy left the new table's counter at its highest id; the old + // counter can be above that when the newest rows were deleted. + `UPDATE sqlite_sequence SET seq = (SELECT old.seq FROM sqlite_sequence old WHERE old.name = 'vacay_user_years') + WHERE name = 'vacay_user_years_new' + AND seq < (SELECT old.seq FROM sqlite_sequence old WHERE old.name = 'vacay_user_years')`, + `INSERT INTO sqlite_sequence (name, seq) + SELECT 'vacay_user_years_new', seq FROM sqlite_sequence WHERE name = 'vacay_user_years' + AND NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = 'vacay_user_years_new')`, + 'DROP TABLE vacay_user_years', + 'ALTER TABLE vacay_user_years_new RENAME TO vacay_user_years', + 'CREATE INDEX idx_vacay_user_years_plan_id ON vacay_user_years(plan_id)', + ], + postgres: [ + 'alter table "vacay_user_years" alter column "carried_over" type double precision using ("carried_over"::double precision)', + ], + }); + } +} diff --git a/server/tests/integration/vacay.test.ts b/server/tests/integration/vacay.test.ts index c25f58fc2..1f6e51d76 100644 --- a/server/tests/integration/vacay.test.ts +++ b/server/tests/integration/vacay.test.ts @@ -4,6 +4,7 @@ */ import { buildApp } from '../../src/bootstrap'; import { VacayHolidayCalendars } from '../../src/db/entities/VacayHolidayCalendars.entity'; +import { VacayUserYears } from '../../src/db/entities/VacayUserYears.entity'; import { VacayService } from '../../src/nest/vacay/vacay.service'; import { authCookie } from '../helpers/auth'; import type { FactoryOrm } from '../helpers/factories/context'; @@ -200,6 +201,33 @@ describe('Vacay entries', () => { }); }); +describe('Vacay carry-over', () => { + it('VACAY-046 — a half day left over carries as a half day into the next year, on every engine', async () => { + const { user } = await makeUser(orm); + const cookie = authCookie(user.id); + await request(app).get('/api/addons/vacay/plan').set('Cookie', cookie); + await request(app).put('/api/addons/vacay/plan').set('Cookie', cookie).send({ carry_over_enabled: true }); + await request(app).post('/api/addons/vacay/years').set('Cookie', cookie).send({ year: 2025 }); + await request(app).post('/api/addons/vacay/years').set('Cookie', cookie).send({ year: 2026 }); + const half = await request(app) + .post('/api/addons/vacay/entries/toggle') + .set('Cookie', cookie) + .send({ date: '2025-06-16', year: 2025, type: 'vacation', fraction: 0.5 }); + expect(half.status).toBe(200); + + // Reading 2025 writes what is left of it into 2026: 30 days less a half day. + expect((await request(app).get('/api/addons/vacay/stats/2025').set('Cookie', cookie)).status).toBe(200); + const next = await request(app).get('/api/addons/vacay/stats/2026').set('Cookie', cookie); + expect(next.status).toBe(200); + const mine = (next.body.stats as Array<{ user_id: number; carried_over: number }>).find( + (s) => s.user_id === user.id, + ); + expect(mine?.carried_over).toBe(29.5); + const [row] = await findRows(orm, VacayUserYears, { user: user.id, year: 2026 }); + expect(row.carried_over).toBe(29.5); + }); +}); + describe('Vacay color', () => { it('VACAY-024 — PUT /api/addons/vacay/color sets user color in plan', async () => { const { user } = await makeUser(orm);