Skip to content

Commit 2c6466d

Browse files
committed
improvement: add c:AshPostgres.Repo.create_schemas_in_migrations? callback
1 parent cf3da36 commit 2c6466d

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,14 +1361,20 @@ defmodule AshPostgres.MigrationGenerator do
13611361

13621362
defp group_into_phases(
13631363
[
1364-
%Operation.CreateTable{table: table, schema: schema, multitenancy: multitenancy} | rest
1364+
%Operation.CreateTable{
1365+
table: table,
1366+
schema: schema,
1367+
multitenancy: multitenancy,
1368+
repo: repo
1369+
}
1370+
| rest
13651371
],
13661372
nil,
13671373
acc
13681374
) do
13691375
group_into_phases(
13701376
rest,
1371-
%Phase.Create{table: table, schema: schema, multitenancy: multitenancy},
1377+
%Phase.Create{table: table, schema: schema, multitenancy: multitenancy, repo: repo},
13721378
acc
13731379
)
13741380
end
@@ -2010,6 +2016,7 @@ defmodule AshPostgres.MigrationGenerator do
20102016
%Operation.CreateTable{
20112017
table: snapshot.table,
20122018
schema: snapshot.schema,
2019+
repo: snapshot.repo,
20132020
multitenancy: snapshot.multitenancy,
20142021
old_multitenancy: empty_snapshot.multitenancy
20152022
}

lib/migration_generator/operation.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
145145

146146
defmodule CreateTable do
147147
@moduledoc false
148-
defstruct [:table, :schema, :multitenancy, :old_multitenancy]
148+
defstruct [:table, :schema, :multitenancy, :old_multitenancy, :repo]
149149
end
150150

151151
defmodule AddAttribute do

lib/migration_generator/phase.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ defmodule AshPostgres.MigrationGenerator.Phase do
33

44
defmodule Create do
55
@moduledoc false
6-
defstruct [:table, :schema, :multitenancy, operations: [], commented?: false]
6+
defstruct [:table, :schema, :multitenancy, :repo, operations: [], commented?: false]
77

88
import AshPostgres.MigrationGenerator.Operation.Helper, only: [as_atom: 1]
99

10-
def up(%{schema: schema, table: table, operations: operations, multitenancy: multitenancy}) do
10+
def up(%{
11+
schema: schema,
12+
table: table,
13+
operations: operations,
14+
multitenancy: multitenancy,
15+
repo: repo
16+
}) do
1117
if multitenancy.strategy == :context do
1218
"create table(:#{as_atom(table)}, primary_key: false, prefix: prefix()) do\n" <>
1319
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
1420
"\nend"
1521
else
1622
{pre_create, opts} =
17-
if schema do
23+
if schema && repo.create_schemas_in_migrations?() do
1824
{"execute(\"CREATE SCHEMA IF NOT EXISTS #{schema}\")" <> "\n\n",
1925
", prefix: \"#{schema}\""}
2026
else

lib/repo.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ defmodule AshPostgres.Repo do
7878
@doc "The default prefix(postgres schema) to use when building queries"
7979
@callback default_prefix() :: String.t()
8080

81+
@doc "Whether or not to create schemas for tables when generating migrations"
82+
@callback create_schemas_in_migrations?() :: boolean()
83+
8184
@doc "Whether or not to explicitly start and close a transaction for each action, even if there are no transaction hooks. Defaults to `true`."
8285
@callback prefer_transaction?() :: boolean
8386

@@ -135,6 +138,7 @@ defmodule AshPostgres.Repo do
135138
def installed_extensions, do: []
136139
def tenant_migrations_path, do: nil
137140
def migrations_path, do: nil
141+
def create_schemas_in_migrations?, do: true
138142
def default_prefix, do: "public"
139143
def override_migration_type(type), do: type
140144
def create?, do: true
@@ -304,6 +308,7 @@ defmodule AshPostgres.Repo do
304308
prefer_transaction?: 0,
305309
prefer_transaction_for_atomic_updates?: 0,
306310
tenant_migrations_path: 0,
311+
create_schemas_in_migrations?: 0,
307312
migrations_path: 0,
308313
default_prefix: 0,
309314
override_migration_type: 1,

0 commit comments

Comments
 (0)