diff --git a/migrations/20250101000000_change_time_us_to_bigint.sql b/migrations/20250101000000_change_time_us_to_bigint.sql index 6c01c2b..d94b4b7 100644 --- a/migrations/20250101000000_change_time_us_to_bigint.sql +++ b/migrations/20250101000000_change_time_us_to_bigint.sql @@ -1,4 +1,9 @@ -- Change time_us from TEXT to BIGINT. --- The existing values are stored as microsecond epoch strings and cast cleanly. +-- First, replace any invalid values (like "undef") with 0. +UPDATE posts +SET time_us = '0' +WHERE time_us = 'undef' OR time_us !~ '^\d+$'; + +-- Now cast the column to BIGINT ALTER TABLE posts ALTER COLUMN time_us TYPE BIGINT USING time_us::BIGINT;