Skip to content

Commit a57d508

Browse files
committed
test: make migration generator tests 95% faster
fixes #693
1 parent 45d139a commit a57d508

File tree

4 files changed

+550
-435
lines changed

4 files changed

+550
-435
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# The directory Mix downloads your dependencies sources to.
1212
/deps/
1313

14+
/tmp/
15+
1416
# Where third-party dependencies like ExDoc output generated docs.
1517
/doc/
1618

lib/migration_generator/migration_generator.ex

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ defmodule AshPostgres.MigrationGenerator do
268268
{module, migration_name} =
269269
case to_install do
270270
[{ext_name, version, _up_fn, _down_fn}] ->
271-
{"install_#{ext_name}_v#{version}_#{timestamp(true)}",
272-
"#{timestamp(true)}_install_#{ext_name}_v#{version}_extension#{dev}"}
271+
{"install_#{ext_name}_v#{version}_#{timestamp()}",
272+
"#{timestamp()}_install_#{ext_name}_v#{version}_extension#{dev}"}
273273

274274
["ash_functions"] ->
275-
{"install_ash_functions_extension_#{AshPostgres.MigrationGenerator.AshFunctions.latest_version()}_#{timestamp(true)}",
276-
"#{timestamp(true)}_install_ash_functions_extension_#{AshPostgres.MigrationGenerator.AshFunctions.latest_version()}"}
275+
{"install_ash_functions_extension_#{AshPostgres.MigrationGenerator.AshFunctions.latest_version()}_#{timestamp()}",
276+
"#{timestamp()}_install_ash_functions_extension_#{AshPostgres.MigrationGenerator.AshFunctions.latest_version()}"}
277277

278278
_multiple ->
279279
migration_path = migration_path(opts, repo, false)
@@ -303,7 +303,7 @@ defmodule AshPostgres.MigrationGenerator do
303303
|> Kernel.+(1)
304304

305305
{"#{opts.name}_extensions_#{count}",
306-
"#{timestamp(true)}_#{opts.name}_extensions_#{count}#{dev}"}
306+
"#{timestamp()}_#{opts.name}_extensions_#{count}#{dev}"}
307307
else
308308
count =
309309
migration_path
@@ -327,7 +327,7 @@ defmodule AshPostgres.MigrationGenerator do
327327
|> Kernel.+(1)
328328

329329
{"migrate_resources_extensions_#{count}",
330-
"#{timestamp(true)}_migrate_resources_extensions_#{count}#{dev}"}
330+
"#{timestamp()}_migrate_resources_extensions_#{count}#{dev}"}
331331
end
332332
end
333333

@@ -1107,7 +1107,7 @@ defmodule AshPostgres.MigrationGenerator do
11071107

11081108
{migration_name, last_part} =
11091109
if opts.name do
1110-
{"#{timestamp(true)}_#{opts.name}", "#{opts.name}"}
1110+
{"#{timestamp()}_#{opts.name}", "#{opts.name}"}
11111111
else
11121112
count =
11131113
migration_path
@@ -1130,7 +1130,7 @@ defmodule AshPostgres.MigrationGenerator do
11301130
|> Enum.max(fn -> 0 end)
11311131
|> Kernel.+(1)
11321132

1133-
{"#{timestamp(true)}_migrate_resources#{count}", "migrate_resources#{count}"}
1133+
{"#{timestamp()}_migrate_resources#{count}", "migrate_resources#{count}"}
11341134
end
11351135

11361136
migration_file =
@@ -3016,12 +3016,34 @@ defmodule AshPostgres.MigrationGenerator do
30163016
end
30173017
end
30183018

3019-
defp timestamp(require_unique? \\ false) do
3020-
# Alright, this is silly I know. But migration ids need to be unique
3021-
# and "synthesizing" that behavior is significantly more annoying than
3022-
# just waiting a bit, ensuring the migration versions are unique.
3023-
if require_unique?, do: :timer.sleep(1500)
3019+
defp timestamp do
30243020
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
3021+
current = "#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
3022+
3023+
last = Process.get(:ash_postgres_last_migration_timestamp)
3024+
3025+
result =
3026+
if last && current <= last do
3027+
increment_timestamp(last)
3028+
else
3029+
current
3030+
end
3031+
3032+
Process.put(:ash_postgres_last_migration_timestamp, result)
3033+
result
3034+
end
3035+
3036+
defp increment_timestamp(timestamp) do
3037+
<<y::binary-4, m::binary-2, d::binary-2, hh::binary-2, mm::binary-2, ss::binary-2>> =
3038+
timestamp
3039+
3040+
seconds =
3041+
:calendar.datetime_to_gregorian_seconds({
3042+
{String.to_integer(y), String.to_integer(m), String.to_integer(d)},
3043+
{String.to_integer(hh), String.to_integer(mm), String.to_integer(ss)}
3044+
})
3045+
3046+
{{y, m, d}, {hh, mm, ss}} = :calendar.gregorian_seconds_to_datetime(seconds + 1)
30253047
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
30263048
end
30273049

0 commit comments

Comments
 (0)