-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reset createdAt to now for record previously deleted
- Loading branch information
Showing
2 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/shared/lib/db/migrations/20240322141605_undelete_records_reset_createdat.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const RECORDS_TABLE = '_nango_sync_data_records'; | ||
exports.up = async function (knex) { | ||
await knex.transaction((trx) => { | ||
return trx | ||
.raw( | ||
` | ||
CREATE OR REPLACE FUNCTION ${RECORDS_TABLE}_reset_created_at() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
IF OLD.external_deleted_at IS NOT NULL AND NEW.external_deleted_at IS NULL THEN | ||
NEW.created_at = NOW(); | ||
NEW.updated_at = NOW(); | ||
NEW.external_is_deleted = FALSE; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
` | ||
) | ||
.then(function () { | ||
return trx.raw(` | ||
CREATE TRIGGER ${RECORDS_TABLE}_reset_created_at_trigger | ||
BEFORE UPDATE ON ${RECORDS_TABLE} | ||
FOR EACH ROW | ||
EXECUTE FUNCTION ${RECORDS_TABLE}_reset_created_at(); | ||
`); | ||
}); | ||
}); | ||
}; | ||
|
||
exports.down = async function (knex) { | ||
await knex.transaction((trx) => { | ||
return trx.raw(`DROP TRIGGER IF EXISTS ${RECORDS_TABLE}_reset_created_at_trigger ON ${RECORDS_TABLE};`).then(function () { | ||
return trx.raw(`DROP FUNCTION IF EXISTS ${RECORDS_TABLE}_reset_created_at();`); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters