Skip to content

Commit 4c280c8

Browse files
committed
chore: make migraiton tests faster
1 parent 428dd22 commit 4c280c8

4 files changed

Lines changed: 236 additions & 193 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# The directory Mix will write compiled artifacts to.
66
/_build/
77

8+
/tmp/
9+
810
# If you run "mix test --cover", coverage assets end up here.
911
/cover/
1012

lib/migration_generator/migration_generator.ex

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ defmodule AshSqlite.MigrationGenerator do
238238
case to_install do
239239
[{ext_name, version, _up_fn, _down_fn}] ->
240240
{"install_#{ext_name}_v#{version}",
241-
"#{timestamp(true)}_install_#{ext_name}_v#{version}_extension"}
241+
"#{timestamp()}_install_#{ext_name}_v#{version}_extension"}
242242

243243
[single] ->
244-
{"install_#{single}", "#{timestamp(true)}_install_#{single}_extension"}
244+
{"install_#{single}", "#{timestamp()}_install_#{single}_extension"}
245245

246246
multiple ->
247247
{"install_#{Enum.count(multiple)}_extensions",
248-
"#{timestamp(true)}_install_#{Enum.count(multiple)}_extensions"}
248+
"#{timestamp()}_install_#{Enum.count(multiple)}_extensions"}
249249
end
250250

251251
migration_file =
@@ -855,7 +855,7 @@ defmodule AshSqlite.MigrationGenerator do
855855

856856
{migration_name, last_part} =
857857
if opts.name do
858-
{"#{timestamp(true)}_#{opts.name}", "#{opts.name}"}
858+
{"#{timestamp()}_#{opts.name}", "#{opts.name}"}
859859
else
860860
count =
861861
migration_path
@@ -878,7 +878,7 @@ defmodule AshSqlite.MigrationGenerator do
878878
|> Enum.max(fn -> 0 end)
879879
|> Kernel.+(1)
880880

881-
{"#{timestamp(true)}_migrate_resources#{count}", "migrate_resources#{count}"}
881+
{"#{timestamp()}_migrate_resources#{count}", "migrate_resources#{count}"}
882882
end
883883

884884
migration_file =
@@ -2115,12 +2115,34 @@ defmodule AshSqlite.MigrationGenerator do
21152115
end
21162116
end
21172117

2118-
defp timestamp(require_unique? \\ false) do
2119-
# Alright, this is silly I know. But migration ids need to be unique
2120-
# and "synthesizing" that behavior is significantly more annoying than
2121-
# just waiting a bit, ensuring the migration versions are unique.
2122-
if require_unique?, do: :timer.sleep(1500)
2118+
defp timestamp do
21232119
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
2120+
current = "#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
2121+
2122+
last = Process.get(:ash_sqlite_last_migration_timestamp)
2123+
2124+
result =
2125+
if last && current <= last do
2126+
increment_timestamp(last)
2127+
else
2128+
current
2129+
end
2130+
2131+
Process.put(:ash_sqlite_last_migration_timestamp, result)
2132+
result
2133+
end
2134+
2135+
defp increment_timestamp(timestamp) do
2136+
<<y::binary-4, m::binary-2, d::binary-2, hh::binary-2, mm::binary-2, ss::binary-2>> =
2137+
timestamp
2138+
2139+
seconds =
2140+
:calendar.datetime_to_gregorian_seconds({
2141+
{String.to_integer(y), String.to_integer(m), String.to_integer(d)},
2142+
{String.to_integer(hh), String.to_integer(mm), String.to_integer(ss)}
2143+
})
2144+
2145+
{{y, m, d}, {hh, mm, ss}} = :calendar.gregorian_seconds_to_datetime(seconds + 1)
21242146
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
21252147
end
21262148

test/dev_migrations_test.exs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
defmodule AshSqlite.DevMigrationsTest do
66
use AshSqlite.RepoCase, async: false
77
@moduletag :migration
8+
@moduletag :tmp_dir
89

910
alias Ecto.Adapters.SQL.Sandbox
1011

11-
setup do
12+
setup %{tmp_dir: tmp_dir} do
1213
current_shell = Mix.shell()
1314

1415
:ok = Mix.shell(Mix.Shell.Process)
@@ -19,6 +20,11 @@ defmodule AshSqlite.DevMigrationsTest do
1920

