Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Dependent Views and recreating views #165

Open
bamorim opened this issue Jul 5, 2014 · 3 comments
Open

Dependent Views and recreating views #165

bamorim opened this issue Jul 5, 2014 · 3 comments

Comments

@bamorim
Copy link

bamorim commented Jul 5, 2014

Well, I have a test suite that I keep database up do date with ActiveRecord::Migration.maintain_test_schema!

The problem is that it tries to recreate the database and sometimes it drops a view that others views depends, so it raises this exception

/home/bamorim/.rvm/gems/ruby-2.1.0@respondeai/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec': PG::DependentObjectsStillExist: ERROR:  cannot drop view analysis_periods because other objects depend on it (ActiveRecord::StatementInvalid)
DETAIL:  view impression_counts_by_period depends on view analysis_periods
view activity_statuses_by_period depends on view impression_counts_by_period
view subscription_statuses_by_period depends on view analysis_periods
view user_metrics_by_period depends on view analysis_periods
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
: DROP VIEW IF EXISTS "analysis_periods"

I have two approaches for this but can't realize which one is better because they have flaws.

The first one is using CREATE OR REPLACE VIEW istead o DROP and then CREATING.
The problem is that with this approach we can't change the type of the return, I mean, we cannot change the type of each column nor the order or the number of columns.

The other approach is using DROP VIEW ... CASCADE, but with this one we wouldn't know what views are going to be dropped and then needed to be rebuilt.

What do you guys think?

@bamorim
Copy link
Author

bamorim commented Jul 5, 2014

Another solution that I don't know if it is possible would be always adding a drop_view in reverse order in schema.rb, but that is somewhat not DRY.

@ronen
Copy link
Member

ronen commented Jul 7, 2014

I think DROP VIEW ... CASCADE would be OK. it should be fine to drop all views that depend on the current view, since presumably the migration is going to be recreating all those other views anyway. no?

(as you say, another choice would be to explicitly include drop_views in reverse order -- but in order for that to work we'd need to drop all the views that depend on it, which is what CASCADE would do anyway.)

@hoenth
Copy link

hoenth commented Jul 10, 2015

@bamorim - I am curious about your statement that the CREATE OR REPLACE VIEW statement cannot be used if you are changing the type, order, or number of columns. Is this a postgres limitation. Mysql does not appear to have the same limitations, as the CREATE OR REPLACE VIEW is synonymous with ALTER VIEW (https://dev.mysql.com/doc/refman/5.0/en/alter-view.html).

If it is a postgres issue, then the mysql adapter might always use CREATE OR REPLACE VIEW instead of create. This would solve for us the same issue you are currently facing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants