/
migration.ex
972 lines (730 loc) · 29.4 KB
/
migration.ex
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
defmodule Ecto.Migration do
@moduledoc """
Migrations are used to modify your database schema over time.
This module provides many helpers for migrating the database,
allowing developers to use Elixir to alter their storage in
a way that is database independent.
Here is an example:
defmodule MyRepo.Migrations.AddWeatherTable do
use Ecto.Migration
def up do
create table("weather") do
add :city, :string, size: 40
add :temp_lo, :integer
add :temp_hi, :integer
add :prcp, :float
timestamps()
end
end
def down do
drop table("weather")
end
end
Note migrations have an `up/0` and `down/0` instructions, where
`up/0` is used to update your database and `down/0` rolls back
the prompted changes.
Ecto creates a table (see the `:migration_source` configuration)
in the database in order to keep track of migrations and will add
an entry to this table for each migration you define. Ecto also
locks the table when adding/removing entries, guaranteeing two
different servers cannot run the same migration at the same time.
Ecto provides some mix tasks to help developers work with migrations:
* `mix ecto.gen.migration add_weather_table` - generates a
migration that the user can fill in with particular commands
* `mix ecto.migrate` - migrates a repository
* `mix ecto.rollback` - rolls back a particular migration
Run the `mix help COMMAND` for more information.
## Change
Migrations can also be automatically reversible by implementing
`change/0` instead of `up/0` and `down/0`. For example, the
migration above can be written as:
defmodule MyRepo.Migrations.AddWeatherTable do
use Ecto.Migration
def change do
create table("weather") do
add :city, :string, size: 40
add :temp_lo, :integer
add :temp_hi, :integer
add :prcp, :float
timestamps()
end
end
end
Notice not all commands are reversible though. Trying to rollback
a non-reversible command will raise an `Ecto.MigrationError`.
A notable command in this regard is `execute/2`, which makes
plain SQL migrations steps reversible. The first string argument is run on
forward migration, and the second on reverse.
If `up/0` and `down/0` are implemented they take precedence, and
`change/0` isn't invoked.
## Field Types
The Ecto primitive types are mapped to the appropriate database
type by the various database adapters. For example, `:string` is converted to
`:varchar`, `:binary` to `:bits` or `:blob`, and so on.
Similarly, you can pass any field type supported by your database
as long as it maps to an Ecto type. For instance, you can use `:text`,
`:varchar` or `:char` in your migrations as `add :field_name, :text`.
In your Ecto schema, they will all map to the same `:string` type.
Remember, atoms can contain arbitrary characters by enclosing in
double quotes the characters following the colon. So, if you want to use
field type with your database specific options, you can pass atoms containing
these options like `:"int unsigned"`, `:"time without time zone"`.
## Prefixes
Migrations support specifying a table prefix or index prefix which will
target either a schema if using Postgres, or a different database if using
MySQL. If no prefix is provided, the default schema or database is used.
Any reference declared in the table migration refers by default to the table
with the same declared prefix. The prefix is specified in the table options:
def up do
create table("weather", prefix: "north_america") do
add :city, :string, size: 40
add :temp_lo, :integer
add :temp_hi, :integer
add :prcp, :float
add :group_id, references(:groups)
timestamps()
end
create index("weather", [:city], prefix: "north_america")
end
Note: if using MySQL with a prefixed table, you must use the same prefix
for the references since cross database references are not supported.
For both MySQL and Postgres with a prefixed table, you must use the same
prefix for the index field to ensure you index the prefix qualified table.
## Transactions
For PostgreSQL, Ecto always runs migrations inside a transaction, but that's not
always desired: for example, you cannot create/drop indexes concurrently inside
a transaction (see the [PostgreSQL docs](http://www.postgresql.org/docs/9.2/static/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY)).
Migrations can be forced to run outside a transaction by setting the
`@disable_ddl_transaction` module attribute to `true`:
defmodule MyRepo.Migrations.CreateIndexes do
use Ecto.Migration
@disable_ddl_transaction true
def change do
create index("posts", [:slug], concurrently: true)
end
end
Since running migrations outside a transaction can be dangerous, consider
performing very few operations in such migrations.
See the `index/3` function for more information on creating/dropping indexes
concurrently.
## Comments
Migrations where you create or alter a table support specifying table
and column comments, the same can be done when creating constraints
and indexes. At the moment there is support only for Postgres.
def up do
create index("posts", [:name], comment: "Index Comment")
create constraint("products", "price_must_be_positive", check: "price > 0", comment: "Index Comment")
create table("weather", prefix: "north_america", comment: "Table Comment") do
add :city, :string, size: 40, comment: "Column Comment"
timestamps()
end
end
## Repo configuration
The following migration configurations are available for under
a given repository.
* `:migration_source` - Version numbers of migrations will be saved in
`schema_migrations` table but you can configure the table via:
config :app, App.Repo, migration_source: "my_migrations"
* `:migration_primary_key` - Ecto uses the `:id` column with type
`:bigserial` but you can configure it via:
config :app, App.Repo, migration_primary_key: [name: :uuid, type: :binary_id]
* `:migration_timestamps` - Ecto uses type `:naive_datetime` but you
can configure it via:
config :app, App.Repo, migration_timestamps: [type: :utc_datetime]
* `:migration_lock` - Ecto will lock the migration table to handle concurrent
migrators using `FOR UPDATE` by default but you can configure it via:
config :app, App.Repo, migration_lock: nil
* `:migration_default_prefix` - Ecto defaults to `nil` for the database prefix for
migrations but you can configure it via:
config :app, App.Repo, migration_default_prefix: "kewl"
"""
defmodule Index do
@moduledoc """
Used internally by adapters.
To define an index in a migration, see `Ecto.Migration.index/3`
"""
defstruct table: nil,
prefix: nil,
name: nil,
columns: [],
unique: false,
concurrently: false,
using: nil,
where: nil,
comment: nil,
options: nil
@type t :: %__MODULE__{
table: String.t,
prefix: atom,
name: atom,
columns: [atom | String.t],
unique: boolean,
concurrently: boolean,
using: atom | String.t,
where: atom | String.t,
comment: String.t | nil,
options: String.t
}
end
defmodule Table do
@moduledoc """
Used internally by adapters.
To define a table in a migration, see `Ecto.Migration.table/2`
"""
defstruct name: nil, prefix: nil, comment: nil, primary_key: true, engine: nil, options: nil
@type t :: %__MODULE__{name: String.t, prefix: atom | nil, comment: String.t | nil, primary_key: boolean,
engine: atom, options: String.t}
end
defmodule Reference do
@moduledoc """
Used internally by adapters.
To define a reference in a migration, see `Ecto.Migration.references/2`
"""
defstruct name: nil, table: nil, column: :id, type: :bigserial, on_delete: :nothing, on_update: :nothing
@type t :: %__MODULE__{table: String.t, column: atom, type: atom, on_delete: atom, on_update: atom}
end
defmodule Constraint do
@moduledoc """
Used internally by adapters.
To define a constraint in a migration, see `Ecto.Migration.constraint/3`
"""
defstruct name: nil, table: nil, check: nil, exclude: nil, prefix: nil, comment: nil
@type t :: %__MODULE__{name: atom, table: String.t, prefix: atom | nil,
check: String.t | nil, exclude: String.t | nil, comment: String.t | nil}
end
defmodule Command do
@moduledoc """
Used internally by adapters.
This represents the up and down legs of a reversible raw command
that is usually define with `Ecto.Migration.execute/1`.
To define a reversible command in a migration, see `Ecto.Migration.execute/2`
"""
defstruct up: nil, down: nil
@type t :: %__MODULE__{up: String.t, down: String.t}
end
alias Ecto.Migration.Runner
@doc false
defmacro __using__(_) do
quote location: :keep do
import Ecto.Migration
@disable_ddl_transaction false
@before_compile Ecto.Migration
end
end
@doc false
defmacro __before_compile__(_env) do
quote do
def __migration__,
do: [disable_ddl_transaction: @disable_ddl_transaction]
end
end
@doc """
Creates a table.
By default, the table will also include a primary_key of name `:id`
and type `:bigserial`. Check `table/2` docs for more information.
## Examples
create table(:posts) do
add :title, :string, default: "Untitled"
add :body, :text
timestamps()
end
"""
defmacro create(object, do: block) do
expand_create(object, :create, block)
end
@doc """
Creates a table if it does not exist.
Works just like `create/2` but does not raise an error when table
already exists.
"""
defmacro create_if_not_exists(object, do: block) do
expand_create(object, :create_if_not_exists, block)
end
defp expand_create(object, command, block) do
quote do
table = %Table{} = unquote(object)
Runner.start_command({unquote(command), Ecto.Migration.__prefix__(table)})
if table.primary_key do
opts = Runner.repo_config(:migration_primary_key, [])
opts = Keyword.put(opts, :primary_key, true)
{name, opts} = Keyword.pop(opts, :name, :id)
{type, opts} = Keyword.pop(opts, :type, :bigserial)
add(name, type, opts)
end
unquote(block)
Runner.end_command
table
end
end
@doc """
Alters a table.
## Examples
alter table("posts") do
add :summary, :text
modify :title, :text
remove :views
end
"""
defmacro alter(object, do: block) do
quote do
table = %Table{} = unquote(object)
Runner.start_command({:alter, Ecto.Migration.__prefix__(table)})
unquote(block)
Runner.end_command
end
end
@doc """
Creates one of the following:
* an index
* a table with only an `:id` field
* a constraint
When reversing (in `change` running backward) indexes are only dropped if they
exist and no errors are raised. To enforce dropping an index use `drop/1`.
## Examples
create index("posts", [:name])
create table("version")
create constraint("products", "price_must_be_positive", check: "price > 0")
"""
def create(%Index{} = index) do
Runner.execute {:create, __prefix__(index)}
index
end
def create(%Constraint{} = constraint) do
Runner.execute {:create, __prefix__(constraint)}
constraint
end
def create(%Table{} = table) do
do_create table, :create
table
end
@doc """
Creates an index or a table with only `:id` field if one does not yet exist.
## Examples
create_if_not_exists index("posts", [:name])
create_if_not_exists table("version")
"""
def create_if_not_exists(%Index{} = index) do
Runner.execute {:create_if_not_exists, __prefix__(index)}
end
def create_if_not_exists(%Table{} = table) do
do_create table, :create_if_not_exists
end
defp do_create(table, command) do
columns =
if table.primary_key do
[{:add, :id, :bigserial, primary_key: true}]
else
[]
end
Runner.execute {command, __prefix__(table), columns}
end
@doc """
Drops one of the following:
* an index
* a table
* a constraint
## Examples
drop index("posts", [:name])
drop table("posts")
drop constraint("products", "price_must_be_positive")
"""
def drop(%{} = index_or_table_or_constraint) do
Runner.execute {:drop, __prefix__(index_or_table_or_constraint)}
index_or_table_or_constraint
end
@doc """
Drops a table or index if it exists.
Does not raise an error if table or index does not exist.
## Examples
drop_if_exists index("posts", [:name])
drop_if_exists table("posts")
"""
def drop_if_exists(%{} = index_or_table) do
Runner.execute {:drop_if_exists, __prefix__(index_or_table)}
index_or_table
end
@doc """
Returns a table struct that can be given on create, alter, etc.
## Examples
create table("products") do
add :name, :string
add :price, :decimal
end
drop table("products")
create table("products", primary_key: false) do
add :name, :string
add :price, :decimal
end
## Options
* `:primary_key` - when false, does not generate primary key on table creation
* `:engine` - customizes the table storage for supported databases. For MySQL,
the default is InnoDB
* `:prefix` - the prefix for the table
* `:options` - provide custom options that will be appended after generated
statement, for example "WITH", "INHERITS" or "ON COMMIT" clauses
"""
def table(name, opts \\ [])
def table(name, opts) when is_atom(name) do
table(Atom.to_string(name), opts)
end
def table(name, opts) when is_binary(name) and is_list(opts) do
struct(%Table{name: name}, opts)
end
@doc ~S"""
Returns an index struct that can be used on `create`, `drop`, etc.
Expects the table name as first argument and the index fields as
second. The field can be an atom, representing a column, or a
string representing an expression that is sent as is to the database.
Indexes are non-unique by default.
## Options
* `:name` - the name of the index. Defaults to "#{table}_#{column}_index"
* `:unique` - if the column(s) is unique or not
* `:concurrently` - if the index should be created/dropped concurrently
* `:using` - configures the index type
* `:prefix` - prefix for the index
* `:where` - the conditions for a partial index
## Adding/dropping indexes concurrently
PostgreSQL supports adding/dropping indexes concurrently (see the
[docs](http://www.postgresql.org/docs/9.4/static/sql-createindex.html)).
In order to take advantage of this, the `:concurrently` option needs to be set
to `true` when the index is created/dropped.
**Note**: in order for the `:concurrently` option to work, the migration must
not be run inside a transaction. See the `Ecto.Migration` docs for more
information on running migrations outside of a transaction.
## Index types
PostgreSQL supports several index types like B-tree, Hash or GiST. When
creating an index, the index type defaults to B-tree, but it can be specified
with the `:using` option. The `:using` option can be an atom or a string; its
value is passed to the `USING` clause as is.
More information on index types can be found in the [PostgreSQL
docs](http://www.postgresql.org/docs/9.4/static/indexes-types.html).
## Partial indexes
Databases like PostgreSQL and MSSQL supports partial indexes.
A partial index is an index built over a subset of a table. The subset
is defined by a conditional expression using the `:where` option.
The `:where` option can be an atom or a string; its value is passed
to the `WHERE` clause as is.
More information on partial indexes can be found in the [PostgreSQL
docs](http://www.postgresql.org/docs/9.4/static/indexes-partial.html).
## Examples
# Without a name, index defaults to products_category_id_sku_index
create index("products", [:category_id, :sku], unique: true)
# Name can be given explicitly though
drop index("products", [:category_id, :sku], name: :my_special_name)
# Indexes can be added concurrently
create index("products", [:category_id, :sku], concurrently: true)
# The index type can be specified
create index("products", [:name], using: :hash)
# Create a partial index
create index("products", [:user_id], where: "price = 0", name: :free_products_index)
Indexes also support custom expressions. Some databases may require the
index expression to be written between parens:
# Create an index on custom expressions
create index("products", ["(lower(name))"], name: :products_lower_name_index)
# To create a tsvector index with GIN on Postgres
create index("products", ["(to_tsvector('english', name))"],
name: :products_name_vector, using: "GIN")
"""
def index(table, columns, opts \\ [])
def index(table, columns, opts) when is_atom(table) do
index(Atom.to_string(table), columns, opts)
end
def index(table, column, opts) when is_binary(table) and is_atom(column) do
index(table, [column], opts)
end
def index(table, columns, opts) when is_binary(table) and is_list(columns) and is_list(opts) do
index = struct(%Index{table: table, columns: columns}, opts)
%{index | name: index.name || default_index_name(index)}
end
@doc """
Shortcut for creating a unique index.
See `index/3` for more information.
"""
def unique_index(table, columns, opts \\ [])
def unique_index(table, columns, opts) when is_list(opts) do
index(table, columns, [unique: true] ++ opts)
end
defp default_index_name(index) do
[index.table, index.columns, "index"]
|> List.flatten
|> Enum.map(&to_string(&1))
|> Enum.map(&String.replace(&1, ~r"[^\w_]", "_"))
|> Enum.map(&String.replace_trailing(&1, "_", ""))
|> Enum.join("_")
|> String.to_atom
end
@doc """
Executes arbitrary SQL or a keyword command.
Reversible commands can be defined by calling `execute/2`.
## Examples
execute "CREATE EXTENSION postgres_fdw"
execute create: "posts", capped: true, size: 1024
"""
def execute(command) when is_binary(command) or is_list(command) do
Runner.execute command
end
@doc """
Executes reversible SQL commands.
This is useful for database-specific functionality that does not
warrant special support in Ecto, for example, creating and dropping
a PostgreSQL extension, and avoids having to define up/down blocks.
## Examples
execute "CREATE EXTENSION postgres_fdw", "DROP EXTENSION postgres_fdw"
"""
def execute(up, down) when (is_binary(up) or is_list(up)) and
(is_binary(down) or is_list(down)) do
Runner.execute %Command{up: up, down: down}
end
@doc """
Gets the migrator direction.
"""
@spec direction :: :up | :down
def direction do
Runner.migrator_direction
end
@doc """
Gets the migrator prefix.
"""
def prefix do
Runner.prefix
end
@doc """
Adds a column when creating or altering a table.
This function also accepts Ecto primitive types as column types
and they are normalized by the database adapter. For example,
`:string` is converted to `:varchar`, `:binary` to `:bits` or `:blob`,
and so on.
However, the column type is not always the same as the type used in your
schema. For example, a schema that has a `:string` field,
can be supported by columns of types `:char`, `:varchar`, `:text` and others.
For this reason, this function also accepts `:text` and other columns,
which are sent as is to the underlying database.
To sum up, the column type may be either an Ecto primitive type,
which is normalized in cases the database does not understand it,
like `:string` or `:binary`, or a database type which is passed as is.
Custom Ecto types, like `Ecto.UUID`, are not supported because
they are application level concern and may not always map to the
database.
## Examples
create table("posts") do
add :title, :string, default: "Untitled"
end
alter table("posts") do
add :summary, :text # Database type
add :object, :map # Elixir type which is handled by the database
end
## Options
* `:primary_key` - when true, marks this field as the primary key
* `:default` - the column's default value. can be a string, number, empty list,
list of strings, list of numbers or a fragment generated by `fragment/1`
* `:null` - when `false`, the column does not allow null values
* `:size` - the size of the type (for example the numbers of characters).
Default is no size, except for `:string` that defaults to 255.
* `:precision` - the precision for a numeric type. Required when `scale` is specified.
* `:scale` - the scale of a numeric type. Default is 0 scale
"""
def add(column, type, opts \\ [])
def add(column, :datetime, _opts) when is_atom(column) do
raise ArgumentError, "the :datetime type in migrations is not supported, " <>
"please use :utc_datetime or :naive_datetime instead"
end
def add(column, type, opts) when is_atom(column) and is_list(opts) do
if opts[:scale] && !opts[:precision] do
raise ArgumentError, "column #{Atom.to_string(column)} is missing precision option"
end
validate_type!(type)
Runner.subcommand {:add, column, type, opts}
end
@doc """
Renames a table.
## Examples
rename table("posts"), to: table("new_posts")
"""
def rename(%Table{} = table_current, to: %Table{} = table_new) do
Runner.execute {:rename, __prefix__(table_current), __prefix__(table_new)}
table_new
end
@doc """
Renames a column outside of the `alter` statement.
## Examples
rename table("posts"), :title, to: :summary
"""
def rename(%Table{} = table, current_column, to: new_column) when is_atom(current_column) and is_atom(new_column) do
Runner.execute {:rename, __prefix__(table), current_column, new_column}
table
end
@doc """
Generates a fragment to be used as default value.
## Examples
create table("posts") do
add :inserted_at, :naive_datetime, default: fragment("now()")
end
"""
def fragment(expr) when is_binary(expr) do
{:fragment, expr}
end
@doc """
Adds `:inserted_at` and `:updated_at` timestamps columns.
Those columns are of `:naive_datetime` type, and by default
cannot be null. `opts` can be given to customize the generated
fields.
## Options
* `:inserted_at` - the name of the column for insertion times, providing `false` disables column
* `:updated_at` - the name of the column for update times, providing `false` disables column
* `:type` - column type, defaults to `:naive_datetime`
"""
def timestamps(opts \\ []) when is_list(opts) do
opts = Keyword.merge(Runner.repo_config(:migration_timestamps, []), opts)
opts = Keyword.put_new(opts, :null, false)
{type, opts} = Keyword.pop(opts, :type, :naive_datetime)
{inserted_at, opts} = Keyword.pop(opts, :inserted_at, :inserted_at)
{updated_at, opts} = Keyword.pop(opts, :updated_at, :updated_at)
if inserted_at != false, do: add(inserted_at, type, opts)
if updated_at != false, do: add(updated_at, type, opts)
end
@doc """
Modifies the type of column when altering a table.
This command is not reversible as Ecto does not know what
is the current type to revert it back to.
See `add/3` for more information on supported types.
## Examples
alter table("posts") do
modify :title, :text
end
## Options
* `:null` - sets to null or not null
* `:default` - changes the default
* `:size` - the size of the type (for example the numbers of characters). Default is no size.
* `:precision` - the precision for a numeric type. Required when `scale` is specified.
* `:scale` - the scale of a numeric type. Default is 0 scale.
"""
def modify(column, type, opts \\ [])
def modify(column, :datetime, _opts) when is_atom(column) do
raise ArgumentError, "the :datetime type in migrations is not supported, " <>
"please use :utc_datetime or :naive_datetime instead"
end
def modify(column, type, opts) when is_atom(column) and is_list(opts) do
if opts[:scale] && !opts[:precision] do
raise ArgumentError, "column #{Atom.to_string(column)} is missing precision option"
end
Runner.subcommand {:modify, column, type, opts}
end
@doc """
Removes a column when altering a table.
This command is not reversible as Ecto does not know what
is the current type to add the column back as. See
`remove/3` as a reversible alternative.
## Examples
alter table("posts") do
remove :title
end
"""
def remove(column) when is_atom(column) do
Runner.subcommand {:remove, column}
end
@doc """
Removes a column when altering a table in a reversible way.
`type` and `opts` are exactly the same as in `add/3` and
they are only used when the command is reversed.
## Examples
alter table("posts") do
remove :title, :string, default: ""
end
"""
def remove(column, type, opts \\ []) when is_atom(column) do
Runner.subcommand {:remove, column, type, opts}
end
@doc ~S"""
Defines a foreign key.
## Examples
create table("products") do
add :group_id, references("groups")
end
## Options
* `:name` - The name of the underlying reference,
defaults to "#{table}_#{column}_fkey"
* `:column` - The foreign key column, default is `:id`
* `:type` - The foreign key type, default is `:bigserial`
* `:on_delete` - What to perform if the referenced entry
is deleted. May be `:nothing`, `:delete_all`,
`:nilify_all` or `:restrict`. Defaults to `:nothing`.
* `:on_update` - What to perform if the referenced entry
is updated. May be `:nothing`, `:update_all`,
`:nilify_all` or `:restrict`. Defaults to `:nothing`.
"""
def references(table, opts \\ [])
def references(table, opts) when is_atom(table) do
references(Atom.to_string(table), opts)
end
def references(table, opts) when is_binary(table) and is_list(opts) do
repo_opts = Keyword.take(Runner.repo_config(:migration_primary_key, []), [:type])
opts = Keyword.merge(repo_opts, opts)
reference = struct(%Reference{table: table}, opts)
unless reference.on_delete in [:nothing, :delete_all, :nilify_all, :restrict] do
raise ArgumentError, "unknown :on_delete value: #{inspect reference.on_delete}"
end
unless reference.on_update in [:nothing, :update_all, :nilify_all, :restrict] do
raise ArgumentError, "unknown :on_update value: #{inspect reference.on_update}"
end
reference
end
@doc ~S"""
Defines a constraint (either a check constraint or an exclusion constraint) to be evaluated by the database when a row is inserted or updated.
## Examples
create constraint("users", :price_must_be_positive, check: "price > 0")
create constraint("size_ranges", :no_overlap, exclude: ~s|gist (int4range("from", "to", '[]') WITH &&)|)
drop constraint("products", "price_must_be_positive")
## Options
* `:check` - A check constraint expression. Required when creating a check constraint.
* `:exclude` - An exclusion constraint expression. Required when creating an exclusion constraint.
* `:prefix` - The prefix for the table.
"""
def constraint(table, name, opts \\ [])
def constraint(table, name, opts) when is_atom(table) do
constraint(Atom.to_string(table), name, opts)
end
def constraint(table, name, opts) when is_binary(table) and is_list(opts) do
struct(%Constraint{table: table, name: name}, opts)
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
Runner.flush
end
defp validate_type!(type) when is_atom(type) do
case Atom.to_string(type) do
"Elixir." <> _ ->
raise ArgumentError,
"#{inspect type} is not a valid database type, " <>
"please use an atom like :string, :text and so on"
_ ->
:ok
end
end
defp validate_type!({type, subtype}) when is_atom(type) and is_atom(subtype) do
validate_type!(subtype)
end
defp validate_type!({type, subtype}) when is_atom(type) and is_tuple(subtype) do
for t <- Tuple.to_list(subtype), do: validate_type!(t)
end
defp validate_type!(%Reference{} = reference) do
reference
end
@doc false
def __prefix__(%{prefix: prefix} = index_or_table) do
runner_prefix = Runner.prefix()
cond do
is_nil(prefix) ->
prefix = runner_prefix || Runner.repo_config(:migration_default_prefix, nil)
%{index_or_table | prefix: prefix}
is_nil(runner_prefix) or runner_prefix == to_string(prefix) ->
index_or_table
true ->
raise Ecto.MigrationError, message:
"the :prefix option `#{prefix}` does match the migrator prefix `#{runner_prefix}`"
end
end
end