From 54594ebca18eff6a4a699788c3f801bad1fb5063 Mon Sep 17 00:00:00 2001
From: "Sinisa@sinisa.nasamreza.org" <>
Date: Wed, 28 Aug 2002 16:41:23 +0300
Subject: [PATCH] Little but nasty bug with BDB and alter table

---
 Docs/manual.texi                      |  3 +++
 mysql-test/r/bdb-alter-table-1.result |  4 ++++
 mysql-test/r/bdb-alter-table-2.result |  4 ++++
 mysql-test/t/bdb-alter-table-1.test   | 12 ++++++++++++
 mysql-test/t/bdb-alter-table-2.test   |  3 +++
 sql/sql_table.cc                      | 13 ++++++++-----
 6 files changed, 34 insertions(+), 5 deletions(-)
 create mode 100644 mysql-test/r/bdb-alter-table-1.result
 create mode 100644 mysql-test/r/bdb-alter-table-2.result
 create mode 100644 mysql-test/t/bdb-alter-table-1.test
 create mode 100644 mysql-test/t/bdb-alter-table-2.test

diff --git a/Docs/manual.texi b/Docs/manual.texi
index 77122dce2d8..9bbb9fa0c5a 100644
--- a/Docs/manual.texi
+++ b/Docs/manual.texi
@@ -46929,6 +46929,9 @@ not yet 100% confident in this code.
 @appendixsubsec Changes in release 3.23.53
 @itemize @bullet
 @item
+Fixed a bug with BDB @code[ALTER TABLE] with dropping column, and shutdown 
+immediately thereafter.
+@item
 Fixed problem with @code{UNSIGNED BIGINT} on AIX (again).
 @item
 Fixed bug in pthread_mutex_trylock() on HPUX 11.0
diff --git a/mysql-test/r/bdb-alter-table-1.result b/mysql-test/r/bdb-alter-table-1.result
new file mode 100644
index 00000000000..3742f45eb7a
--- /dev/null
+++ b/mysql-test/r/bdb-alter-table-1.result
@@ -0,0 +1,4 @@
+objid	tablename	oid	test
+1	t1	4	9
+2	metatable	1	9
+3	metaindex	1	9
diff --git a/mysql-test/r/bdb-alter-table-2.result b/mysql-test/r/bdb-alter-table-2.result
new file mode 100644
index 00000000000..19c6d5e1ff9
--- /dev/null
+++ b/mysql-test/r/bdb-alter-table-2.result
@@ -0,0 +1,4 @@
+objid	tablename	oid
+1	t1	4
+2	metatable	1
+3	metaindex	1
diff --git a/mysql-test/t/bdb-alter-table-1.test b/mysql-test/t/bdb-alter-table-1.test
new file mode 100644
index 00000000000..25a86cd92f6
--- /dev/null
+++ b/mysql-test/t/bdb-alter-table-1.test
@@ -0,0 +1,12 @@
+-- source include/have_bdb.inc
+
+#
+# Small basic test for ALTER TABLE bug ..
+#
+drop table if exists t1;
+create table t1(objid BIGINT not null, tablename  varchar(64), oid BIGINT not null, test BIGINT,  PRIMARY KEY (objid), UNIQUE(tablename))  type=BDB;
+insert into t1 values(1, 't1',4,9);
+insert into t1 values(2, 'metatable',1,9);
+insert into t1 values(3, 'metaindex',1,9 );
+select * from t1;
+alter table t1 drop column test;
diff --git a/mysql-test/t/bdb-alter-table-2.test b/mysql-test/t/bdb-alter-table-2.test
new file mode 100644
index 00000000000..69ff04ee24a
--- /dev/null
+++ b/mysql-test/t/bdb-alter-table-2.test
@@ -0,0 +1,3 @@
+-- source include/have_bdb.inc
+select * from t1;
+drop table t1;
\ No newline at end of file
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 9a5056a1f35..534b267403c 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -1665,11 +1665,6 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name,
     VOID(pthread_cond_broadcast(&COND_refresh));
     goto err;
   }
-#ifdef HAVE_BERKELEY_DB
-  extern bool berkeley_flush_logs(void);
-  if (old_db_type == DB_TYPE_BERKELEY_DB &&  berkeley_flush_logs())
-    goto err;
-#endif
   thd->proc_info="end";
   mysql_update_log.write(thd, thd->query,thd->query_length);
   if (mysql_bin_log.is_open())
@@ -1679,6 +1674,14 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name,
   }
   VOID(pthread_cond_broadcast(&COND_refresh));
   VOID(pthread_mutex_unlock(&LOCK_open));
+#ifdef HAVE_BERKELEY_DB
+  extern bool berkeley_flush_logs(void);
+  if (old_db_type == DB_TYPE_BERKELEY_DB)
+  {
+    (void)berkeley_flush_logs();
+    table=open_ltable(thd,table_list,TL_READ);
+  }
+#endif
 
 end_temporary:
   sprintf(tmp_name,ER(ER_INSERT_INFO),(ulong) (copied+deleted),