I’m trying to fetch records based on the timestamps, here are the expression I tried and didnt work
def get_expired_contracts(seconds) do
now = NaiveDateTime.utc_now()
cutoff_time = NaiveDateTime.add(now, -seconds, :second)
query =
from c in Contract,
where: c.inserted_at < ^cutoff_time,
select: c
Repo.all(query)
rescue
error ->
{:error, FormatError.initerror("expired", "database_error")}
end
where: c.inserted_at < ago(100, "second"),
where: c.inserted_at > type(^cutoff_time, :naive_datetime)
i tried the operador back and forward (< >) no results are returned
i’m running mix phx.server with no env variable, so it as dev enviroment
1 | btc | btc | 54.000 | 57.240 | | 0.00 | 00020126580014br.gov.bcb.pix0136ab09d852-14bd-43ac-91eb-d38b717e6aea520400005303986540554.005802BR5914Pedro Henrique6005Macae62290525PEDROHEN00000000601126ASA63042C22 | 2ASqVVJQkv6MDzinpktRRY5iF9F2vJRZAKvTHYS2sead | 5BjxZERxM7y38EFgTya5QXDSfGgdNAeDbyPVamWnn11ayNhTNRW663UreTssks2cGLWVbfMcCvYbL9aHo2aDsqNZ | 0.074 | pending | 2025-02-26 17:53:44 | 2025-02-26 17:53:44
(1 row)
the record does exist on the database
I use default timestamps
def change do
create table(:contracts) do
add :chain, :string, size: 5, null: false
add :coin, :string, size: 5, null: false
add :transaction_value, :decimal, precision: 7, scale: 3, null: false
add :transaction_charged, :decimal, precision: 7, scale: 3, null: false
add :ref, :string, size: 20
add :ref_fee, :decimal, default: 0.0
add :payload, :string, size: 170, null: false
add :pubkey, :string, size: 100, null: false
add :private, :string, size: 100, null: false
add :amount, :decimal, precision: 7, scale: 3, null: false
add :status, :string, size: 20, default: "pending"
timestamps()
end
I inserted the record via api, so the schema tracks to the right table
Interactive Elixir (1.17.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Testespay.DataLayer.EctoContractDomain.get_all_contracts()
[debug] QUERY OK source="contracts" db=16.5ms queue=2.1ms idle=963.6ms
SELECT c0."id", c0."chain", c0."coin", c0."transaction_value", c0."transaction_charged", c0."ref", c0."ref_fee", c0."payload", c0."pubkey", c0."private", c0."status", c0."amount", c0."inserted_at", c0."updated_at" FROM "contracts" AS c0 []
[
%Testespay.Schemas.Contract{
__meta__: #Ecto.Schema.Metadata<:loaded, "contracts">,
id: 1,
chain: "btc",
coin: "btc",
transaction_value: Decimal.new("54.000"),
transaction_charged: Decimal.new("57.240"),
ref: nil,
ref_fee: Decimal.new("0.00"),
payload: "00020126580014br.gov.bcb.pix0136ab09d852-14bd-43ac-91eb-d38b717e6aea520400005303986540554.005802BR5914Pedro Henrique6005Macae62290525PEDROHEN00000000601126ASA63042C22",
pubkey: "2ASqVVJQkv6MDzinpktRRY5iF9F2vJRZAKvTHYS2sead",
private: "5BjxZERxM7y38EFgTya5QXDSfGgdNAeDbyPVamWnn11ayNhTNRW663UreTssks2cGLWVbfMcCvYbL9aHo2aDsqNZ",
status: "pending",
amount: Decimal.new("0.074"),
inserted_at: ~N[2025-02-26 17:53:44],
updated_at: ~N[2025-02-26 17:53:44]
}
]