Sha256: 1f0628baf07def0f178f9bbdebfbebd1e00b46a83b13a01629678ad7eb954e9d

Contents?: true

Size: 1.54 KB

Versions: 16

Compression:

Stored size: 1.54 KB

Contents

class CreateRefreshAllMaterializedViewsFn < ActiveRecord::Migration[5.0]
  def up
    # Function to refresh all materialized views in all or a specified schema,
    # optionally concurrently
    # From https://github.com/frankhommers/RefreshAllMaterializedViews
    sql = <<-SQL.squish
      CREATE OR REPLACE FUNCTION
      refresh_all_matierialized_views(_schema TEXT DEFAULT '*', _concurrently BOOLEAN DEFAULT false)
      RETURNS INT AS $$
        DECLARE
          r RECORD;
        BEGIN
          RAISE NOTICE 'Refreshing materialized view(s) in % %',
            CASE WHEN _schema = '*' THEN 'all schemas'
            ELSE 'schema "'|| _schema || '"'
            END,
            CASE WHEN _concurrently
            THEN 'concurrently'
            ELSE '' END;
          IF pg_is_in_recovery() THEN
            RETURN 0;
          ELSE
            FOR r IN SELECT schemaname,
                            matviewname FROM pg_matviews WHERE schemaname = _schema OR _schema = '*'
            LOOP
              RAISE NOTICE 'Refreshing materialized view "%"."%"', r.schemaname, r.matviewname;
              EXECUTE 'REFRESH MATERIALIZED VIEW ' || CASE WHEN _concurrently THEN 'CONCURRENTLY '
              ELSE '' END || '"' || r.schemaname || '"."' || r.matviewname || '"';
            END LOOP;
          END IF;
          RETURN 1;
        END
      $$ LANGUAGE plpgsql;
    SQL

    ActiveRecord::Base.connection.execute(sql)
  end

  def down
    ActiveRecord::Base.connection.execute(
      "DROP FUNCTION refresh_all_matierialized_views(TEXT,BOOLEAN);"
    )
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.rc8 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc7 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc6 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc5 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc4 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc3 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.rc1 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta12 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta11 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta10 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta9 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta8 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta7 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta6 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta5 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb
renalware-core-2.0.0.pre.beta4 db/migrate/20170705090219_create_refresh_all_materialized_views_fn.rb