Update failure - Missing database table or columns

Update failure - Missing database table or columns

You can get an error like:

The table '{{table_name}}' for active record class 'ModelName' cannot be found in the database.

Which means a database table is missing from your app, or you can get an error where the app complaints about a missing database column in a table, etc.
You get this error mainly because you have a large database and while running the import from the web interface, the web server timed out and you end up with an incomplete upgrade. Best is to always run the command line updater, it’s safer than the one from the web browser as it does not time out. Fixing errors like the above is not hard, but just time consuming.

Let’s assume you were upgrading from 1.4.0. to 1.4.3 when you got the above error.
So now, what we have to do, is to go via FTP at /apps/common/data/update-sql and open the files 1.4.0.sql / 1.4.1.sql / 1.4.2.sql and 1.4.3.sql.
These are the files that AppMail tried to run in order to upgrade your database, but it failed to do so for one of them. We can see that the files 1.4.0.sql and 1.4.1.sql are empty, but 1.4.2.sql contains this:

--
-- campaign option
--
ALTER TABLE `campaign_option` ADD `autoresponder_include_current` ENUM('yes','no') NOT NULL DEFAULT 'no' AFTER `autoresponder_include_imported`;

--
-- create campaign open action subscriber table
--
CREATE TABLE IF NOT EXISTS `campaign_sent_action_subscriber` (
  `action_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `campaign_id` int(11) NOT NULL,
  `list_id` int(11) NOT NULL,
  `action` char(5) NOT NULL DEFAULT 'copy',
  `date_added` datetime NOT NULL,
  `last_updated` datetime NOT NULL,
  PRIMARY KEY (`action_id`),
  KEY `fk_campaign_sent_action_subscriber_campaign1_idx` (`campaign_id`),
  KEY `fk_campaign_sent_action_subscriber_list1_idx` (`list_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- add the fk to the campaign_sent_action_subscriber table
--
ALTER TABLE `campaign_sent_action_subscriber`
  ADD CONSTRAINT `fk_campaign_sent_action_subscriber_campaign1` FOREIGN KEY (`campaign_id`) REFERENCES `campaign` (`campaign_id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_campaign_sent_action_subscriber_list1` FOREIGN KEY (`list_id`) REFERENCES `list` (`list_id`) ON DELETE CASCADE ON UPDATE NO ACTION;

So, there’s a bunch of SQL queries in there, and we must run them, one by one against our database server in order to bring the database up to date.
You can use phpMyAdmin to run the above queries against your database, or the command line, for those that have more experience.
Now, in SQL, lines that start with two dashes (–) means they are comments, so we can omit them.
So, this means, from the above file, the first SQL query that we have to run, is:

ALTER TABLE `campaign_option` ADD `autoresponder_include_current` ENUM('yes','no') NOT NULL DEFAULT 'no' AFTER `autoresponder_include_imported`;

However, running it like so, will most likely end up with an error, because our tables contain a prefix which we have added when we installed mailwizz. Most likely this table prefix is set to `mw_`, but you might choose something else when you installed the app.
Either way, in this case, our database query becomes:

ALTER TABLE `mw_campaign_option` ADD `autoresponder_include_current` ENUM('yes','no') NOT NULL DEFAULT 'no' AFTER `autoresponder_include_imported`;

and we can run it in phpMyAdmin against our database, and we will see it runs okay.
If you get any error, it means this query has run successfully during the upgrade process so you can simply go to the next query.
Now that we know the above, we can transform the rest of the queries from our file and add the right table prefix, like so:

--
-- create campaign open action subscriber table
--
CREATE TABLE IF NOT EXISTS `mw_campaign_sent_action_subscriber` (
  `action_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `campaign_id` int(11) NOT NULL,
  `list_id` int(11) NOT NULL,
  `action` char(5) NOT NULL DEFAULT 'copy',
  `date_added` datetime NOT NULL,
  `last_updated` datetime NOT NULL,
  PRIMARY KEY (`action_id`),
  KEY `fk_campaign_sent_action_subscriber_campaign1_idx` (`campaign_id`),
  KEY `fk_campaign_sent_action_subscriber_list1_idx` (`list_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- add the fk to the campaign_sent_action_subscriber table
--
ALTER TABLE `campaign_sent_action_subscriber`
  ADD CONSTRAINT `fk_campaign_sent_action_subscriber_campaign1` FOREIGN KEY (`campaign_id`) REFERENCES `mw_campaign` (`campaign_id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_campaign_sent_action_subscriber_list1` FOREIGN KEY (`list_id`) REFERENCES `mw_list` (`list_id`) ON DELETE CASCADE ON UPDATE NO ACTION;

And we can run them in phpMyAdmin against our database.
We should do the same for all the .sql files left to complete the upgrade.

This is a bit difficult, and you should avoid getting in such situation by simply using the command line upgrade tool instead of upgrading from the browser.

Please don’t forget to clear the app cache after you do all these changes. Simply rename the folder apps/common/runtime/cache to something else will force the cache to regenerate.

    • Related Articles

    • The upgrade timed out and now I have missing columns in the database

      If during the upgrade process you get a timeout it is because your database is just too big and it takes too much time to run all the queries against your existing database. In such case, the best is to restore your backup that you did before ...
    • Internal failure, maybe due to missing functions like proc_open!

      When validating a delivery server, and you get an error that says, “Internal failure, maybe due to missing functions like proc_open!” Most likely the issue is that AppMail cannot find a valid email template to send the validation email. To fix this, ...
    • Update AppMail from the command line

      If your database is too large, you might want to update your AppMail application from command line rather than the web interface. In order to do this, follow the upgrade steps but once you reach the update screen, do not press the Submit button in ...
    • Column already exists 1060 Duplicate column name

      Update failure: Column already exists: 1060 Duplicate column name. A typical error would look like: Updating to version 1.3.6.1 failed with: CDbCommand failed to execute the SQL statement: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column ...
    • The IMAP extension is missing from your PHP installation.

      In case this kind of error isn’t pretty self-explanatory, your php instance doesn’t contain the imap extension. If you have access to command line, following commands should install php imap on your server: If you are on CentOS / RedHat / Fedora or ...