Files
supabase/apps/studio/lib/migration-utils.ts
Ali Waseem 74b13e2cef fix: added logic to check and parse valid day.js (#40960)
* wip: added logic for migration checking

* updated null check

* updated to return undefined

* updated use memo check

* updated migration text

* updated text based on migrations data

* re-added tabbed deleted code

* Updated insertedAt date logic

* Fit missing gap + add tooltip

* Niot

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-12-02 13:18:33 +08:00

30 lines
1.0 KiB
TypeScript

import dayjs, { Dayjs } from 'dayjs'
/**
* Safely parses a migration version string as a date.
* Migration versions are typically in the format YYYYMMDDHHmmss (e.g., "20231128095400").
* However, some projects may have custom version formats (e.g., "001", "002") that cannot be parsed as dates.
*
* @param version - Migration version string
* @returns Dayjs object if the version is a valid datetime, null otherwise
*
* @example
* const parsed = parseMigrationVersion('20231128095400')
* if (parsed) {
* console.log(parsed.fromNow()) // "2 hours ago"
* console.log(parsed.format('DD MMM YYYY')) // "28 Nov 2023"
* }
*
* @example
* const invalid = parseMigrationVersion('001') // returns null
*/
export function parseMigrationVersion(version: string | null | undefined): Dayjs | undefined {
if (!version) return undefined
// Must contain only digits
if (!/^\d{14}$/.test(version)) return undefined
const parsed = dayjs(version, 'YYYYMMDDHHmmss', true)
return parsed.isValid() ? parsed : undefined
}