11import cigogne/internal/utils
22import cigogne/types
33import gleam/bool
4+ import gleam/int
45import gleam/list
56import gleam/option
67import gleam/order
78import gleam/result
89import gleam/string
9- import tempo
10- import tempo/naive_datetime
11-
12- pub const timestamp_format = tempo . CustomNaive ( "YYYYMMDDHHmmss" )
10+ import gleam/time/calendar
11+ import gleam/time/timestamp
1312
1413const max_name_length = 255
1514
15+ pub fn format_migration_timestamp ( timestamp : timestamp . Timestamp ) -> String {
16+ let # ( date , time ) = timestamp |> timestamp . to_calendar ( calendar . utc_offset )
17+
18+ let date_str =
19+ [ date . year , date . month |> calendar . month_to_int , date . day ]
20+ |> list . map ( int_to_2_chars_string )
21+ |> string . join ( "" )
22+ let time_str =
23+ [ time . hours , time . minutes , time . seconds ]
24+ |> list . map ( int_to_2_chars_string )
25+ |> string . join ( "" )
26+
27+ date_str <> time_str
28+ }
29+
30+ fn int_to_2_chars_string ( n : Int ) -> String {
31+ case n {
32+ n if n < 10 -> "0" <> int . to_string ( n )
33+ _ -> int . to_string ( n )
34+ }
35+ }
36+
37+ pub fn parse_migration_timestamp (
38+ timestamp_str : String ,
39+ ) -> Result ( timestamp . Timestamp , types . MigrateError ) {
40+ use <- bool . guard (
41+ string . length ( timestamp_str ) != 14 ,
42+ Error ( types . DateParseError ( timestamp_str ) ) ,
43+ )
44+
45+ let year = string . slice ( timestamp_str , 0 , 4 ) |> int . parse
46+ let month = string . slice ( timestamp_str , 4 , 2 ) |> int . parse
47+ let day = string . slice ( timestamp_str , 6 , 2 ) |> int . parse
48+ let hours = string . slice ( timestamp_str , 8 , 2 ) |> int . parse
49+ let minutes = string . slice ( timestamp_str , 10 , 2 ) |> int . parse
50+ let seconds = string . slice ( timestamp_str , 12 , 2 ) |> int . parse
51+
52+ case year , month , day , hours , minutes , seconds {
53+ Ok ( y ) , Ok ( m ) , Ok ( d ) , Ok ( h ) , Ok ( min ) , Ok ( s ) -> {
54+ calendar . month_from_int ( m )
55+ |> result . replace_error ( types . DateParseError ( timestamp_str ) )
56+ |> result . map ( fn ( m ) {
57+ timestamp . from_calendar (
58+ calendar . Date ( y , m , d ) ,
59+ calendar . TimeOfDay ( h , min , s , 0 ) ,
60+ calendar . utc_offset ,
61+ )
62+ } )
63+ }
64+ _ , _ , _ , _ , _ , _ -> Error ( types . DateParseError ( timestamp_str ) )
65+ }
66+ }
67+
1668pub fn format_migration_name ( migration : types . Migration ) -> String {
17- migration . timestamp
18- |> naive_datetime . format ( timestamp_format )
19- <> "-"
20- <> migration . name
69+ format_migration_timestamp ( migration . timestamp ) <> "-" <> migration . name
2170}
2271
2372pub fn to_migration_filename (
24- timestamp : tempo . NaiveDateTime ,
73+ timestamp : timestamp . Timestamp ,
2574 name : String ,
2675) -> String {
27- timestamp |> naive_datetime . format ( timestamp_format ) <> "-" <> name <> ".sql"
76+ format_migration_timestamp ( timestamp ) <> "-" <> name <> ".sql"
2877}
2978
3079pub fn check_name ( name : String ) -> Result ( String , types . MigrateError ) {
@@ -40,9 +89,7 @@ pub fn find_migration(
4089 data : types . Migration ,
4190) -> Result ( types . Migration , types . MigrateError ) {
4291 migrations
43- |> list . find ( fn ( m ) {
44- naive_datetime . is_equal ( m . timestamp , data . timestamp ) && m . name == data . name
45- } )
92+ |> list . find ( fn ( m ) { m . timestamp == data . timestamp && m . name == data . name } )
4693 |> result . replace_error ( types . MigrationNotFoundError (
4794 data . timestamp ,
4895 data . name ,
@@ -53,7 +100,7 @@ pub fn compare_migrations(
53100 migration_a : types . Migration ,
54101 migration_b : types . Migration ,
55102) -> order . Order {
56- naive_datetime . compare ( migration_a . timestamp , migration_b . timestamp )
103+ timestamp . compare ( migration_a . timestamp , migration_b . timestamp )
57104 |> order . break_tie ( string . compare ( migration_a . name , migration_b . name ) )
58105}
59106
@@ -204,7 +251,7 @@ pub fn create_zero_migration(
204251) -> types . Migration {
205252 types . Migration (
206253 "" ,
207- utils . tempo_epoch ( ) ,
254+ utils . epoch ( ) ,
208255 name ,
209256 queries_up ,
210257 queries_down ,
@@ -216,6 +263,5 @@ pub fn create_zero_migration(
216263
217264/// Checks if a migration is a zero migration (has been created with create_zero_migration)
218265pub fn is_zero_migration ( migration : types . Migration ) -> Bool {
219- { migration . path == "" }
220- |> bool . and ( naive_datetime . is_equal ( migration . timestamp , utils . tempo_epoch ( ) ) )
266+ migration . path == "" && migration . timestamp == utils . epoch ( )
221267}
0 commit comments