title: ActiveRecord migration not populating a Postgres materialized view
body: <p>I have a <code>MATERIALIZED VIEW</code> that is created via a migration.</p>
  
  <pre><code>class MyView &lt; ActiveRecord::Migration
    def up
      ActiveRecord::Base.connection.execute &lt;&lt;-SQL
      CREATE MATERIALIZED VIEW my_view AS (
        SELECT DISTINCT something, something_else, other.thing as real_thing, thing.some_id
            FROM some_table
            JOIN another_table on another_table.id = something
            JOIN one_more_table on some_table.id = other_id
            ORDER BY order_column)
        WITH DATA;
      SQL
  
      add_index :table, [:key_part_one, :key_part_two]
    end
  
    ...
  end
  </code></pre>
  
  <p>Note: I've obfuscated the SELECT statement, just trust me that it works.</p>
  
  <p>The important part to note here is that I've explicitly called <code>WITH DATA</code>, so the view should be populated and scannable right away.</p>
  
  <p>This is not happening.  The migration runs, shown below</p>
  
  <pre><code>==  MyView: migrating ========================
  ==  MyView: migrated (0.0763s) ===============
  </code></pre>
  
  <p>Later on in the <code>db:refresh</code> we see the following</p>
  
  <pre><code>Reindexing Something...
  Reindex queued
  Reindexing Another...
  Reindex queued
  Reindexing SomeOtherThing...
  Reindex queued
  Reindexing One::OtherThing...
  Reindex queued
  Reindexing MyViewModel...
  rake aborted!
  ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR:  materialized view "my_view" has not been populated
  HINT:  Use the REFRESH MATERIALIZED VIEW command.
  </code></pre>
  
  <p>Um, what? I declared <code>WITH DATA</code>.  I also have another consecutive migration that explicitly calls the <code>REFRESH MATERIALIZED VIEW</code> command on the view.</p>
  
  <p>To no avail, in order to get the rake db:refresh task to complete, I have to go in and manually refresh the view.</p>
  
  <p>Interesting note, in the structure.sql file, its shown as being created WITH NO DATA</p>
  
  <pre><code>CREATE MATERIALIZED VIEW my_view AS (
    SELECT DISTINCT something, something_else, other.thing as real_thing, thing.some_id
        FROM some_table
        JOIN another_table on another_table.id = something
        JOIN one_more_table on some_table.id = other_id
        ORDER BY order_column)
    WITH NO DATA;
  </code></pre>
  
  <p>I believe this is the real issue but I'm unaware of a fix/workaround.  Its also confusing because even if it was created WITH NO DATA, the subsequent REFRESH MATERIALIZED VIEW should populate it and mark it as scannable.</p>
  
  <p>Is there some issue with Postgres or AR that I'm not aware of that is preventing me from populating this materialized view?</p>