Skip to content

Commit 325aa9a

Browse files
authored
improvement: create schema before table creation (#518)
1 parent 5d0d63b commit 325aa9a

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

lib/migration_generator/phase.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ defmodule AshPostgres.MigrationGenerator.Phase do
1313
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
1414
"\nend"
1515
else
16-
opts =
16+
{pre_create, opts} =
1717
if schema do
18-
", prefix: \"#{schema}\""
18+
{"execute(\"CREATE SCHEMA IF NOT EXISTS #{schema}\")" <> "\n\n",
19+
", prefix: \"#{schema}\""}
1920
else
20-
""
21+
{"", ""}
2122
end
2223

23-
"create table(:#{as_atom(table)}, primary_key: false#{opts}) do\n" <>
24+
pre_create <>
25+
"create table(:#{as_atom(table)}, primary_key: false#{opts}) do\n" <>
2426
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
2527
"\nend"
2628
end

test/migration_generator_test.exs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,6 @@ defmodule AshPostgres.MigrationGeneratorTest do
243243

244244
defdomain([Post])
245245

246-
{:ok, _} =
247-
Ecto.Adapters.SQL.query(
248-
AshPostgres.TestRepo,
249-
"""
250-
CREATE SCHEMA IF NOT EXISTS example;
251-
"""
252-
)
253-
254246
AshPostgres.MigrationGenerator.generate(Domain,
255247
snapshot_path: "test_snapshots_path",
256248
migration_path: "test_migration_path",
@@ -272,6 +264,9 @@ defmodule AshPostgres.MigrationGeneratorTest do
272264

273265
file_contents = File.read!(file)
274266

267+
# the migration creates the schema
268+
assert file_contents =~ "execute(\"CREATE SCHEMA IF NOT EXISTS example\")"
269+
275270
# the migration creates the table
276271
assert file_contents =~ "create table(:posts, primary_key: false, prefix: \"example\") do"
277272

0 commit comments

Comments
 (0)