Skip to content

Commit f0efda6

Browse files
authored
fix: generate_migrations --dev duplicating migration files (#173)
This issue was due to `get_existing_snapshot` ignoring dev migrations because they don't comform to `{timestamp.json}`. I fixed by making it closer to its postgres counterpart: not trying to parse the timestamp and instead comparing the filenames, which works the same until timestamps change their numbers of digits. I'll think about how to introduce some reuse, because postgres has the same feature except its getting more love.
1 parent 3f6044d commit f0efda6

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,25 +2006,13 @@ defmodule AshSqlite.MigrationGenerator do
20062006
snapshot_folder
20072007
|> File.ls!()
20082008
|> Enum.filter(&String.ends_with?(&1, ".json"))
2009-
|> Enum.map(&String.trim_trailing(&1, ".json"))
2010-
|> Enum.map(&Integer.parse/1)
2011-
|> Enum.filter(fn
2012-
{_int, remaining} ->
2013-
remaining == ""
2014-
2015-
:error ->
2016-
false
2017-
end)
2018-
|> Enum.map(&elem(&1, 0))
20192009
|> case do
20202010
[] ->
20212011
get_old_snapshot(folder, snapshot)
20222012

2023-
timestamps ->
2024-
timestamp = Enum.max(timestamps)
2025-
snapshot_file = Path.join(snapshot_folder, "#{timestamp}.json")
2026-
2027-
snapshot_file
2013+
snapshot_files ->
2014+
snapshot_folder
2015+
|> Path.join(Enum.max(snapshot_files))
20282016
|> File.read!()
20292017
|> load_snapshot()
20302018
end

test/migration_generator_test.exs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,67 @@ defmodule AshSqlite.MigrationGeneratorTest do
192192
end
193193
end
194194

195+
describe "dev migrations" do
196+
setup do
197+
on_exit(fn ->
198+
File.rm_rf!("test_snapshots_path")
199+
File.rm_rf!("test_migration_path")
200+
end)
201+
202+
defposts do
203+
identities do
204+
identity(:title, [:title])
205+
end
206+
207+
attributes do
208+
uuid_primary_key(:id)
209+
attribute(:title, :string)
210+
end
211+
end
212+
213+
defdomain([Post])
214+
215+
Mix.shell(Mix.Shell.Process)
216+
217+
AshSqlite.MigrationGenerator.generate(Domain,
218+
snapshot_path: "test_snapshots_path",
219+
migration_path: "test_migration_path",
220+
quiet: true,
221+
format: false,
222+
auto_name: true,
223+
dev: true
224+
)
225+
226+
:ok
227+
end
228+
229+
test "running it again doesn't create a new file" do
230+
defposts do
231+
identities do
232+
identity(:title, [:title])
233+
end
234+
235+
attributes do
236+
uuid_primary_key(:id)
237+
attribute(:title, :string)
238+
end
239+
end
240+
241+
defdomain([Post])
242+
243+
AshSqlite.MigrationGenerator.generate(Domain,
244+
snapshot_path: "test_snapshots_path",
245+
migration_path: "test_migration_path",
246+
quiet: true,
247+
format: false,
248+
auto_name: true,
249+
dev: true
250+
)
251+
252+
assert [_] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
253+
end
254+
end
255+
195256
describe "creating follow up migrations" do
196257
setup do
197258
on_exit(fn ->
@@ -225,6 +286,31 @@ defmodule AshSqlite.MigrationGeneratorTest do
225286
:ok
226287
end
227288

289+
test "without change" do
290+
defposts do
291+
identities do
292+
identity(:title, [:title])
293+
end
294+
295+
attributes do
296+
uuid_primary_key(:id)
297+
attribute(:title, :string)
298+
end
299+
end
300+
301+
defdomain([Post])
302+
303+
AshSqlite.MigrationGenerator.generate(Domain,
304+
snapshot_path: "test_snapshots_path",
305+
migration_path: "test_migration_path",
306+
quiet: true,
307+
format: false,
308+
auto_name: true
309+
)
310+
311+
assert [_] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
312+
end
313+
228314
test "when renaming an index, it is properly renamed" do
229315
defposts do
230316
sqlite do

0 commit comments

Comments
 (0)