-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathrunner.ex
More file actions
513 lines (407 loc) · 15.4 KB
/
runner.ex
File metadata and controls
513 lines (407 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
defmodule Ecto.Migration.Runner do
@moduledoc false
use Agent, restart: :temporary
require Logger
alias Ecto.Migration.Table
alias Ecto.Migration.Index
alias Ecto.Migration.Constraint
alias Ecto.Migration.Command
@doc """
Runs the given migration.
"""
def run(repo, config, version, module, direction, operation, migrator_direction, opts) do
level = Keyword.get(opts, :log, :info)
sql = Keyword.get(opts, :log_migrations_sql, false)
log = %{level: level, sql: sql}
args = {self(), repo, config, module, direction, migrator_direction, log}
{:ok, runner} = DynamicSupervisor.start_child(Ecto.MigratorSupervisor, {__MODULE__, args})
metadata(runner, opts)
log(level, "== Running #{version} #{inspect(module)}.#{operation}/0 #{direction}")
{time, _} = :timer.tc(fn -> perform_operation(repo, module, operation) end)
log(level, "== Migrated #{version} in #{inspect(div(time, 100_000) / 10)}s")
after
stop()
end
@doc """
Stores the runner metadata.
"""
def metadata(runner, opts) do
prefix = opts[:prefix]
Process.put(:ecto_migration, %{runner: runner, prefix: prefix && to_string(prefix)})
end
@doc """
Starts the runner for the specified repo.
"""
def start_link({parent, repo, config, module, direction, migrator_direction, log}) do
Agent.start_link(fn ->
Process.link(parent)
%{
direction: direction,
repo: repo,
migration: module,
migrator_direction: migrator_direction,
command: nil,
subcommands: [],
log: log,
commands: [],
config: config
}
end)
end
@doc """
Stops the runner.
"""
def stop() do
Agent.stop(runner())
end
@doc """
Accesses the given repository configuration.
"""
def repo_config(key, default) do
Agent.get(runner(), &Keyword.get(&1.config, key, default))
end
@doc """
Returns the migrator command (up or down).
* forward + up: up
* forward + down: down
* forward + change: up
* backward + change: down
"""
def migrator_direction do
Agent.get(runner(), & &1.migrator_direction)
end
@doc """
Gets the repo for this migration
"""
def repo do
Agent.get(runner(), & &1.repo)
end
@doc """
Gets the prefix for this migration
"""
def prefix do
case Process.get(:ecto_migration) do
%{prefix: prefix} -> prefix
_ -> raise "could not find migration runner process for #{inspect(self())}"
end
end
@doc """
Executes queue migration commands.
Reverses the order commands are executed when doing a rollback
on a change/0 function and resets commands queue.
"""
def flush do
%{commands: commands, direction: direction, repo: repo, log: log, migration: migration} =
Agent.get_and_update(runner(), fn state -> {state, %{state | commands: []}} end)
commands = if direction == :backward, do: commands, else: Enum.reverse(commands)
for command <- commands do
execute_in_direction(repo, migration, direction, log, command)
end
end
@doc """
Queues command tuples or strings for execution.
Ecto.MigrationError will be raised when the server
is in `:backward` direction and `command` is irreversible.
"""
def execute(command) do
reply =
Agent.get_and_update(runner(), fn
%{command: nil} = state ->
{:ok, %{state | subcommands: [], commands: [command | state.commands]}}
%{command: _} = state ->
{:error, %{state | command: nil}}
end)
case reply do
:ok ->
:ok
:error ->
raise Ecto.MigrationError, "cannot execute nested commands"
end
end
@doc """
Starts a command.
"""
def start_command(command) do
reply =
Agent.get_and_update(runner(), fn
%{command: nil} = state ->
{:ok, %{state | command: command}}
%{command: _} = state ->
{:error, %{state | command: command}}
end)
case reply do
:ok ->
:ok
:error ->
raise Ecto.MigrationError, "cannot execute nested commands"
end
end
@doc """
Queues and clears current command. Must call `start_command/1` first.
"""
def end_command do
Agent.update(runner(), fn state ->
{operation, object} = state.command
command = {operation, object, Enum.reverse(state.subcommands)}
%{state | command: nil, subcommands: [], commands: [command | state.commands]}
end)
end
@doc """
Adds a subcommand to the current command. Must call `start_command/1` first.
"""
def subcommand(subcommand) do
reply =
Agent.get_and_update(runner(), fn
%{command: nil} = state ->
{:error, state}
state ->
{:ok, update_in(state.subcommands, &[subcommand | &1])}
end)
case reply do
:ok ->
:ok
:error ->
raise Ecto.MigrationError, message: "cannot execute command outside of block"
end
end
## Execute
defp execute_in_direction(repo, migration, :forward, log, %Command{up: up}) do
log_and_execute_ddl(repo, migration, log, up)
end
defp execute_in_direction(repo, migration, :forward, log, command) do
log_and_execute_ddl(repo, migration, log, command)
end
defp execute_in_direction(repo, migration, :backward, log, %Command{down: down}) do
log_and_execute_ddl(repo, migration, log, down)
end
defp execute_in_direction(repo, migration, :backward, log, command) do
if reversed = reverse(command) do
log_and_execute_ddl(repo, migration, log, reversed)
else
raise Ecto.MigrationError,
message:
"cannot reverse migration command: #{command(command)}. " <>
"You will need to explicitly define up/0 and down/0 in your migration"
end
end
defp reverse({:create, %Index{} = index}),
do: {:drop, index, :restrict}
defp reverse({:create_if_not_exists, %Index{} = index}),
do: {:drop_if_exists, index, :restrict}
defp reverse({:drop, %Index{} = index, _}),
do: {:create, index}
defp reverse({:drop_if_exists, %Index{} = index, _}),
do: {:create_if_not_exists, index}
defp reverse({:rename, %Index{} = index, new_name}),
do: {:rename, %{index | name: new_name}, index.name}
defp reverse({:create, %Table{} = table, _columns}),
do: {:drop, table, :restrict}
defp reverse({:create_if_not_exists, %Table{} = table, _columns}),
do: {:drop_if_exists, table, :restrict}
defp reverse({:rename, %Table{} = table_current, %Table{} = table_new}),
do: {:rename, table_new, table_current}
defp reverse({:rename, %Table{} = table, current_column, new_column}),
do: {:rename, table, new_column, current_column}
defp reverse({:alter, %Table{} = table, changes}) do
if reversed = table_reverse(changes, []) do
{:alter, table, reversed}
end
end
# It is not a good idea to reverse constraints because
# we can't guarantee data integrity when applying them back.
defp reverse({:create_if_not_exists, %Constraint{} = constraint}),
do: {:drop_if_exists, constraint, :restrict}
defp reverse({:create, %Constraint{} = constraint}),
do: {:drop, constraint, :restrict}
defp reverse(_command), do: false
defp table_reverse([{:remove, name, type, opts} | t], acc) do
table_reverse(t, [{:add, name, type, opts} | acc])
end
defp table_reverse([{:modify, name, type, opts} | t], acc) do
case opts[:from] do
nil ->
false
{reverse_type, from_opts} when is_list(from_opts) ->
reverse_from = {type, Keyword.delete(opts, :from)}
reverse_opts = Keyword.put(from_opts, :from, reverse_from)
table_reverse(t, [{:modify, name, reverse_type, reverse_opts} | acc])
reverse_type ->
reverse_opts = Keyword.put(opts, :from, type)
table_reverse(t, [{:modify, name, reverse_type, reverse_opts} | acc])
end
end
defp table_reverse([{:add, name, type, _opts} | t], acc) do
table_reverse(t, [{:remove, name, type, []} | acc])
end
defp table_reverse([_ | _], _acc) do
false
end
defp table_reverse([], acc) do
acc
end
## Helpers
defp perform_operation(repo, module, operation) do
if function_exported?(repo, :in_transaction?, 0) and repo.in_transaction?() do
if function_exported?(module, :after_begin, 0) do
module.after_begin()
flush()
end
apply(module, operation, [])
flush()
if function_exported?(module, :before_commit, 0) do
module.before_commit()
flush()
end
else
apply(module, operation, [])
flush()
end
end
defp runner do
case Process.get(:ecto_migration) do
%{runner: runner} -> runner
_ -> raise "could not find migration runner process for #{inspect(self())}"
end
end
defp log_and_execute_ddl(repo, migration, log, {instruction, %Index{} = index}) do
maybe_warn_index_ddl_transaction(index, migration)
maybe_warn_index_migration_lock(index, repo, migration)
log_and_execute_ddl(repo, log, {instruction, index})
end
defp log_and_execute_ddl(repo, _migration, log, command) do
log_and_execute_ddl(repo, log, command)
end
defp log_and_execute_ddl(_repo, _log, func) when is_function(func, 0) do
func.()
:ok
end
defp log_and_execute_ddl(repo, %{level: level, sql: sql}, command) do
log(level, command(command))
meta = Ecto.Adapter.lookup_meta(repo.get_dynamic_repo())
{:ok, logs} = repo.__adapter__().execute_ddl(meta, command, timeout: :infinity, log: sql)
Enum.each(logs, fn {ddl_log_level, message, metadata} ->
ddl_log(ddl_log_level, level, message, metadata)
end)
:ok
end
defp ddl_log(_level, false, _msg, _metadata), do: :ok
defp ddl_log(level, _, msg, metadata), do: log(level, msg, metadata)
defp log(level, msg, metadata \\ [])
defp log(false, _msg, _metadata), do: :ok
defp log(true, msg, metadata), do: Logger.log(:info, msg, metadata)
defp log(level, msg, metadata), do: Logger.log(level, msg, metadata)
defp maybe_warn_index_ddl_transaction(%{concurrently: true} = index, migration) do
migration_config = migration.__migration__()
if not migration_config[:disable_ddl_transaction] do
IO.warn(
"""
Migration #{inspect(migration)} has set index `#{index.name}` on table \
`#{index.table}` to concurrently but did not disable ddl transaction. \
Please set:
use Ecto.Migration
@disable_ddl_transaction true
""",
[]
)
end
end
defp maybe_warn_index_ddl_transaction(_index, _migration), do: :ok
defp maybe_warn_index_migration_lock(%{concurrently: true} = index, repo, migration) do
migration_lock_disabled = migration.__migration__()[:disable_migration_lock]
lock_strategy = repo.config()[:migration_lock]
adapter = repo.__adapter__()
case {migration_lock_disabled, adapter, lock_strategy} do
{false, Ecto.Adapters.Postgres, :pg_advisory_lock} ->
:ok
{false, Ecto.Adapters.Postgres, _} ->
IO.warn(
"""
Migration #{inspect(migration)} has set index `#{index.name}` on table \
`#{index.table}` to concurrently but did not disable migration lock. \
Please set:
use Ecto.Migration
@disable_migration_lock true
Alternatively, consider using advisory locks during migrations in the \
repo configuration:
config #{inspect(repo)}, migration_lock: :pg_advisory_lock
""",
[]
)
{false, _adapter, _migration_lock} ->
IO.warn(
"""
Migration #{inspect(migration)} has set index `#{index.name}` on table \
`#{index.table}` to concurrently but did not disable migration lock. \
Please set:
use Ecto.Migration
@disable_migration_lock true
""",
[]
)
_ ->
:ok
end
end
defp maybe_warn_index_migration_lock(_index, _repo, _migration), do: :ok
defp command(ddl) when is_binary(ddl) or is_list(ddl),
do: "execute #{inspect(ddl)}"
defp command({:create, %Table{} = table, _}) do
if repo_config(:create_unlogged_tables, false) do
"create unlogged table #{quote_name(table.prefix, table.name)}"
else
"create table #{quote_name(table.prefix, table.name)}"
end
end
defp command({:create_if_not_exists, %Table{} = table, _}) do
if repo_config(:create_unlogged_tables, false) do
"create unlogged table if not exists #{quote_name(table.prefix, table.name)}"
else
"create table if not exists #{quote_name(table.prefix, table.name)}"
end
end
defp command({:alter, %Table{} = table, _}),
do: "alter table #{quote_name(table.prefix, table.name)}"
defp command({:drop, %Table{} = table, mode}),
do: "drop table #{quote_name(table.prefix, table.name)}#{drop_mode(mode)}"
defp command({:drop_if_exists, %Table{} = table, mode}),
do: "drop table if exists #{quote_name(table.prefix, table.name)}#{drop_mode(mode)}"
defp command({:create, %Index{} = index}),
do: "create index #{quote_name(index.prefix, index.name)}"
defp command({:create_if_not_exists, %Index{} = index}),
do: "create index if not exists #{quote_name(index.prefix, index.name)}"
defp command({:drop, %Index{} = index, mode}),
do: "drop index #{quote_name(index.prefix, index.name)}#{drop_mode(mode)}"
defp command({:drop_if_exists, %Index{} = index, mode}),
do: "drop index if exists #{quote_name(index.prefix, index.name)}#{drop_mode(mode)}"
defp command({:rename, %Index{} = index_current, new_name}),
do: "rename index #{quote_name(index_current.prefix, index_current.name)} to #{new_name}"
defp command({:rename, %Table{} = current_table, %Table{} = new_table}),
do:
"rename table #{quote_name(current_table.prefix, current_table.name)} to #{quote_name(new_table.prefix, new_table.name)}"
defp command({:rename, %Table{} = table, current_column, new_column}),
do:
"rename column #{current_column} to #{new_column} on table #{quote_name(table.prefix, table.name)}"
defp command({:create, %Constraint{check: nil, exclude: nil}}),
do: raise(ArgumentError, "a constraint must have either a check or exclude option")
defp command({:create, %Constraint{check: check, exclude: exclude}})
when is_binary(check) and is_binary(exclude),
do: raise(ArgumentError, "a constraint must not have both check and exclude options")
defp command({:create, %Constraint{check: check} = constraint}) when is_binary(check),
do:
"create check constraint #{constraint.name} on table #{quote_name(constraint.prefix, constraint.table)}"
defp command({:create, %Constraint{exclude: exclude} = constraint}) when is_binary(exclude),
do:
"create exclude constraint #{constraint.name} on table #{quote_name(constraint.prefix, constraint.table)}"
defp command({:drop, %Constraint{} = constraint, _}),
do:
"drop constraint #{constraint.name} from table #{quote_name(constraint.prefix, constraint.table)}"
defp command({:drop_if_exists, %Constraint{} = constraint, _}),
do:
"drop constraint if exists #{constraint.name} from table #{quote_name(constraint.prefix, constraint.table)}"
defp drop_mode(:restrict), do: ""
defp drop_mode(:cascade), do: " cascade"
defp quote_name(nil, name), do: quote_name(name)
defp quote_name(prefix, name), do: quote_name(prefix) <> "." <> quote_name(name)
defp quote_name(name) when is_atom(name), do: quote_name(Atom.to_string(name))
defp quote_name(name), do: name
end