diff --git a/lib/migration_generator/phase.ex b/lib/migration_generator/phase.ex index b1e3e2b3..d743810d 100644 --- a/lib/migration_generator/phase.ex +++ b/lib/migration_generator/phase.ex @@ -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 diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index f36418a0..d9efc296 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -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", @@ -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"