I’m using a MySQL GUI to migrate some sites to a new version of a CMS by selecting certain tables and running the INSERT statement generated from a backup dump into an empty table (the new schema). There are a few columns in the old tables that don’t exist in the new one, so the script stops with an error like this:
Script line: 1 Unknown column ‘user_id’ in ‘field list’
Cherry-picking the desired columns to export, or editing the dump file would be too tedious and time consuming. To work around this I’m creating the unused columns as the errors are generated, importing the data by running the query, then dropping the unused columns when I’m done with that table. I’ve looked at INSERT IGNORE, but this seems to be for ignoring duplicate keys (not what I’m looking for).
Is there any way to preform an INSERT while ignoring columns that don’t exist in the target table? I’m looking for something “painless”, like some existing SQL functionality.
To clarify, I’m working with a bunch of backup files and importing the data to a local database for testing before moving it to the live server. Example of the kind of solution I’m hoping for:
-- Don't try to insert data from columns that don't exist in "new_table"
INSERT INTO `new_table` {IGNORE UNKNOWN COLUMNS} (`id`, `col1`, `col2`) VALUES
(1, '', ''),
(2, '', '');
If something like this simply doesn’t exist, I’m happy to accept that as an answer and continue to use my current workaround.
Your current technique seems practical enough. Just one small change.
Rather than waiting for error and then creating columns one by one, you can just export the schema, do a diff and find out all the missing columns in all the tables.
That way it would be less work.
Your gui will be capable of exporting just schema or the following switch on mysqldump will be useful to find out all the missing columns.
Diffing the dbdump1.sql and dbdump2.sql will give you all the differences in both the databases.