2021
Sandbox.checkout(AshSqlite.DevTestRepo)
2122
Sandbox.mode(AshSqlite.DevTestRepo, {:shared, self()})
23+
24+
%{
25+
snapshot_path: Path.join(tmp_dir, "snapshots"),
26+
migration_path: Path.join(tmp_dir, "migrations")
27+
}
2228
end
2329

2430
defmacrop defresource(mod, do: body) do
@@ -103,20 +109,19 @@ defmodule AshSqlite.DevMigrationsTest do
103109
Enum.each(new_migration_files, &File.rm!(Path.join(migrations_dev_path, &1)))
104110
end
105111

106-
# Clean up test directories
107-
File.rm_rf!("test_snapshots_path")
108-
File.rm_rf!("test_migration_path")
109-
110112
try do
111113
AshSqlite.DevTestRepo.query!("DROP TABLE IF EXISTS posts")
112-
rescue
113-
_ -> :ok
114+
catch
115+
_, _ -> :ok
114116
end
115117
end)
116118
end
117119

118120
describe "--dev option" do
119-
test "generates dev migration" do
121+
test "generates dev migration", %{
122+
snapshot_path: snapshot_path,
123+
migration_path: migration_path
124+
} do
120125
defposts do
121126
attributes do
122127
uuid_primary_key(:id)
@@ -127,32 +132,35 @@ defmodule AshSqlite.DevMigrationsTest do
127132
defdomain([Post])
128133

129134
AshSqlite.MigrationGenerator.generate(Domain,
130-
snapshot_path: "test_snapshots_path",
131-
migration_path: "test_migration_path",
135+
snapshot_path: snapshot_path,
136+
migration_path: migration_path,
132137
dev: true
133138
)
134139

135140
assert [dev_file] =
136-
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
141+
Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
137142

138143
assert String.contains?(dev_file, "_dev.exs")
139144
contents = File.read!(dev_file)
140145

141146
AshSqlite.MigrationGenerator.generate(Domain,
142-
snapshot_path: "test_snapshots_path",
143-
migration_path: "test_migration_path",
147+
snapshot_path: snapshot_path,
148+
migration_path: migration_path,
144149
auto_name: true
145150
)
146151

147152
assert [file] =
148-
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
153+
Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
149154

150155
refute String.contains?(file, "_dev.exs")
151156

152157
assert contents == File.read!(file)
153158
end
154159

155-
test "removes dev migrations when generating regular migrations" do
160+
test "removes dev migrations when generating regular migrations", %{
161+
snapshot_path: snapshot_path,
162+
migration_path: migration_path
163+
} do
156164
defposts do
157165
attributes do
158166
uuid_primary_key(:id)
@@ -164,31 +172,34 @@ defmodule AshSqlite.DevMigrationsTest do
164172

165173
# Generate dev migration first
166174
AshSqlite.MigrationGenerator.generate(Domain,
167-
snapshot_path: "test_snapshots_path",
168-
migration_path: "test_migration_path",
175+
snapshot_path: snapshot_path,
176+
migration_path: migration_path,
169177
dev: true
170178
)
171179

172180
assert [dev_file] =
173-
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
181+
Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
174182

175183
assert String.contains?(dev_file, "_dev.exs")
176184

177185
# Generate regular migration - should remove dev migration
178186
AshSqlite.MigrationGenerator.generate(Domain,
179-
snapshot_path: "test_snapshots_path",
180-
migration_path: "test_migration_path",
187+
snapshot_path: snapshot_path,
188+
migration_path: migration_path,
181189
auto_name: true
182190
)
183191

184192
# Should only have regular migration now
185-
files = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
193+
files = Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
186194
assert length(files) == 1
187195
assert [regular_file] = files
188196
refute String.contains?(regular_file, "_dev.exs")
189197
end
190198

191-
test "requires name when not using dev option" do
199+
test "requires name when not using dev option", %{
200+
snapshot_path: snapshot_path,
201+
migration_path: migration_path
202+
} do
192203
defposts do
193204
attributes do
194205
uuid_primary_key(:id)
@@ -200,8 +211,8 @@ defmodule AshSqlite.DevMigrationsTest do
200211

201212
assert_raise RuntimeError, ~r/Name must be provided/, fn ->
202213
AshSqlite.MigrationGenerator.generate(Domain,
203-
snapshot_path: "test_snapshots_path",
204-
migration_path: "test_migration_path"
214+
snapshot_path: snapshot_path,
215+
migration_path: migration_path
205216
)
206217
end
207218
end

0 commit comments

Comments
 (0)