Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute interdependent views from mysqldump backup

mysqldump creates a dump with the tables (and views) listed alphabetically. When there are foreign key relationships between the tables this is not very convenient, however, the problem is easily resolved by running:

SET FOREIGN_KEY_CHECKS=0;

I have a situation whereby the view, vwapple, is dependent on the view, vworange, say. With the mysqldump, vwapple is listed and executed before vworange, which is problematic as we'll get a "view vworange does not exist" error message.

How is this situation solved? Do views have a similar solutions as tables have? Or is it better to make each view independent just to get round this problem?

NOTE
I'm restoring the database using a python script. Each view and table is in its separate .sql file as I want to source control each database object independently.

like image 535
SleepingSpider Avatar asked Jun 20 '26 09:06

SleepingSpider


1 Answers

Recent versions of mysqldump, when dumping whole databases, solve things this way: for every view in the database, they first create an empty table with the same structure, then they create the views, dropping each placeholder table before creating a view. It seems that replacing a table with a view won't harm views that depend on it.

I know about this because this approach has another flaw, which bit me: rows in views may contain more data than rows in tables, so some of the placeholder tables cannot be created, causing some dumps to be unrestorable without manual intervention. For this reason, a topological sorting of views would be preferable, although it might require some work.

like image 131
MvG Avatar answered Jun 22 '26 23:06

MvG