Skip to content

fix: create schema before table creation #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/migration_generator/phase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ defmodule AshPostgres.MigrationGenerator.Phase do
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
else
opts =
{pre_create, opts} =
if schema do
", prefix: \"#{schema}\""
{"execute(\"CREATE SCHEMA IF NOT EXISTS #{schema}\")" <> "\n\n",
", prefix: \"#{schema}\""}
else
""
{"", ""}
end

"create table(:#{as_atom(table)}, primary_key: false#{opts}) do\n" <>
pre_create <>
"create table(:#{as_atom(table)}, primary_key: false#{opts}) do\n" <>
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
end
Expand Down
11 changes: 3 additions & 8 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,6 @@ defmodule AshPostgres.MigrationGeneratorTest do

defdomain([Post])

{:ok, _} =
Ecto.Adapters.SQL.query(
AshPostgres.TestRepo,
"""
CREATE SCHEMA IF NOT EXISTS example;
"""
)

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
Expand All @@ -272,6 +264,9 @@ defmodule AshPostgres.MigrationGeneratorTest do

file_contents = File.read!(file)

# the migration creates the schema
assert file_contents =~ "execute(\"CREATE SCHEMA IF NOT EXISTS example\")"

# the migration creates the table
assert file_contents =~ "create table(:posts, primary_key: false, prefix: \"example\") do"

Expand Down
Loading