fix(habits): preserve task state and original occurrence expiry

This commit is contained in:
syntaxbullet
2026-09-04 09:46:22 +02:00
parent 724b1423eb
commit 18a1af9dfc
7 changed files with 783 additions and 6 deletions

View File

@@ -27,3 +27,27 @@ test('carryover migration preserves pre-existing counts and explicit zero correc
expect(JSON.parse(config).carryPartialProgress).toBe(false);
} finally { sqlite.close(); rmSync(directory, { recursive: true, force: true }); }
});
test('deadline migration repairs historical expiry using the original state before corrections', () => {
const directory = mkdtempSync(join(tmpdir(), 'minabot-expiry-upgrade-'));
const sqlite = new Database(':memory:'); const db = drizzle(sqlite);
try {
mkdirSync(join(directory, 'meta'));
const journal = JSON.parse(readFileSync('drizzle/meta/_journal.json', 'utf8'));
journal.entries = journal.entries.slice(0, 3);
writeFileSync(join(directory, 'meta/_journal.json'), JSON.stringify(journal));
for (const entry of journal.entries) copyFileSync(`drizzle/${entry.tag}.sql`, join(directory, `${entry.tag}.sql`));
migrate(db, { migrationsFolder: directory });
sqlite.exec("INSERT INTO users (id, discord_id, username, created_at, updated_at) VALUES ('u', 'u', 'u', 0, 0)");
sqlite.exec("INSERT INTO habits (id, user_id, created_date, created_at) VALUES ('h', 'u', '2020-01-01', 0)");
sqlite.query('INSERT INTO habit_revisions (habit_id, effective_date, config, created_at) VALUES (?, ?, ?, ?)').run('h', '2020-01-01', JSON.stringify({ name: 'Tasks', method: 'tasks', tasks: [], schedule: { type: 'daily' }, archived: false }), 0);
sqlite.exec("INSERT INTO habit_days (habit_id, revision_id, date, timezone, ends_at) VALUES ('h', 1, '2020-01-01', 'UTC', 100)");
sqlite.exec("INSERT INTO task_occurrences (id, day_id, task_id, name, done, expired_at, updated_at) VALUES ('a', 1, 'a', 'Originally completed', 0, 100, 200), ('b', 1, 'b', 'Originally incomplete', 1, 100, 200)");
sqlite.query('INSERT INTO progress_events (day_id, occurrence_id, before, after, created_at) VALUES (?, ?, ?, ?, ?)').run(1, 'a', '{"done":true}', '{"done":false}', 200);
sqlite.query('INSERT INTO progress_events (day_id, occurrence_id, before, after, created_at) VALUES (?, ?, ?, ?, ?)').run(1, 'b', '{"done":false}', '{"done":true}', 200);
migrate(db, { migrationsFolder: './drizzle' });
expect(sqlite.query('SELECT id, expired_at, closed_at, done FROM task_occurrences ORDER BY id').all()).toEqual([
{ id: 'a', expired_at: null, closed_at: 100, done: 0 }, { id: 'b', expired_at: 100, closed_at: 100, done: 1 },
]);
} finally { sqlite.close(); rmSync(directory, { recursive: true, force: true }); }
});

View File

@@ -37,7 +37,7 @@ export const habitDays = sqliteTable('habit_days', {
}, t => [index('habit_days_lookup_idx').on(t.habitId, t.date, t.revisionId)]);
export const taskOccurrences = sqliteTable('task_occurrences', {
id: text('id').primaryKey(), dayId: integer('day_id').notNull().references(() => habitDays.id), taskId: text('task_id').notNull(), name: text('name').notNull(),
done: integer('done', { mode: 'boolean' }).notNull().default(false), expiredAt: integer('expired_at'), updatedAt: integer('updated_at').notNull(),
done: integer('done', { mode: 'boolean' }).notNull().default(false), expiredAt: integer('expired_at'), closedAt: integer('closed_at'), updatedAt: integer('updated_at').notNull(),
}, t => [index('task_occurrences_day_idx').on(t.dayId)]);
export const progressEvents = sqliteTable('progress_events', {
id: integer('id').primaryKey({ autoIncrement: true }), dayId: integer('day_id').notNull().references(() => habitDays.id), occurrenceId: text('occurrence_id'),