Skip to content

Commit 5a384e8

Browse files
authored
Merge pull request #24 from kaeedo/main
Upgrade Pog to v4
2 parents a19292f + e670729 commit 5a384e8

File tree

7 files changed

+112
-50
lines changed

7 files changed

+112
-50
lines changed

gleam.toml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ internal_modules = [
1717
]
1818

1919
[dependencies]
20-
gleam_stdlib = ">= 0.60.0 and < 2.0.0"
20+
gleam_stdlib = ">= 0.60.0 and < 1.0.0"
2121
argv = ">= 1.0.2 and < 2.0.0"
22-
pog = ">= 3.1.0 and < 4.0.0"
22+
pog = ">= 4.0.0 and < 5.0.0"
2323
envoy = ">= 1.0.2 and < 2.0.0"
24-
shellout = ">= 1.6.0 and < 2.0.0"
25-
globlin = ">= 2.0.2 and < 3.0.0"
24+
shellout = ">= 1.7.0 and < 2.0.0"
25+
globlin = ">= 2.0.3 and < 3.0.0"
2626
globlin_fs = ">= 2.0.0 and < 3.0.0"
27-
simplifile = ">= 2.2.0 and < 3.0.0"
28-
gtempo = ">= 7.2.0"
29-
gleam_crypto = ">= 1.5.0 and < 2.0.0"
27+
simplifile = ">= 2.2.1 and < 3.0.0"
28+
gtempo = ">= 7.2.2 and < 8.0.0"
29+
gleam_crypto = ">= 1.5.1 and < 2.0.0"
30+
gleam_time = ">= 1.2.0 and < 2.0.0"
31+
gleam_erlang = ">= 1.2.0 and < 2.0.0"
32+
gleam_otp = ">= 1.0.0 and < 2.0.0"
3033

3134
[dev-dependencies]
32-
gleeunit = ">= 1.0.0 and < 2.0.0"
35+
gleeunit = ">= 1.6.0 and < 2.0.0"

