Skip to content

Commit 448ac31

Browse files
authored
fix: fix some issues in migration generator related to tenancy (#539)
1 parent 64c3c82 commit 448ac31

File tree

1 file changed

+18
-74
lines changed

1 file changed

+18
-74
lines changed

lib/migration_generator/operation.ex

+18-74
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ defmodule AshPostgres.MigrationGenerator.Operation do
127127
end
128128

129129
def match_type(_), do: nil
130+
131+
def index_keys(keys, all_tenants?, multitenancy) do
132+
if multitenancy.strategy == :attribute and not all_tenants? do
133+
[multitenancy.attribute | keys]
134+
else
135+
keys
136+
end
137+
end
130138
end
131139

132140
defmodule CreateTable do
@@ -851,18 +859,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
851859
schema: schema,
852860
multitenancy: multitenancy
853861
}) do
854-
keys =
855-
if all_tenants? do
856-
keys
857-
else
858-
case multitenancy.strategy do
859-
:attribute ->
860-
[multitenancy.attribute | keys]
861-
862-
_ ->
863-
keys
864-
end
865-
end
862+
keys = index_keys(keys, all_tenants?, multitenancy)
866863

867864
index_name = index_name || "#{table}_#{name}_index"
868865

@@ -888,19 +885,12 @@ defmodule AshPostgres.MigrationGenerator.Operation do
888885
end
889886

890887
def down(%{
891-
identity: %{name: name, keys: keys, index_name: index_name},
888+
identity: %{name: name, keys: keys, index_name: index_name, all_tenants?: all_tenants?},
892889
table: table,
893890
schema: schema,
894891
multitenancy: multitenancy
895892
}) do
896-
keys =
897-
case multitenancy.strategy do
898-
:attribute ->
899-
[multitenancy.attribute | keys]
900-
901-
_ ->
902-
keys
903-
end
893+
keys = index_keys(keys, all_tenants?, multitenancy)
904894

905895
index_name = index_name || "#{table}_#{name}_index"
906896

@@ -962,12 +952,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
962952
base_filter: base_filter,
963953
multitenancy: multitenancy
964954
}) do
965-
keys =
966-
if !index.all_tenants? and multitenancy.strategy == :attribute do
967-
[multitenancy.attribute | index.fields]
968-
else
969-
index.fields
970-
end
955+
keys = index_keys(index.fields, index.all_tenants?, multitenancy)
971956

972957
index =
973958
case {index.where, base_filter} do
@@ -997,12 +982,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
997982
end
998983

999984
def down(%{schema: schema, index: index, table: table, multitenancy: multitenancy}) do
1000-
keys =
1001-
if !index.all_tenants? and multitenancy.strategy == :attribute do
1002-
[multitenancy.attribute | index.fields]
1003-
else
1004-
index.fields
1005-
end
985+
keys = index_keys(index.fields, index.all_tenants?, multitenancy)
1006986

1007987
opts =
1008988
join([
@@ -1167,7 +1147,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
11671147
@moduledoc false
11681148
defstruct [:schema, :table, no_phase: true]
11691149

1170-
def up(%{schema: schema, table: table, multitenancy: multitenancy}) do
1150+
def up(%{schema: schema, table: table, old_multitenancy: multitenancy}) do
11711151
cond do
11721152
multitenancy.strategy == :context ->
11731153
"drop constraint(#{inspect(table)}, \"#{table}_pkey\", prefix: prefix())"
@@ -1305,48 +1285,12 @@ defmodule AshPostgres.MigrationGenerator.Operation do
13051285

13061286
import Helper
13071287

1308-
def up(%{
1309-
identity: %{name: name, keys: keys, index_name: index_name},
1310-
table: table,
1311-
schema: schema,
1312-
old_multitenancy: multitenancy
1313-
}) do
1314-
keys =
1315-
case multitenancy.strategy do
1316-
:attribute ->
1317-
[multitenancy.attribute | keys]
1318-
1319-
_ ->
1320-
keys
1321-
end
1322-
1323-
index_name = index_name || "#{table}_#{name}_index"
1324-
1325-
"drop_if_exists unique_index(:#{as_atom(table)}, [#{Enum.map_join(keys, ", ", &inspect/1)}], #{join(["name: \"#{index_name}\"", option(:prefix, schema)])})"
1288+
def up(operation) do
1289+
AddUniqueIndex.down(%{operation | multitenancy: operation.old_multitenancy})
13261290
end
13271291

1328-
def down(%{
1329-
identity: %{name: name, keys: keys, base_filter: base_filter, index_name: index_name},
1330-
table: table,
1331-
schema: schema,
1332-
multitenancy: multitenancy
1333-
}) do
1334-
keys =
1335-
case multitenancy.strategy do
1336-
:attribute ->
1337-
[multitenancy.attribute | keys]
1338-
1339-
_ ->
1340-
keys
1341-
end
1342-
1343-
index_name = index_name || "#{table}_#{name}_index"
1344-
1345-
if base_filter do
1346-
"create unique_index(:#{as_atom(table)}, [#{Enum.map_join(keys, ", ", &inspect/1)}], where: \"#{base_filter}\", #{join(["name: \"#{index_name}\"", option(:prefix, schema)])})"
1347-
else
1348-
"create unique_index(:#{as_atom(table)}, [#{Enum.map_join(keys, ", ", &inspect/1)}], #{join(["name: \"#{index_name}\"", option(:prefix, schema)])})"
1349-
end
1292+
def down(operation) do
1293+
AddUniqueIndex.up(%{operation | multitenancy: operation.old_multitenancy})
13501294
end
13511295
end
13521296

0 commit comments

Comments
 (0)