Bug #35533 Foreign keys: can't drop database with foreign keys
Submitted: 24 Mar 2008 17:59 Modified: 28 Apr 2008 7:55
Reporter: Peter Gulutzan Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:6.1 OS:Linux (SUSE 10 | 32-bit)
Assigned to: Dmitry Lenev CPU Architecture:Any

[24 Mar 2008 17:59] Peter Gulutzan
Description:
I create a database.
I create 10,000 tables.
I try to drop the database.
I get an error message.

How to repeat:
In the following, I create a database with 10,000 tables,
which is the same thing I did for an earlier bug report
"Foreign keys: errors with automatic constraint names".
But this time, after trying to create the table, I say
use test
drop database m;
The result is error 1010. I think it has nothing to do
with the automatically generated names, so I report it
as a separate bug.

delimiter //
use test//
drop database m//
create database m//
use m//
drop procedure p//
create table t (s1 int primary key)//
create procedure p ()
begin
  declare v int default 0;
  while v < 10000 do
    set @x = 'create table t';
    set @x = concat(@x,v);
    set @x = concat(@x,' (s1 int constraint fk_m_1_');
    set @x = concat(@x,v);
    set @x = concat(@x,' references t(s1))');
    prepare stmt1 from @x;
    execute stmt1;
    set v = v + 1;
    end while;
  end//
call p()//

/* now quit, stop server, restart server, and restart client */

use m
create table m (s1 int references t(s1));
use test
drop database m;

The result of "drop database m;" looks like this:
ERROR 1010 (HY000): Error dropping database (can't rmdir './m/', errno: 17)

Then I quit and looked at /usr/local/mysql/var/m.
It is an empty directory.
[24 Mar 2008 23:26] MySQL Verification Team
Thank you for the bug report.

mysql> drop database m;
ERROR 1010 (HY000): Error dropping database (can't rmdir './m/', errno: 17)
mysql> use m
Database changed
mysql> show tables;
Empty set (0.00 sec)
[22 Apr 2008 17:41] Dmitry Lenev
After investigation I have realized that much simpler test case which
exposes the same problem looks like:

create database mysqltest;
use mysqltest;
create table t1 (i int primary key);
create table t2 (fk int references t1 (i));
use test;
# Statement below emits error 1010
drop database mysqltest;
[22 Apr 2008 17:42] Dmitry Lenev
Updated bug synopsis after finding out its cause.
[23 Apr 2008 5:18] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/45860

ChangeSet@1.2610, 2008-04-23 09:18:40+04:00, dlenev@mockturtle.local +5 -0
  Fix for bug #35533 "Foreign keys: can't drop database with foreign keys".
  
  In --foreign-key-all-engines mode attempt to drop database containing
  tables with foreign keys failed with error and left empty directory behind.
  
  Code implementing DROP DATABASE was not aware of the fact that .CNS files,
  which represent occupied names of foreign key constraints, are automatically
  removed if table corresponding on which foreign key is defined is dropped
  and therefore they can be safely ignored while scanning database directory
  before removing it.
  This fix adds ".CNS" to the list of such known ignorable files extensions.
[28 Apr 2008 7:43] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/46095

ChangeSet@1.2610, 2008-04-28 11:38:27+04:00, dlenev@mockturtle.local +5 -0
  Fix for bug #35533 "Foreign keys: can't drop database with foreign keys".
  
  In --foreign-key-all-engines mode attempt to drop database containing
  tables with foreign keys failed with error and left empty directory behind.
  
  Code implementing DROP DATABASE was not aware of the fact that .CNS files,
  which represent occupied names of foreign key constraints, are automatically
  removed if table corresponding on which foreign key is defined is dropped
  and therefore they can be safely ignored while scanning database directory
  before removing it.
  This fix adds ".CNS" to the list of such known ignorable files extensions.
[28 Apr 2008 7:55] Dmitry Lenev
Fix for this bug was pushed into mysql-6.1-fk tree. Since this bug was reported against tree which is not publicly available yet I am simply closing this report.