pg_trunk 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +1 -1
- data/lib/pg_trunk/core/railtie/custom_types.rb +5 -6
- data/lib/pg_trunk/operations/check_constraints/add_check_constraint.rb +18 -12
- data/lib/pg_trunk/operations/check_constraints/drop_check_constraint.rb +18 -10
- data/lib/pg_trunk/operations/check_constraints/rename_check_constraint.rb +12 -6
- data/lib/pg_trunk/operations/check_constraints/validate_check_constraint.rb +6 -2
- data/lib/pg_trunk/operations/composite_types/change_composite_type.rb +24 -18
- data/lib/pg_trunk/operations/composite_types/create_composite_type.rb +10 -9
- data/lib/pg_trunk/operations/composite_types/drop_composite_type.rb +25 -23
- data/lib/pg_trunk/operations/composite_types/rename_composite_type.rb +3 -3
- data/lib/pg_trunk/operations/domains/change_domain.rb +27 -24
- data/lib/pg_trunk/operations/domains/create_domain.rb +11 -11
- data/lib/pg_trunk/operations/domains/drop_domain.rb +19 -13
- data/lib/pg_trunk/operations/domains/rename_domain.rb +3 -1
- data/lib/pg_trunk/operations/enums/change_enum.rb +13 -11
- data/lib/pg_trunk/operations/enums/create_enum.rb +9 -9
- data/lib/pg_trunk/operations/enums/drop_enum.rb +18 -10
- data/lib/pg_trunk/operations/enums/rename_enum.rb +3 -1
- data/lib/pg_trunk/operations/foreign_keys/add_foreign_key.rb +23 -17
- data/lib/pg_trunk/operations/foreign_keys/drop_foreign_key.rb +17 -11
- data/lib/pg_trunk/operations/foreign_keys/rename_foreign_key.rb +11 -5
- data/lib/pg_trunk/operations/functions/change_function.rb +24 -21
- data/lib/pg_trunk/operations/functions/create_function.rb +34 -26
- data/lib/pg_trunk/operations/functions/drop_function.rb +28 -18
- data/lib/pg_trunk/operations/functions/rename_function.rb +6 -2
- data/lib/pg_trunk/operations/materialized_views/change_materialized_view.rb +24 -17
- data/lib/pg_trunk/operations/materialized_views/create_materialized_view.rb +37 -29
- data/lib/pg_trunk/operations/materialized_views/drop_materialized_view.rb +20 -10
- data/lib/pg_trunk/operations/materialized_views/refresh_materialized_view.rb +3 -1
- data/lib/pg_trunk/operations/materialized_views/rename_materialized_view.rb +8 -4
- data/lib/pg_trunk/operations/procedures/change_procedure.rb +22 -18
- data/lib/pg_trunk/operations/procedures/create_procedure.rb +26 -18
- data/lib/pg_trunk/operations/procedures/drop_procedure.rb +18 -10
- data/lib/pg_trunk/operations/procedures/rename_procedure.rb +6 -2
- data/lib/pg_trunk/operations/statistics/create_statistics.rb +32 -24
- data/lib/pg_trunk/operations/statistics/drop_statistics.rb +26 -18
- data/lib/pg_trunk/operations/statistics/rename_statistics.rb +3 -1
- data/lib/pg_trunk/operations/triggers/change_trigger.rb +9 -7
- data/lib/pg_trunk/operations/triggers/create_trigger.rb +26 -20
- data/lib/pg_trunk/operations/triggers/drop_trigger.rb +16 -10
- data/lib/pg_trunk/operations/views/change_view.rb +20 -14
- data/lib/pg_trunk/operations/views/create_view.rb +18 -10
- data/lib/pg_trunk/operations/views/drop_view.rb +19 -9
- data/lib/pg_trunk/operations/views/rename_view.rb +6 -2
- data/lib/pg_trunk/version.rb +1 -1
- metadata +1 -1
@@ -16,28 +16,36 @@
|
|
16
16
|
# # The operation drops a enumerated type identified by its
|
17
17
|
# # qualified name (it can include a schema).
|
18
18
|
# #
|
19
|
-
# #
|
19
|
+
# # ```ruby
|
20
|
+
# # drop_enum "finances.currency"
|
21
|
+
# # ```
|
20
22
|
# #
|
21
23
|
# # To make the operation invertible, use the same options
|
22
24
|
# # as in the `create_enum` operation.
|
23
25
|
# #
|
24
|
-
# #
|
25
|
-
# #
|
26
|
-
# #
|
27
|
-
# #
|
28
|
-
# #
|
29
|
-
# #
|
30
|
-
# #
|
26
|
+
# # ```ruby
|
27
|
+
# # drop_enum "finances.currency" do |e|
|
28
|
+
# # e.values "BTC", "EUR", "GBP", "USD"
|
29
|
+
# # e.value "JPY" # the alternative way to add a value
|
30
|
+
# # e.comment <<~COMMENT
|
31
|
+
# # The list of values for supported currencies.
|
32
|
+
# # COMMENT
|
33
|
+
# # end
|
34
|
+
# # ```
|
31
35
|
# #
|
32
36
|
# # With the `force: :cascade` option the operation would remove
|
33
37
|
# # all the objects that use the type.
|
34
38
|
# #
|
35
|
-
# #
|
39
|
+
# # ```ruby
|
40
|
+
# # drop_enum "finances.currency", force: :cascade
|
41
|
+
# # ```
|
36
42
|
# #
|
37
43
|
# # With the `if_exists: true` option the operation won't fail
|
38
44
|
# # even when the view was absent in the database.
|
39
45
|
# #
|
40
|
-
# #
|
46
|
+
# # ```ruby
|
47
|
+
# # drop_enum "finances.currency", if_exists: true
|
48
|
+
# # ```
|
41
49
|
# #
|
42
50
|
# # Both options make a migration irreversible due to uncertainty
|
43
51
|
# # of the previous state of the database.
|
@@ -10,7 +10,9 @@
|
|
10
10
|
# #
|
11
11
|
# # @example:
|
12
12
|
# #
|
13
|
-
# #
|
13
|
+
# # ```ruby
|
14
|
+
# # rename_enum "currencies", to: "finances.currency"
|
15
|
+
# # ```
|
14
16
|
# #
|
15
17
|
# # The operation is always reversible.
|
16
18
|
# def rename_enum(name, to:); end
|
@@ -26,29 +26,35 @@
|
|
26
26
|
# # The table and reference of the new key must be set explicitly.
|
27
27
|
# # All the rest (including the name) can be generated by default:
|
28
28
|
# #
|
29
|
-
# #
|
30
|
-
# #
|
29
|
+
# # ```ruby
|
30
|
+
# # # same as `..., column: 'role_id', primary_key: 'id'`
|
31
|
+
# # add_foreign_key :users, :roles
|
32
|
+
# # ```
|
31
33
|
# #
|
32
34
|
# # The block syntax can be used for any argument:
|
33
35
|
# #
|
34
|
-
# #
|
35
|
-
# #
|
36
|
-
# #
|
37
|
-
# #
|
38
|
-
# #
|
39
|
-
# #
|
40
|
-
# #
|
41
|
-
# #
|
42
|
-
# #
|
43
|
-
# #
|
36
|
+
# # ```ruby
|
37
|
+
# # add_foreign_key do |k|
|
38
|
+
# # k.table "users"
|
39
|
+
# # k.reference "roles"
|
40
|
+
# # k.column "role_id" # (generated by default from reference and pk)
|
41
|
+
# # k.primary_key "id" # (default)
|
42
|
+
# # k.on_update :cascade # :restrict (default)
|
43
|
+
# # k.on_delete :cascade # :restrict (default)
|
44
|
+
# # k.name "user_roles_fk" # can be generated
|
45
|
+
# # k.comment "Phone is 10+ chars long"
|
46
|
+
# # end
|
47
|
+
# # ```
|
44
48
|
# #
|
45
49
|
# # Composite foreign keys are supported as well:
|
46
50
|
# #
|
47
|
-
# #
|
48
|
-
# #
|
49
|
-
# #
|
50
|
-
# #
|
51
|
-
# #
|
51
|
+
# # ```ruby
|
52
|
+
# # add_foreign_key "users", "roles" do |k|
|
53
|
+
# # k.columns %w[role_name role_id]
|
54
|
+
# # k.primary_key %w[name id] # Requires unique index
|
55
|
+
# # k.match :full # :partial, :simple (default)
|
56
|
+
# # end
|
57
|
+
# # ```
|
52
58
|
# #
|
53
59
|
# # The operation is always invertible.
|
54
60
|
# def add_foreign_key(table, reference, **options, &block); end
|
@@ -26,27 +26,33 @@
|
|
26
26
|
# #
|
27
27
|
# # The key can be identified by table/name (not invertible):
|
28
28
|
# #
|
29
|
-
# #
|
29
|
+
# # ```ruby
|
30
|
+
# # drop_foreign_key "users", name: "user_roles_fk"
|
31
|
+
# # ```
|
30
32
|
# #
|
31
33
|
# # To make it invertible use the same options like
|
32
34
|
# # in the `add_foreign_key` operation.
|
33
35
|
# #
|
34
|
-
# #
|
35
|
-
# #
|
36
|
-
# #
|
37
|
-
# #
|
38
|
-
# #
|
39
|
-
# #
|
40
|
-
# #
|
41
|
-
# #
|
42
|
-
# #
|
36
|
+
# # ```ruby
|
37
|
+
# # drop_foreign_key do |k|
|
38
|
+
# # k.table "users"
|
39
|
+
# # k.reference "roles"
|
40
|
+
# # k.column "role_id"
|
41
|
+
# # k.primary_key "id"
|
42
|
+
# # k.on_update :cascade
|
43
|
+
# # k.on_delete :cascade
|
44
|
+
# # k.comment "Phone is 10+ chars long"
|
45
|
+
# # end
|
46
|
+
# # ```
|
43
47
|
# #
|
44
48
|
# # Notice that the name can be skipped, in this case we would
|
45
49
|
# # find it in the database.
|
46
50
|
# #
|
47
51
|
# # The operation can be called with `if_exists` option.
|
48
52
|
# #
|
49
|
-
# #
|
53
|
+
# # ```ruby
|
54
|
+
# # drop_foreign_key "users", name: "user_roles_fk", if_exists: true
|
55
|
+
# # ```
|
50
56
|
# #
|
51
57
|
# # In this case the operation is always irreversible due to
|
52
58
|
# # uncertainty of the previous state of the database.
|
@@ -17,17 +17,23 @@
|
|
17
17
|
# #
|
18
18
|
# # You can rename the foreign key constraint identified by its explicit name:
|
19
19
|
# #
|
20
|
-
# #
|
21
|
-
# #
|
22
|
-
# #
|
20
|
+
# # ```ruby
|
21
|
+
# # rename_foreign_key :users,
|
22
|
+
# # name: "user_roles_fk",
|
23
|
+
# # to: "constraints.users_by_roles_fk"
|
24
|
+
# # ```
|
23
25
|
# #
|
24
26
|
# # The key can also be found in the database by table/reference/columns/pk
|
25
27
|
# #
|
26
|
-
# #
|
28
|
+
# # ```ruby
|
29
|
+
# # rename_foreign_key :users, :roles, primary_key: "name", to: "user_roles"
|
30
|
+
# # ```
|
27
31
|
# #
|
28
32
|
# # If a new name is missed, then the name will be reset to the auto-generated one:
|
29
33
|
# #
|
30
|
-
# #
|
34
|
+
# # ```ruby
|
35
|
+
# # rename_foreign_key :users, "user_roles_fk"
|
36
|
+
# # ```
|
31
37
|
# #
|
32
38
|
# # The operation is always reversible.
|
33
39
|
# def rename_foreign_key(table, reference, **options, &block); end
|
@@ -17,33 +17,36 @@
|
|
17
17
|
# # You can change any property except for the name
|
18
18
|
# # (use `rename_function` instead) and `language`.
|
19
19
|
# #
|
20
|
-
# #
|
21
|
-
# #
|
22
|
-
# #
|
23
|
-
# #
|
24
|
-
# #
|
25
|
-
# #
|
26
|
-
# #
|
27
|
-
# #
|
28
|
-
# #
|
20
|
+
# # ```ruby
|
21
|
+
# # change_function "math.mult(int, int)" do |f|
|
22
|
+
# # f.volatility :immutable, from: :stable
|
23
|
+
# # f.parallel :safe, from: :restricted
|
24
|
+
# # f.security :invoker
|
25
|
+
# # f.leakproof true
|
26
|
+
# # f.strict true
|
27
|
+
# # f.cost 5.0
|
28
|
+
# # # f.rows 1 (supported for functions returning sets of rows)
|
29
|
+
# # SQL
|
30
|
+
# # ```
|
29
31
|
# #
|
30
32
|
# # The example above is not invertible because of uncertainty
|
31
33
|
# # about the previous volatility, parallelism, and cost.
|
32
34
|
# # To define them, use a from options (available in a block syntax only):
|
33
35
|
# #
|
34
|
-
# #
|
35
|
-
# #
|
36
|
-
# #
|
37
|
-
# #
|
38
|
-
# #
|
39
|
-
# #
|
40
|
-
# # f.volatility :immutable, from: :volatile
|
41
|
-
# # f.parallel :safe, from: :unsafe
|
42
|
-
# # f.leakproof true
|
43
|
-
# # f.strict true
|
44
|
-
# # f.cost 5.0, from: 100.0
|
45
|
-
# # # f.rows 1, from: 0
|
36
|
+
# # ```ruby
|
37
|
+
# # change_function "math.mult(a int, b int)" do |f|
|
38
|
+
# # f.body <<~SQL, from: <<~SQL
|
39
|
+
# # SELECT a * b;
|
40
|
+
# # SQL
|
41
|
+
# # SELECT min(a * b, 1);
|
46
42
|
# # SQL
|
43
|
+
# # f.volatility :immutable, from: :volatile
|
44
|
+
# # f.parallel :safe, from: :unsafe
|
45
|
+
# # f.leakproof true
|
46
|
+
# # f.strict true
|
47
|
+
# # f.cost 5.0, from: 100.0
|
48
|
+
# # SQL
|
49
|
+
# # ```
|
47
50
|
# #
|
48
51
|
# # Like in the other operations, the function can be
|
49
52
|
# # identified by a qualified name (with types of arguments).
|
@@ -26,46 +26,54 @@
|
|
26
26
|
# #
|
27
27
|
# # The function can be created either using inline syntax
|
28
28
|
# #
|
29
|
-
# #
|
30
|
-
# #
|
31
|
-
# #
|
32
|
-
# #
|
33
|
-
# #
|
34
|
-
# #
|
29
|
+
# # ```ruby
|
30
|
+
# # create_function "math.mult(a int, b int) int",
|
31
|
+
# # language: :sql,
|
32
|
+
# # body: "SELECT a * b",
|
33
|
+
# # volatility: :immutable,
|
34
|
+
# # leakproof: true,
|
35
|
+
# # comment: "Multiplies 2 integers"
|
36
|
+
# # ```
|
35
37
|
# #
|
36
38
|
# # or using a block:
|
37
39
|
# #
|
38
|
-
# #
|
39
|
-
# #
|
40
|
-
# #
|
41
|
-
# #
|
42
|
-
# #
|
43
|
-
# # f.volatility :immutable # :stable, :volatile (default)
|
44
|
-
# # f.parallel :safe # :restricted, :unsafe (default)
|
45
|
-
# # f.security :invoker # (default), also :definer
|
46
|
-
# # f.leakproof true
|
47
|
-
# # f.strict true
|
48
|
-
# # f.cost 5.0
|
49
|
-
# # # f.rows 1 (supported for functions returning sets of rows)
|
50
|
-
# # f.comment "Multiplies 2 integers"
|
40
|
+
# # ```ruby
|
41
|
+
# # create_function "math.mult(a int, b int) int" do |f|
|
42
|
+
# # f.language "sql" # (default)
|
43
|
+
# # f.body <<~SQL
|
44
|
+
# # SELECT a * b;
|
51
45
|
# # SQL
|
46
|
+
# # f.volatility :immutable # :stable, :volatile (default)
|
47
|
+
# # f.parallel :safe # :restricted, :unsafe (default)
|
48
|
+
# # f.security :invoker # (default), also :definer
|
49
|
+
# # f.leakproof true
|
50
|
+
# # f.strict true
|
51
|
+
# # f.cost 5.0
|
52
|
+
# # # f.rows 1 (supported for functions returning sets of rows)
|
53
|
+
# # f.comment "Multiplies 2 integers"
|
54
|
+
# # SQL
|
55
|
+
# # ```
|
52
56
|
# #
|
53
57
|
# # With a `replace_existing: true` option,
|
54
58
|
# # it will be created using the `CREATE OR REPLACE` clause.
|
55
59
|
# # In this case the migration is irreversible because we
|
56
60
|
# # don't know if and how to restore its previous definition.
|
57
61
|
# #
|
58
|
-
# #
|
59
|
-
# #
|
60
|
-
# #
|
62
|
+
# # ```ruby
|
63
|
+
# # create_function "math.mult(a int, b int) int",
|
64
|
+
# # body: "SELECT a * b",
|
65
|
+
# # replace_existing: true
|
66
|
+
# # ```
|
61
67
|
# #
|
62
68
|
# # We presume a function without arguments should have
|
63
69
|
# # no arguments and return `void` like
|
64
70
|
# #
|
65
|
-
# #
|
66
|
-
# #
|
67
|
-
# #
|
68
|
-
# #
|
71
|
+
# # ```ruby
|
72
|
+
# # # the same as "do_something() void"
|
73
|
+
# # create_function "do_something" do |f|
|
74
|
+
# # # ...
|
75
|
+
# # end
|
76
|
+
# # ```
|
69
77
|
# def create_function(name, **options, &block); end
|
70
78
|
# end
|
71
79
|
module PGTrunk::Operations::Functions
|
@@ -28,42 +28,52 @@
|
|
28
28
|
# #
|
29
29
|
# # A function can be dropped by a plain name:
|
30
30
|
# #
|
31
|
-
# #
|
31
|
+
# # ```ruby
|
32
|
+
# # drop_function "multiply"
|
33
|
+
# # ```
|
32
34
|
# #
|
33
35
|
# # If several overloaded functions have the name,
|
34
36
|
# # then you must specify the signature having
|
35
37
|
# # types of attributes at least:
|
36
38
|
# #
|
37
|
-
# #
|
39
|
+
# # ```ruby
|
40
|
+
# # drop_function "multiply(int, int)"
|
41
|
+
# # ```
|
38
42
|
# #
|
39
43
|
# # In both cases above the operation is irreversible. To make it
|
40
44
|
# # inverted you have to provide a full signature along with
|
41
45
|
# # the body definition. The other options are supported as well:
|
42
46
|
# #
|
43
|
-
# #
|
44
|
-
# #
|
45
|
-
# #
|
46
|
-
# #
|
47
|
-
# #
|
48
|
-
# #
|
49
|
-
# #
|
50
|
-
# #
|
51
|
-
# #
|
52
|
-
# #
|
53
|
-
# #
|
54
|
-
# #
|
55
|
-
# #
|
56
|
-
# #
|
47
|
+
# # ```ruby
|
48
|
+
# # drop_function "math.mult(a int, b int) int" do |f|
|
49
|
+
# # f.language "sql" # (default)
|
50
|
+
# # f.body <<~SQL
|
51
|
+
# # SELECT a * b;
|
52
|
+
# # SQL
|
53
|
+
# # f.volatility :immutable # :stable, :volatile (default)
|
54
|
+
# # f.parallel :safe # :restricted, :unsafe (default)
|
55
|
+
# # f.security :invoker # (default), also :definer
|
56
|
+
# # f.leakproof true
|
57
|
+
# # f.strict true
|
58
|
+
# # f.cost 5.0
|
59
|
+
# # # f.rows 1 (supported for functions returning sets of rows)
|
60
|
+
# # f.comment "Multiplies 2 integers"
|
61
|
+
# # end
|
62
|
+
# # ```
|
57
63
|
# #
|
58
64
|
# # The operation can be called with `if_exists` option. In this case
|
59
65
|
# # it would do nothing when no function existed.
|
60
66
|
# #
|
61
|
-
# #
|
67
|
+
# # ```ruby
|
68
|
+
# # drop_function "math.multiply(integer, integer)", if_exists: true
|
69
|
+
# # ```
|
62
70
|
# #
|
63
71
|
# # Another operation-specific option `force: :cascade` enables
|
64
72
|
# # to drop silently any object depending on the function.
|
65
73
|
# #
|
66
|
-
# #
|
74
|
+
# # ```ruby
|
75
|
+
# # drop_function "math.multiply(integer, integer)", force: :cascade
|
76
|
+
# # ```
|
67
77
|
# #
|
68
78
|
# # Both options make the operation irreversible because of
|
69
79
|
# # uncertainty about the previous state of the database.
|
@@ -13,11 +13,15 @@
|
|
13
13
|
# #
|
14
14
|
# # If there are no overloaded functions, then you can use a plain name:
|
15
15
|
# #
|
16
|
-
# #
|
16
|
+
# # ```ruby
|
17
|
+
# # rename_function "math.multiply", to: "public.product"
|
18
|
+
# # ```
|
17
19
|
# #
|
18
20
|
# # otherwise the types of attributes must be explicitly specified.
|
19
21
|
# #
|
20
|
-
# #
|
22
|
+
# # ```ruby
|
23
|
+
# # rename_function "math.multiply(int, int)", to: "public.product"
|
24
|
+
# # ```
|
21
25
|
# #
|
22
26
|
# # Any specification of attributes or returned values in `to:` option
|
23
27
|
# # is ignored because they cannot be changed anyway.
|
@@ -12,15 +12,16 @@
|
|
12
12
|
# #
|
13
13
|
# # The operation enables to alter a view without recreating
|
14
14
|
# # its from scratch. You can rename columns, change their
|
15
|
-
# # storage settings (how the column is TOAST-ed), or
|
16
|
-
# # customize their statistics.
|
15
|
+
# # storage settings (how the column is TOAST-ed), or customize their statistics.
|
17
16
|
# #
|
18
|
-
# #
|
19
|
-
# #
|
20
|
-
# #
|
21
|
-
# #
|
22
|
-
# #
|
23
|
-
# #
|
17
|
+
# # ```ruby
|
18
|
+
# # change_materialized_view "admin_users" do |v|
|
19
|
+
# # v.rename_column "name", to: "full_name"
|
20
|
+
# # v.column "name", storage: "extended", from_storage: "expanded"
|
21
|
+
# # v.column "admin", n_distinct: 2
|
22
|
+
# # v.column "role", statistics: 100
|
23
|
+
# # end
|
24
|
+
# # ```
|
24
25
|
# #
|
25
26
|
# # Notice that renaming will be done AFTER all changes even
|
26
27
|
# # though the order of declarations can be different.
|
@@ -32,26 +33,32 @@
|
|
32
33
|
# # In addition to changing columns, the operation enables
|
33
34
|
# # to set a default clustering by given index:
|
34
35
|
# #
|
35
|
-
# #
|
36
|
-
# #
|
37
|
-
# #
|
36
|
+
# # ```ruby
|
37
|
+
# # change_materialized_view "admin_users" do |v|
|
38
|
+
# # v.cluster_on "admin_users_by_names_idx"
|
39
|
+
# # end
|
40
|
+
# # ```
|
38
41
|
# #
|
39
42
|
# # The clustering is invertible, but its inversion does nothing,
|
40
43
|
# # keeping the clustering unchanged.
|
41
44
|
# #
|
42
45
|
# # The comment can also be changed:
|
43
46
|
# #
|
44
|
-
# #
|
45
|
-
# #
|
46
|
-
# #
|
47
|
+
# # ```ruby
|
48
|
+
# # change_materialized_view "admin_users" do |v|
|
49
|
+
# # v.comment "Admin users", from: "Admin users only"
|
50
|
+
# # end
|
51
|
+
# # ```
|
47
52
|
# #
|
48
53
|
# # Notice, that without `from` option the operation is still
|
49
54
|
# # invertible, but its inversion would delete the comment.
|
50
55
|
# # It can also be reset to the blank string explicitly:
|
51
56
|
# #
|
52
|
-
# #
|
53
|
-
# #
|
54
|
-
# #
|
57
|
+
# # ```ruby
|
58
|
+
# # change_materialized_view "admin_users" do |v|
|
59
|
+
# # v.comment "", from: "Admin users only"
|
60
|
+
# # end
|
61
|
+
# # ```
|
55
62
|
# #
|
56
63
|
# # With the `if_exists: true` option, the operation won't fail
|
57
64
|
# # even when the view wasn't existed. At the same time,
|
@@ -18,14 +18,18 @@
|
|
18
18
|
# #
|
19
19
|
# # The operation creates the view using its `sql_definition`:
|
20
20
|
# #
|
21
|
-
# #
|
22
|
-
# #
|
23
|
-
# #
|
21
|
+
# # ```ruby
|
22
|
+
# # create_materialized_view("views.admin_users", sql_definition: <<~SQL)
|
23
|
+
# # SELECT id, name FROM users WHERE admin;
|
24
|
+
# # SQL
|
25
|
+
# # ```
|
24
26
|
# #
|
25
27
|
# # For compatibility to the `scenic` gem, we also support
|
26
28
|
# # adding a definition via its version:
|
27
29
|
# #
|
28
|
-
# #
|
30
|
+
# # ```ruby
|
31
|
+
# # create_materialized_view "admin_users", version: 1
|
32
|
+
# # ```
|
29
33
|
# #
|
30
34
|
# # It is expected, that a `db/materialized_views/admin_users_v01.sql`
|
31
35
|
# # to contain the SQL snippet.
|
@@ -35,41 +39,45 @@
|
|
35
39
|
# # can be moved to another tablespace, but we don't support
|
36
40
|
# # this feature yet).
|
37
41
|
# #
|
38
|
-
# #
|
39
|
-
# #
|
40
|
-
# #
|
41
|
-
# #
|
42
|
-
# #
|
43
|
-
# #
|
44
|
-
# #
|
45
|
-
# #
|
42
|
+
# # ```ruby
|
43
|
+
# # create_materialized_view "admin_users" do |v|
|
44
|
+
# # v.tablespace "fast_ssd"
|
45
|
+
# # v.sql_definition <<~SQL
|
46
|
+
# # SELECT id, name, password, admin, on_duty
|
47
|
+
# # FROM users
|
48
|
+
# # WHERE admin
|
49
|
+
# # SQL
|
50
|
+
# # end
|
51
|
+
# # ```
|
46
52
|
# #
|
47
53
|
# # You can also set a comment describing the view,
|
48
54
|
# # and redefine the storage options for some TOAST-ed columns,
|
49
55
|
# # as well as their custom statistics:
|
50
56
|
# #
|
51
|
-
# #
|
52
|
-
# #
|
53
|
-
# #
|
54
|
-
# #
|
55
|
-
# #
|
56
|
-
# #
|
57
|
-
# #
|
58
|
-
# #
|
59
|
-
# #
|
60
|
-
# #
|
61
|
-
# #
|
62
|
-
# #
|
63
|
-
# #
|
64
|
-
# #
|
57
|
+
# # ```ruby
|
58
|
+
# # create_materialized_view "admin_users" do |v|
|
59
|
+
# # v.sql_definition <<~SQL
|
60
|
+
# # SELECT id, name, password, admin, on_duty
|
61
|
+
# # FROM users
|
62
|
+
# # WHERE admin
|
63
|
+
# # SQL
|
64
|
+
# # v.column "password", storage: "external" # to avoid compression
|
65
|
+
# # v.column "password", n_distinct: -1 # linear dependency
|
66
|
+
# # v.column "admin", n_distinct: 1 # exact number of values
|
67
|
+
# # v.column "on_duty", statistics: 2 # the total number of values
|
68
|
+
# # v.comment "Admin users only"
|
69
|
+
# # end
|
70
|
+
# # ```
|
65
71
|
# #
|
66
72
|
# # With the `replace_existing: true` option the operation
|
67
73
|
# # would use `CREATE OR REPLACE VIEW` command, so it
|
68
74
|
# # can be used to "update" (or reload) the existing view.
|
69
75
|
# #
|
70
|
-
# #
|
71
|
-
# #
|
72
|
-
# #
|
76
|
+
# # ```ruby
|
77
|
+
# # create_materialized_view "admin_users",
|
78
|
+
# # version: 1,
|
79
|
+
# # replace_existing: true
|
80
|
+
# # ```
|
73
81
|
# #
|
74
82
|
# # This option makes the migration irreversible due to uncertainty
|
75
83
|
# # of the previous state of the database.
|
@@ -20,31 +20,41 @@
|
|
20
20
|
# # The operation drops a materialized view identified by its
|
21
21
|
# # qualified name (it can include a schema).
|
22
22
|
# #
|
23
|
-
# #
|
23
|
+
# # ```ruby
|
24
|
+
# # drop_materialized_view "views.admin_users"
|
25
|
+
# # ```
|
24
26
|
# #
|
25
27
|
# # To make the operation invertible, use the same options
|
26
28
|
# # as in the `create_view` operation.
|
27
29
|
# #
|
28
|
-
# #
|
29
|
-
# #
|
30
|
-
# #
|
31
|
-
# #
|
32
|
-
# #
|
33
|
-
# #
|
30
|
+
# # ```ruby
|
31
|
+
# # drop_materialized_view "views.admin_users" do |v|
|
32
|
+
# # v.sql_definition "SELECT name, password FROM users WHERE admin;"
|
33
|
+
# # v.column "password", storage: "external" # prevent compression
|
34
|
+
# # v.with_data false
|
35
|
+
# # v.comment "Admin users only"
|
36
|
+
# # end
|
37
|
+
# # ```
|
34
38
|
# #
|
35
39
|
# # You can also use a version-base SQL definition like:
|
36
40
|
# #
|
37
|
-
# #
|
41
|
+
# # ```ruby
|
42
|
+
# # drop_materialized_view "admin_users", revert_to_version: 1
|
43
|
+
# # ```
|
38
44
|
# #
|
39
45
|
# # With the `force: :cascade` option the operation would remove
|
40
46
|
# # all the objects which depend on the view.
|
41
47
|
# #
|
42
|
-
# #
|
48
|
+
# # ```ruby
|
49
|
+
# # drop_materialized_view "admin_users", force: :cascade
|
50
|
+
# # ```
|
43
51
|
# #
|
44
52
|
# # With the `if_exists: true` option the operation won't fail
|
45
53
|
# # even when the view was absent in the database.
|
46
54
|
# #
|
47
|
-
# #
|
55
|
+
# # ```ruby
|
56
|
+
# # drop_materialized_view "admin_users", if_exists: true
|
57
|
+
# # ```
|
48
58
|
# #
|
49
59
|
# # Both options make a migration irreversible due to uncertainty
|
50
60
|
# # of the previous state of the database.
|
@@ -12,7 +12,9 @@
|
|
12
12
|
# # The operation enables refreshing a materialized view
|
13
13
|
# # by reloading its underlying SQL query:
|
14
14
|
# #
|
15
|
-
# #
|
15
|
+
# # ```ruby
|
16
|
+
# # refresh_materialized_view "admin_users"
|
17
|
+
# # ```
|
16
18
|
# #
|
17
19
|
# # The option `algorithm: :concurrently` acts exactly
|
18
20
|
# # like in the `create_index` definition. You should
|