Skip to content

Commit 69ea3a8

Browse files
committed
WIP and notes
1 parent a4d8e32 commit 69ea3a8

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/fun_with_flags/store/persistent/ecto/record.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule FunWithFlags.Store.Persistent.Ecto.Record do
1313
field :gate_type, :string
1414
field :target, :string
1515
field :enabled, :boolean
16-
timestamps()
16+
# timestamps()
1717
end
1818

1919
@fields [:flag_name, :gate_type, :target, :enabled]

priv/ecto_repo/migrations/00000000000000_create_feature_flags_table.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule FunWithFlags.Dev.EctoRepo.Migrations.CreateFeatureFlagsTable do
1212
add :gate_type, :string, null: false
1313
add :target, :string, null: false
1414
add :enabled, :boolean, null: false
15-
timestamps()
15+
# timestamps()
1616
end
1717

1818
create index(

priv/ecto_repo/migrations/00000000000002_add_timestamps.exs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,28 @@ defmodule FunWithFlags.Dev.EctoRepo.Migrations.EnsureColumnsAreNotNull do
2222
# timestamp columns exist, do nothing
2323
true
2424

25+
# !! This doesn't work in postgres because the error fails the transaction,
26+
# and then any other statement is ignored.
27+
# It works in MySQL though.
28+
#
2529
{:error, _} ->
30+
# We must set a default value, because Ecto timestamps come with a
31+
# NOT NULL constraint, and without a default the the migration will
32+
# fail if the table already contains records.
33+
#
34+
# WARNING: adding a new column with a default value is a blocking operation
35+
# on Postgres and can be problematic on MySQL unless a special DDL is used.
36+
# This is usually fine for small tables, but it requires special care and
37+
# planning for tables that contain a lot of flag data.
38+
#
39+
current_time = NaiveDateTime.utc_now()
40+
2641
# Assume error is from timestamp columns not existing, run migration
2742
alter table(:fun_with_flags_toggles) do
28-
timestamps()
43+
# timestamps(default: current_time)
44+
45+
# Alternative: declare the timestamps as nullable? Does Ecto complain?
46+
timestamps null: true
2947
end
3048
end
3149
end

0 commit comments

Comments
 (0)