-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add delete after duration column that stores the time to wai…
…t before deletion (#223)
- Loading branch information
1 parent
13b3416
commit 7218ba3
Showing
20 changed files
with
257 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Qrstorage.QrCodes.QrCodeMigrationHelper do | ||
@spec add_delete_after_months(Ecto.Repo.t()) :: any() | ||
def add_delete_after_months(repo) do | ||
repo.query!( | ||
"UPDATE qrcodes | ||
SET delete_after_months = | ||
CASE | ||
WHEN DATE_TRUNC('hour', delete_after) <= DATE_TRUNC('hour', inserted_at + INTERVAL '1 hour') THEN 0 | ||
WHEN DATE_TRUNC('day', delete_after) <= DATE_TRUNC('day', inserted_at + INTERVAL '1 month') THEN 1 | ||
WHEN DATE_TRUNC('day', delete_after) <= DATE_TRUNC('day', inserted_at + INTERVAL '6 months') THEN 6 | ||
WHEN DATE_TRUNC('day', delete_after) <= DATE_TRUNC('day', inserted_at + INTERVAL '12 months') THEN 12 | ||
ELSE 36 | ||
END; | ||
", | ||
[], | ||
log: :info | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 9 additions & 15 deletions
24
lib/qrstorage_web/templates/qr_code/settings/_settings_delete_after.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
<div class="row m-2"> | ||
<div class="col-auto mx-auto"> | ||
<h6><%= gettext("delete after") %></h6> | ||
<div class="d-flex justify-content-center"> | ||
<div class="btn-group" role="group"> | ||
<%= radio_button(@f, :delete_after_months, 1, class: "btn-check", autocomplete: "off") %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_months_1"}>1</label> | ||
|
||
<div class="btn-group" role="group"> | ||
<%= radio_button(@f, :delete_after, 1, | ||
class: "btn-check", | ||
autocomplete: "off", | ||
checked: true | ||
) %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_1"}>1</label> | ||
<%= radio_button(@f, :delete_after_months, 12, class: "btn-check", autocomplete: "off", checked: true) %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_months_12"}>12</label> | ||
|
||
<%= radio_button(@f, :delete_after, 6, class: "btn-check", autocomplete: "off") %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_6"}>6</label> | ||
|
||
<%= radio_button(@f, :delete_after, 12, class: "btn-check", autocomplete: "off") %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_12"}>12</label> | ||
|
||
<%= radio_button(@f, :delete_after, 0, class: "btn-check", autocomplete: "off") %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_0"}>∞</label> | ||
<%= radio_button(@f, :delete_after_months, 36, class: "btn-check", autocomplete: "off") %> | ||
<label class="btn btn-secondary" for={@f.id <> "_delete_after_months_36"}>36</label> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
priv/repo/migrations/20240316123335_add_delete_after_months.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Qrstorage.Repo.Migrations.AddDeleteAfterMonths do | ||
alias Qrstorage.QrCodes.QrCodeMigrationHelper | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:qrcodes) do | ||
add :delete_after_months, :integer, default: 1 | ||
end | ||
|
||
execute(&execute_up/0, &execute_down/0) | ||
end | ||
|
||
defp execute_up, | ||
do: QrCodeMigrationHelper.add_delete_after_months(repo()) | ||
|
||
defp execute_down, do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Qrstorage.QrCodes.QrCodeMigrationHelperTest do | ||
use Qrstorage.DataCase | ||
|
||
alias Qrstorage.QrCodes.QrCodeMigrationHelper | ||
alias Qrstorage.QrCodes.QrCode | ||
|
||
describe "add_deleter_after_months/1" do | ||
test "calculates the delete after duration for qr_codes based on insert_at and delete_after date" do | ||
Enum.each([1, 6, 12], fn month -> | ||
qr_code = qr_code_fixture() | ||
|
||
# we need to manually tore the old delete_after date. the delete_after date can no longer be set, since we are deprecating it:application | ||
updateDeleteAfterDate(qr_code, Timex.shift(Timex.now(), months: month)) | ||
QrCodeMigrationHelper.add_delete_after_months(Qrstorage.Repo) | ||
|
||
qr_code = QrCodes.get_qr_code!(qr_code.id) | ||
assert qr_code.delete_after_months == month | ||
end) | ||
end | ||
|
||
test "calculates the delete after duration for qr_codes based on insert_at and delete_after date for link codes" do | ||
qr_code = qr_code_fixture() | ||
updateDeleteAfterDate(qr_code, Timex.shift(Timex.now(), hours: 1)) | ||
QrCodeMigrationHelper.add_delete_after_months(Qrstorage.Repo) | ||
|
||
qr_code = QrCodes.get_qr_code!(qr_code.id) | ||
assert qr_code.delete_after_months == 0 | ||
end | ||
|
||
test "calculates the delete after duration for qr_codes based on insert_at and delete_after date for infinity codes" do | ||
qr_code = qr_code_fixture() | ||
updateDeleteAfterDate(qr_code, Timex.end_of_year(QrCode.max_delete_after_year())) | ||
QrCodeMigrationHelper.add_delete_after_months(Qrstorage.Repo) | ||
|
||
qr_code = QrCodes.get_qr_code!(qr_code.id) | ||
assert qr_code.delete_after_months == 36 | ||
end | ||
end | ||
|
||
defp updateDeleteAfterDate(qr_code, date) do | ||
qr_code | ||
|> cast(%{delete_after: date}, [:delete_after]) | ||
|> Repo.update!() | ||
end | ||
end |
Oops, something went wrong.