From d0f20521f8257bac256ced806cec46a0e32b2f50 Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sun, 21 Jan 2024 18:53:44 -0500 Subject: [PATCH] Test for schema order of non-public-schema views Upcoming changes like [topological sorting][1] have the potential to write views to `db/schema.rb` in an order that does not respect dependencies. This adds a test to ensure that views in all schemas are both included, and in the correct dependency order. [1]: https://github.com/scenic-views/scenic/pull/398 --- spec/scenic/schema_dumper_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/scenic/schema_dumper_spec.rb b/spec/scenic/schema_dumper_spec.rb index c9f285eb..02378110 100644 --- a/spec/scenic/schema_dumper_spec.rb +++ b/spec/scenic/schema_dumper_spec.rb @@ -69,6 +69,22 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.drop_view :"scenic.searches" end + + it "sorts dependency order when views exist in a non-public schema" do + Search.connection.execute("CREATE SCHEMA IF NOT EXISTS scenic; SET search_path TO public, scenic") + Search.connection.execute("CREATE VIEW scenic.apples AS SELECT 1;") + Search.connection.execute("CREATE VIEW scenic.bananas AS SELECT 2;") + Search.connection.execute("CREATE OR REPLACE VIEW scenic.apples AS SELECT * FROM scenic.bananas;") + stream = StringIO.new + + ActiveRecord::SchemaDumper.dump(Search.connection, stream) + views = stream.string.lines.grep(/create_view/).map do |view_line| + view_line.match('create_view "(?.*)"')[:name] + end + expect(views).to eq(%w[scenic.bananas scenic.apples]) + + Search.connection.execute("DROP SCHEMA IF EXISTS scenic CASCADE; SET search_path TO public") + end end it "handles active record table name prefixes and suffixes" do