manifest.toml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ packages = [
55
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
66
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
77
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
8+
{ name = "exception", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "329D269D5C2A314F7364BD2711372B6F2C58FA6F39981572E5CA68624D291F8C" },
89
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
910
{ name = "gleam_crypto", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "50774BAFFF1144E7872814C566C5D653D83A3EBF23ACC3156B757A1B6819086E" },
11+
{ name = "gleam_erlang", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F91CE62A2D011FA13341F3723DB7DB118541AAA5FE7311BD2716D018F01EF9E3" },
12+
{ name = "gleam_otp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "7020E652D18F9ABAC9C877270B14160519FA0856EE80126231C505D719AD68DA" },
1013
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
1114
{ name = "gleam_stdlib", version = "0.62.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "DC8872BC0B8550F6E22F0F698CFE7F1E4BDA7312FDEB40D6C3F44C5B706C8310" },
1215
{ name = "gleam_time", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "DCDDC040CE97DA3D2A925CDBBA08D8A78681139745754A83998641C8A3F6587E" },
@@ -17,20 +20,23 @@ packages = [
1720
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
1821
{ name = "pg_types", version = "0.5.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "A3023B464AA960BC1628635081E30CCA4F676F2D4C23CCD6179C1C11C9B4A642" },
1922
{ name = "pgo", version = "0.15.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "4B883D751B8D4247F4D8A6FBAE58EDB6FA9DBC183371299BF84975EE36406388" },
20-
{ name = "pog", version = "3.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "35CBBE8F844DE9E5676CF306CDE2BD98AA6A25920D2EBE771FD5A293EB454161" },
23+
{ name = "pog", version = "4.1.0", build_tools = ["gleam"], requirements = ["exception", "gleam_erlang", "gleam_otp", "gleam_stdlib", "gleam_time", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "E4AFBA39A5FAA2E77291836C9683ADE882E65A06AB28CA7D61AE7A3AD61EBBD5" },
2124
{ name = "shellout", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "1BDC03438FEB97A6AF3E396F4ABEB32BECF20DF2452EC9A8C0ACEB7BDDF70B14" },
2225
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
2326
]
2427

2528
[requirements]
2629
argv = { version = ">= 1.0.2 and < 2.0.0" }
2730
envoy = { version = ">= 1.0.2 and < 2.0.0" }
28-
gleam_crypto = { version = ">= 1.5.0 and < 2.0.0" }
29-
gleam_stdlib = { version = ">= 0.60.0 and < 2.0.0" }
30-
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
31-
globlin = { version = ">= 2.0.2 and < 3.0.0" }
31+
gleam_crypto = { version = ">= 1.5.1 and < 2.0.0" }
32+
gleam_erlang = { version = ">= 1.2.0 and < 2.0.0" }
33+
gleam_otp = { version = ">= 1.0.0 and < 2.0.0" }
34+
gleam_stdlib = { version = ">= 0.60.0 and < 1.0.0" }
35+
gleam_time = { version = ">= 1.2.0 and < 2.0.0" }
36+
gleeunit = { version = ">= 1.6.0 and < 2.0.0" }
37+
globlin = { version = ">= 2.0.3 and < 3.0.0" }
3238
globlin_fs = { version = ">= 2.0.0 and < 3.0.0" }
33-
gtempo = { version = ">= 7.2.0" }
34-
pog = { version = ">= 3.1.0 and < 4.0.0" }
35-
shellout = { version = ">= 1.6.0 and < 2.0.0" }
36-
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
39+
gtempo = { version = ">= 7.2.2 and < 8.0.0" }
40+
pog = { version = ">= 4.0.0 and < 5.0.0" }
41+
shellout = { version = ">= 1.7.0 and < 2.0.0" }
42+
simplifile = { version = ">= 2.2.1 and < 3.0.0" }

src/cigogne/internal/database.gleam

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import cigogne/internal/utils
33
import cigogne/types
44
import envoy
55
import gleam/dynamic/decode
6+
import gleam/erlang/process
67
import gleam/list
78
import gleam/option
89
import gleam/result
10+
import gleam/time/timestamp
911
import pog
1012
import shellout
1113

@@ -76,20 +78,35 @@ fn connect(
7678
envoy.get("DATABASE_URL")
7779
|> result.replace_error(types.EnvVarError("DATABASE_URL"))
7880
|> result.try(fn(url) {
79-
pog.url_config(url)
80-
|> result.replace_error(types.UrlError(url))
81-
|> result.map(fn(c) { #(pog.connect(c), option.Some(url)) })
81+
connection_from_url(url)
82+
|> result.map(fn(c) { #(c, option.Some(url)) })
8283
})
8384
}
84-
types.PogConfig(config:) -> Ok(#(config |> pog.connect, option.None))
85+
types.PogConfig(config:) ->
86+
config
87+
|> pog.start
88+
|> result.map_error(types.ActorStartError)
89+
|> result.map(fn(actor) { #(actor.data, option.None) })
8590
types.UrlConfig(url:) -> {
86-
pog.url_config(url)
87-
|> result.replace_error(types.UrlError(url))
88-
|> result.map(fn(c) { #(pog.connect(c), option.Some(url)) })
91+
connection_from_url(url)
92+
|> result.map(fn(c) { #(c, option.Some(url)) })
8993
}
9094
}
9195
}
9296

97+
fn connection_from_url(
98+
url: String,
99+
) -> Result(pog.Connection, types.MigrateError) {
100+
let db_process_name = process.new_name("cigogne")
101+
102+
pog.url_config(db_process_name, url)
103+
|> result.replace_error(types.UrlError(url))
104+
|> result.try(fn(c) {
105+
pog.start(c) |> result.map_error(types.ActorStartError)
106+
})
107+
|> result.map(fn(actor) { actor.data })
108+
}
109+
93110
pub fn migrations_table_exists(
94111
data: DatabaseData,
95112
) -> Result(Bool, types.MigrateError) {
@@ -215,7 +232,7 @@ pub fn get_applied_migrations(
215232
}
216233

217234
fn build_migration_data(
218-
data: #(pog.Timestamp, String, String),
235+
data: #(timestamp.Timestamp, String, String),
219236
) -> Result(types.Migration, types.MigrateError) {
220237
case utils.pog_to_tempo_timestamp(data.0) {
221238
Error(_) ->

src/cigogne/internal/utils.gleam

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import gleam/int
55
import gleam/list
66
import gleam/result
77
import gleam/string
8+
import gleam/time/calendar
9+
import gleam/time/timestamp
810
import pog
911
import tempo
1012
import tempo/date
@@ -32,7 +34,7 @@ pub fn describe_query_error(error: pog.QueryError) -> String {
3234
}
3335
}
3436

35-
pub fn describe_transaction_error(error: pog.TransactionError) -> String {
37+
pub fn describe_transaction_error(error: pog.TransactionError(_)) -> String {
3638
case error {
3739
pog.TransactionQueryError(suberror) -> describe_query_error(suberror)
3840
pog.TransactionRolledBack(message) ->
@@ -50,37 +52,45 @@ pub fn describe_decode_error(error: decode.DecodeError) -> String {
5052
<> "]"
5153
}
5254

53-
pub fn tempo_to_pog_timestamp(from: tempo.NaiveDateTime) -> pog.Timestamp {
54-
let as_tuple = from |> naive_datetime.to_tuple
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
5558

56-
pog.Timestamp(
57-
pog.Date(as_tuple.0.0, as_tuple.0.1, as_tuple.0.2),
58-
pog.Time(as_tuple.1.0, as_tuple.1.1, as_tuple.1.2, 0),
59-
)
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)
6065
}
6166

6267
pub fn pog_to_tempo_timestamp(
63-
from: pog.Timestamp,
68+
from: timestamp.Timestamp,
6469
) -> Result(tempo.NaiveDateTime, Nil) {
65-
use date <- result.try(pog_to_tempo_date(from.date))
66-
use time <- result.try(pog_to_tempo_time(from.time))
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))
6774
Ok(naive_datetime.new(date, time))
6875
}
6976

70-
fn pog_to_tempo_date(from: pog.Date) -> Result(tempo.Date, Nil) {
71-
date.new(from.year, from.month, from.day) |> result.replace_error(Nil)
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)
7280
}
7381

74-
fn pog_to_tempo_time(from: pog.Time) -> Result(tempo.Time, Nil) {
82+
fn pog_to_tempo_time(from: calendar.TimeOfDay) -> Result(tempo.Time, Nil) {
7583
time.new(from.hours, from.minutes, from.seconds) |> result.replace_error(Nil)
7684
}
7785

78-
pub fn pog_timestamp_to_string(value: pog.Timestamp) -> String {
79-
[value.date.year, value.date.month, value.date.day]
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]
8090
|> list.map(int.to_string)
8191
|> string.join("-")
8292
<> " "
83-
<> [value.time.hours, value.time.minutes, value.time.seconds]
93+
<> [time_of_day.hours, time_of_day.minutes, time_of_day.seconds]
8494
|> list.map(fn(v: Int) {
8595
case v {
8696
v if v < 10 -> "0" <> int.to_string(v)

src/cigogne/types.gleam

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cigogne/internal/utils
22
import gleam/io
33
import gleam/list
4+
import gleam/otp/actor
45
import pog
56
import tempo
67
import tempo/naive_datetime
@@ -44,7 +45,7 @@ pub type MigrateError {
4445
FileNameError(path: String)
4546
CompoundError(errors: List(MigrateError))
4647
ContentError(path: String, error: String)
47-
PGOTransactionError(error: pog.TransactionError)
48+
PGOTransactionError(error: pog.TransactionError(String))
4849
PGOQueryError(error: pog.QueryError)
4950
NoResultError
5051
SchemaQueryError(error: String)
@@ -54,6 +55,7 @@ pub type MigrateError {
5455
DateParseError(date: String)
5556
FileHashChanged(migration_ts: tempo.NaiveDateTime, migration_name: String)
5657
NameTooLongError(name: String)
58+
ActorStartError(error: actor.StartError)
5759
}
5860

5961
/// Migrations are often generated by reading migration files.
@@ -133,5 +135,13 @@ pub fn print_migrate_error(error: MigrateError) -> Nil {
133135
)
134136
CreateFolderError(path) ->
135137
io.println_error("Couldn't create folder at path [" <> path <> "]")
138+
ActorStartError(error:) ->
139+
case error {
140+
actor.InitExited(_) ->
141+
io.println_error("Actor init exited unexpectedly")
142+
actor.InitFailed(reason) ->
143+
io.println_error("Actor init failed: " <> reason)
144+
actor.InitTimeout -> io.println_error("Connection actor init timed out")
145+
}
136146
}
137147
}

test/cigogne_test/internal/database_test.gleam

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cigogne/internal/database
22
import cigogne/internal/migrations_utils
33
import cigogne/types
44
import envoy
5+
import gleam/erlang/process
56
import gleam/list
67
import gleam/option
78
import gleam/result
@@ -55,10 +56,11 @@ pub fn init_with_url_test() {
5556
}
5657

5758
pub fn init_with_pog_config_test() {
59+
let name = process.new_name("cigogne_test")
5860
let init_res =
5961
database.init(
6062
types.PogConfig(
61-
pog.default_config()
63+
pog.default_config(name)
6264
|> pog.host("localhost")
6365
|> pog.port(5432)
6466
|> pog.user(db_user)
@@ -76,11 +78,12 @@ pub fn init_with_pog_config_test() {
7678
}
7779

7880
pub fn init_with_connection_test() {
79-
let assert Ok(conf) = pog.url_config(db_url())
80-
let conn = pog.connect(conf)
81+
let name = process.new_name("cigogne_test")
82+
let assert Ok(conf) = pog.url_config(name, db_url())
83+
let assert Ok(actor) = pog.start(conf)
8184

8285
let init_res =
83-
database.init(types.ConnectionConfig(conn), schema, migration_table)
86+
database.init(types.ConnectionConfig(actor.data), schema, migration_table)
8487
let assert Ok(init_res) = init_res
8588

8689
assert init_res.db_url == option.None

test/cigogne_test/internal/utils_test.gleam

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import cigogne/internal/utils
22
import gleam/string
3+
import gleam/time/calendar
4+
import gleam/time/timestamp
35
import gleeunit/should
4-
import pog
56
import tempo/naive_datetime
67

78
pub fn tempo_epoch_test() {
@@ -13,18 +14,30 @@ pub fn tempo_epoch_test() {
1314
pub fn tempo_to_pog_test() {
1415
naive_datetime.literal("2004-10-15T04:25:33")
1516
|> utils.tempo_to_pog_timestamp
16-
|> should.equal(pog.Timestamp(pog.Date(2004, 10, 15), pog.Time(04, 25, 33, 0)))
17+
|> should.equal(timestamp.from_calendar(
18+
calendar.Date(2004, calendar.October, 15),
19+
calendar.TimeOfDay(04, 25, 33, 0),
20+
calendar.utc_offset,
21+
))
1722
}
1823

1924
pub fn pog_to_tempo_test() {
20-
pog.Timestamp(pog.Date(2410, 12, 24), pog.Time(16, 42, 0, 0))
25+
timestamp.from_calendar(
26+
calendar.Date(2410, calendar.December, 24),
27+
calendar.TimeOfDay(16, 42, 0, 0),
28+
calendar.utc_offset,
29+
)
2130
|> utils.pog_to_tempo_timestamp
2231
|> should.be_ok
2332
|> should.equal(naive_datetime.literal("2410-12-24T16:42:00"))
2433
}
2534

2635
pub fn pog_to_string_test() {
27-
pog.Timestamp(pog.Date(2410, 12, 24), pog.Time(16, 42, 0, 0))
36+
timestamp.from_calendar(
37+
calendar.Date(2410, calendar.December, 24),
38+
calendar.TimeOfDay(16, 42, 0, 0),
39+
calendar.utc_offset,
40+
)
2841
|> utils.pog_timestamp_to_string
2942
|> should.equal("2410-12-24 16:42:00")
3043
}

0 commit comments

Comments
 (0)