Skip to content

Commit 46e9cd4

Browse files
authored
Merge pull request #26 from Billuc/drop-gtempo
Drop gtempo and use gleam_time
2 parents 5a384e8 + 4295789 commit 46e9cd4

File tree

15 files changed

+227
-656
lines changed

15 files changed

+227
-656
lines changed

gleam.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ shellout = ">= 1.7.0 and < 2.0.0"
2525
globlin = ">= 2.0.3 and < 3.0.0"
2626
globlin_fs = ">= 2.0.0 and < 3.0.0"
2727
simplifile = ">= 2.2.1 and < 3.0.0"
28-
gtempo = ">= 7.2.2 and < 8.0.0"
2928
gleam_crypto = ">= 1.5.1 and < 2.0.0"
3029
gleam_time = ">= 1.2.0 and < 2.0.0"
3130
gleam_erlang = ">= 1.2.0 and < 2.0.0"

manifest.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ packages = [
1616
{ name = "gleeunit", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "63022D81C12C17B7F1A60E029964E830A4CBD846BBC6740004FC1F1031AE0326" },
1717
{ name = "globlin", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_regexp", "gleam_stdlib"], otp_app = "globlin", source = "hex", outer_checksum = "923BC16814DF95C4BB28111C266F0B873499480E6E125D5A16DBDF732E62CEB4" },
1818
{ name = "globlin_fs", version = "2.0.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib", "globlin", "simplifile"], otp_app = "globlin_fs", source = "hex", outer_checksum = "2A84CE81FD7958B967EF39CC234AFB64DAB20169D0EF9B9C3943CD3C5B561182" },
19-
{ name = "gtempo", version = "7.2.2", build_tools = ["gleam"], requirements = ["gleam_regexp", "gleam_stdlib", "gleam_time"], otp_app = "gtempo", source = "hex", outer_checksum = "51AACF841C5F936455973BAF7C03F1EEBF86EC0F0F8D9EBEE126CB02968D50AC" },
2019
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
2120
{ name = "pg_types", version = "0.5.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "A3023B464AA960BC1628635081E30CCA4F676F2D4C23CCD6179C1C11C9B4A642" },
2221
{ name = "pgo", version = "0.15.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "4B883D751B8D4247F4D8A6FBAE58EDB6FA9DBC183371299BF84975EE36406388" },
@@ -36,7 +35,6 @@ gleam_time = { version = ">= 1.2.0 and < 2.0.0" }
3635
gleeunit = { version = ">= 1.6.0 and < 2.0.0" }
3736
globlin = { version = ">= 2.0.3 and < 3.0.0" }
3837
globlin_fs = { version = ">= 2.0.0 and < 3.0.0" }
39-
gtempo = { version = ">= 7.2.2 and < 8.0.0" }
4038
pog = { version = ">= 4.0.0 and < 5.0.0" }
4139
shellout = { version = ">= 1.7.0 and < 2.0.0" }
4240
simplifile = { version = ">= 2.2.1 and < 3.0.0" }

src/cigogne.gleam

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import gleam/int
88
import gleam/io
99
import gleam/list
1010
import gleam/result
11-
import tempo/datetime
12-
import tempo/instant
11+
import gleam/time/timestamp
1312

1413
type SchemaData {
1514
SchemaData(generate: Bool, filename: String)
@@ -137,9 +136,7 @@ pub fn new_migration(
137136
) -> Result(Nil, types.MigrateError) {
138137
use path <- result.map(fs.create_new_migration_file(
139138
migrations_folder,
140-
instant.now()
141-
|> instant.as_utc_datetime()
142-
|> datetime.drop_offset(),
139+
timestamp.system_time(),
143140
name,
144141
))
145142
io.println("Migration file created : " <> path)

src/cigogne/internal/database.gleam

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ fn insert_migration_query(
191191
) -> pog.Query(Nil) {
192192
query_insert_migration(data.schema, data.migrations_table_name)
193193
|> pog.query()
194-
|> pog.parameter(pog.timestamp(
195-
migration.timestamp |> utils.tempo_to_pog_timestamp,
196-
))
194+
|> pog.parameter(pog.timestamp(migration.timestamp))
197195
|> pog.parameter(pog.text(migration.name))
198196
|> pog.parameter(pog.text(migration.sha256))
199197
}
@@ -205,9 +203,7 @@ fn drop_migration_query(
205203
query_drop_migration(data.schema, data.migrations_table_name)
206204
|> pog.query()
207205
|> pog.parameter(pog.text(migration.name))
208-
|> pog.parameter(pog.timestamp(
209-
migration.timestamp |> utils.tempo_to_pog_timestamp,
210-
))
206+
|> pog.parameter(pog.timestamp(migration.timestamp))
211207
}
212208

213209
pub fn get_applied_migrations(
@@ -226,19 +222,15 @@ pub fn get_applied_migrations(
226222
|> result.try(fn(returned) {
227223
case returned.rows {
228224
[] -> Error(types.NoResultError)
229-
_ as applied -> applied |> list.try_map(build_migration_data)
225+
_ as applied -> applied |> list.map(build_migration_data) |> Ok
230226
}
231227
})
232228
}
233229

234230
fn build_migration_data(
235231
data: #(timestamp.Timestamp, String, String),
236-
) -> Result(types.Migration, types.MigrateError) {
237-
case utils.pog_to_tempo_timestamp(data.0) {
238-
Error(_) ->
239-
Error(types.DateParseError(utils.pog_timestamp_to_string(data.0)))
240-
Ok(timestamp) -> Ok(types.Migration("", timestamp, data.1, [], [], data.2))
241-
}
232+
) -> types.Migration {
233+
types.Migration("", data.0, data.1, [], [], data.2)
242234
}
243235

244236
pub fn get_schema(data: DatabaseData) -> Result(String, types.MigrateError) {

src/cigogne/internal/fs.gleam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import cigogne/internal/utils
44
import cigogne/types
55
import gleam/list
66
import gleam/result
7+
import gleam/time/timestamp
78
import globlin
89
import globlin_fs
910
import simplifile
10-
import tempo
1111

1212
pub fn get_migrations(
1313
migrations_folder: String,
@@ -78,7 +78,7 @@ pub fn write_schema_file(
7878

7979
pub fn create_new_migration_file(
8080
migrations_folder: String,
81-
timestamp: tempo.NaiveDateTime,
81+
timestamp: timestamp.Timestamp,
8282
name: String,
8383
) -> Result(String, types.MigrateError) {
8484
use name <- result.try(migrations_utils.check_name(name))

src/cigogne/internal/migration_parser.gleam

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import gleam/list
55
import gleam/option
66
import gleam/result
77
import gleam/string
8-
import tempo
9-
import tempo/naive_datetime
8+
import gleam/time/timestamp
109

1110
pub const migration_up_guard = "--- migration:up"
1211

@@ -16,7 +15,7 @@ pub const migration_end_guard = "--- migration:end"
1615

1716
pub fn parse_file_name(
1817
path: String,
19-
) -> Result(#(tempo.NaiveDateTime, String), types.MigrateError) {
18+
) -> Result(#(timestamp.Timestamp, String), types.MigrateError) {
2019
use <- bool.guard(
2120
!string.ends_with(path, ".sql"),
2221
Error(types.FileNameError(path)),
@@ -30,10 +29,7 @@ pub fn parse_file_name(
3029
|> result.replace_error(types.FileNameError(path)),
3130
)
3231

33-
use ts <- result.try(
34-
naive_datetime.parse(ts_and_name.0, migrations_utils.timestamp_format)
35-
|> result.replace_error(types.DateParseError(ts_and_name.0)),
36-
)
32+
use ts <- result.try(migrations_utils.parse_migration_timestamp(ts_and_name.0))
3733
use name <- result.try(migrations_utils.check_name(ts_and_name.1))
3834

3935
#(ts, name) |> Ok

src/cigogne/internal/migrations_utils.gleam

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,79 @@
11
import cigogne/internal/utils
22
import cigogne/types
33
import gleam/bool
4+
import gleam/int
45
import gleam/list
56
import gleam/option
67
import gleam/order
78
import gleam/result
89
import 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

1413
const 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+
1668
pub 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

2372
pub 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

3079
pub 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)
218265
pub 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
}

src/cigogne/internal/utils.gleam

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ import gleam/crypto
33
import gleam/dynamic/decode
44
import gleam/int
55
import gleam/list
6-
import gleam/result
76
import gleam/string
87
import gleam/time/calendar
98
import gleam/time/timestamp
109
import pog
11-
import tempo
12-
import tempo/date
13-
import tempo/naive_datetime
14-
import tempo/time
1510

1611
pub fn describe_query_error(error: pog.QueryError) -> String {
1712
case error {
@@ -52,56 +47,10 @@ pub fn describe_decode_error(error: decode.DecodeError) -> String {
5247
<> "]"
5348
}
5449

55-
pub fn tempo_to_pog_timestamp(from: tempo.NaiveDateTime) -> timestamp.Timestamp {
56-
let #(#(year, month, day), #(hour, minutes, seconds)) =
57-
from |> naive_datetime.to_tuple
58-
59-
let assert Ok(month) = calendar.month_from_int(month)
60-
61-
let date = calendar.Date(year, month, day)
62-
let time = calendar.TimeOfDay(hour, minutes, seconds, 0)
63-
64-
timestamp.from_calendar(date, time, calendar.utc_offset)
65-
}
66-
67-
pub fn pog_to_tempo_timestamp(
68-
from: timestamp.Timestamp,
69-
) -> Result(tempo.NaiveDateTime, Nil) {
70-
let #(date, time_of_day) = from |> timestamp.to_calendar(calendar.utc_offset)
71-
72-
use date <- result.try(pog_to_tempo_date(date))
73-
use time <- result.try(pog_to_tempo_time(time_of_day))
74-
Ok(naive_datetime.new(date, time))
75-
}
76-
77-
fn pog_to_tempo_date(from: calendar.Date) -> Result(tempo.Date, Nil) {
78-
date.new(from.year, from.month |> calendar.month_to_int(), from.day)
79-
|> result.replace_error(Nil)
80-
}
81-
82-
fn pog_to_tempo_time(from: calendar.TimeOfDay) -> Result(tempo.Time, Nil) {
83-
time.new(from.hours, from.minutes, from.seconds) |> result.replace_error(Nil)
84-
}
85-
86-
pub fn pog_timestamp_to_string(value: timestamp.Timestamp) -> String {
87-
let #(date, time_of_day) = value |> timestamp.to_calendar(calendar.utc_offset)
88-
89-
[date.year, date.month |> calendar.month_to_int(), date.day]
90-
|> list.map(int.to_string)
91-
|> string.join("-")
92-
<> " "
93-
<> [time_of_day.hours, time_of_day.minutes, time_of_day.seconds]
94-
|> list.map(fn(v: Int) {
95-
case v {
96-
v if v < 10 -> "0" <> int.to_string(v)
97-
_ -> int.to_string(v)
98-
}
99-
})
100-
|> string.join(":")
101-
}
102-
103-
pub fn tempo_epoch() -> tempo.NaiveDateTime {
104-
naive_datetime.literal("1970-01-01 00:00:00")
50+
pub fn epoch() -> timestamp.Timestamp {
51+
let epoch_date = calendar.Date(1970, calendar.January, 1)
52+
let epoch_time = calendar.TimeOfDay(0, 0, 0, 0)
53+
timestamp.from_calendar(epoch_date, epoch_time, calendar.utc_offset)
10554
}
10655

10756
pub fn make_sha256(content: String) -> String {

src/cigogne/to_v3.gleam

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import cigogne/internal/database
22
import cigogne/internal/fs
33
import cigogne/internal/migrations_utils
4-
import cigogne/internal/utils
54
import cigogne/types
65
import gleam/bool
76
import gleam/dynamic/decode
@@ -87,9 +86,7 @@ fn set_hash(
8786
|> pog.query()
8887
|> pog.parameter(pog.text(file.sha256))
8988
|> pog.parameter(pog.text(file.name))
90-
|> pog.parameter(pog.timestamp(
91-
file.timestamp |> utils.tempo_to_pog_timestamp,
92-
))
89+
|> pog.parameter(pog.timestamp(file.timestamp))
9390
|> pog.execute(connection)
9491
|> result.map_error(types.PGOQueryError)
9592
|> result.map(fn(_) {

0 commit comments

Comments
 (0)