@@ -59,6 +59,18 @@ defmodule Ecto.Integration.StorageTest do
5959 System . cmd ( "psql" , args , env: env )
6060 end
6161
62+ def create_schema ( database , schema ) do
63+ run_psql ( ~s[ CREATE SCHEMA #{ schema } ] , [ database ] )
64+ end
65+
66+ def drop_schema ( database , schema ) do
67+ run_psql ( ~s[ DROP SCHEMA #{ schema } ] , [ database ] )
68+ end
69+
70+ def drop_schema_migrations_table ( database , schema ) do
71+ run_psql ( ~s[ DROP TABLE #{ schema } .schema_migrations] , [ database ] )
72+ end
73+
6274 test "storage up (twice in a row)" do
6375 assert Postgres . storage_up ( params ( ) ) == :ok
6476 assert Postgres . storage_up ( params ( ) ) == { :error , :already_up }
@@ -147,6 +159,72 @@ defmodule Ecto.Integration.StorageTest do
147159 assert contents =~ ~s[ INSERT INTO public."schema_migrations" (version) VALUES]
148160 end
149161
162+ test "when :dump_prefixes is not provided, structure is dumped for all schemas but only public schema migration records are inserted" do
163+ # Create the test_schema schema
164+ create_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
165+
166+ # Run migrations
167+ version = @ base_migration + System . unique_integer ( [ :positive ] )
168+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false )
169+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false , prefix: "test_schema" )
170+
171+ { :ok , path } = Postgres . structure_dump ( tmp_path ( ) , TestRepo . config ( ) )
172+ contents = File . read! ( path )
173+
174+ assert contents =~ "CREATE TABLE public.schema_migrations"
175+ assert contents =~ ~s[ INSERT INTO public."schema_migrations" (version) VALUES (#{ version } )]
176+ assert contents =~ "CREATE TABLE test_schema.schema_migrations"
177+ refute contents =~ ~s[ INSERT INTO test_schema."schema_migrations" (version) VALUES (#{ version } )]
178+ after
179+ drop_schema_migrations_table ( PoolRepo . config ( ) [ :database ] , "test_schema" )
180+ drop_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
181+ end
182+
183+ test "dumps structure and schema_migration records from multiple schemas" do
184+ # Create the test_schema schema
185+ create_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
186+
187+ # Run migrations
188+ version = @ base_migration + System . unique_integer ( [ :positive ] )
189+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false )
190+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false , prefix: "test_schema" )
191+
192+ config = Keyword . put ( TestRepo . config ( ) , :dump_prefixes , [ "public" , "test_schema" ] )
193+ { :ok , path } = Postgres . structure_dump ( tmp_path ( ) , config )
194+ contents = File . read! ( path )
195+
196+ assert contents =~ "CREATE TABLE public.schema_migrations"
197+ assert contents =~ ~s[ INSERT INTO public."schema_migrations" (version) VALUES (#{ version } )]
198+ assert contents =~ "CREATE TABLE test_schema.schema_migrations"
199+ assert contents =~ ~s[ INSERT INTO test_schema."schema_migrations" (version) VALUES (#{ version } )]
200+ after
201+ drop_schema_migrations_table ( PoolRepo . config ( ) [ :database ] , "test_schema" )
202+ drop_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
203+ end
204+
205+ test "dumps structure and schema_migration records only from queried schema" do
206+ # Create the test_schema schema
207+ create_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
208+
209+ # Run migrations
210+ version = @ base_migration + System . unique_integer ( [ :positive ] )
211+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false )
212+ :ok = Ecto.Migrator . up ( PoolRepo , version , Migration , log: false , prefix: "test_schema" )
213+
214+ config = Keyword . put ( TestRepo . config ( ) , :dump_prefixes , [ "test_schema" ] )
215+ { :ok , path } = Postgres . structure_dump ( tmp_path ( ) , config )
216+ contents = File . read! ( path )
217+
218+ refute contents =~ "CREATE TABLE public.schema_migrations"
219+ refute contents =~ ~s[ INSERT INTO public."schema_migrations" (version) VALUES (#{ version } )]
220+ assert contents =~ "CREATE TABLE test_schema.schema_migrations"
221+ assert contents =~ ~s[ INSERT INTO test_schema."schema_migrations" (version) VALUES (#{ version } )]
222+ after
223+ drop_schema_migrations_table ( PoolRepo . config ( ) [ :database ] , "test_schema" )
224+ drop_schema ( PoolRepo . config ( ) [ :database ] , "test_schema" )
225+ end
226+
227+
150228 test "storage status is up when database is created" do
151229 create_database ( )
152230 assert :up == Postgres . storage_status ( params ( ) )
0 commit comments