====== MantisBT Schema Comparison and Update ====== This pages describes a process to compare a given MantisBT schema with a standard installation, and update it if necessary. The SQL was written for a MySQL installation, and would not work on other RDBMS without adjustments due to use of metadata. The script and process was tested using a dump from mantisbt.org bugs database taken on 13-Sep-2013. ===== Preparation ===== * First thing, **backup** the schema to compare and update, e.g. mysqldump bugtracker >backup.sql * Perform a fresh install of MantisBT to use a reference, in a separate schema, e.g. //mantis_12x// * Copy the SQL script below to your favorite editor /* * Detect differences betweeen a reference MantisBT schema and a target schema * and generate a SQL script to fix the target schema so that it is identical * to the reference one. * * Usage * 1. Update the current and reference schemas * 2. Save the script * 3. Run it from command line: * mysql -N < this_script.sql >update_script.sql * 4. Update the database * mysql target_schema -v 'mantis_upgrade_table' ) delta; -- --------------------------------------------------------------------- SELECT '-- Indexes'; -- autoincrement ? SELECT CASE WHEN ISNULL(std_12x) THEN -- Drop indexes not existing in standard 1.2.x CONCAT_WS(' ', 'ALTER TABLE', tbl, 'DROP INDEX', idx, ';') WHEN ISNULL(curr_val) THEN -- Create missing indexes existing in standard 1.2.x CONCAT_WS(' ', 'CREATE', IF(non_unique = 0, 'UNIQUE', ''), 'INDEX', idx, 'ON', tbl, '(', std_12x, ')', ';' ) ELSE CONCAT('-- @TODO: Index update ', idx) END "Update indexes" FROM ( SELECT org.sch, org.tbl, org.idx, org.cols curr_val, 12x.cols std_12x, 12x.non_unique FROM (SELECT * FROM v_indexes WHERE sch = @schema_to_update) org LEFT JOIN (SELECT * FROM v_indexes WHERE sch = @schema_reference) 12x ON org.tbl = 12x.tbl AND org.idx = 12x.idx WHERE isnull(12x.cols) OR org.cols <> 12x.cols UNION -- Emulate full outer join SELECT 12x.sch, 12x.tbl, 12x.idx, org.cols curr_val, 12x.cols std_12x, 12x.non_unique FROM (SELECT * FROM v_indexes WHERE sch = @schema_to_update) org RIGHT JOIN (SELECT * FROM v_indexes WHERE sch = @schema_reference) 12x ON org.tbl = 12x.tbl AND org.idx = 12x.idx WHERE org.cols IS NULL ORDER BY 2,3 ) delta WHERE tbl IN (SELECT tbl FROM v_tables WHERE std_12x IS NOT NULL); -- --------------------------------------------------------------------- SELECT '-- Drop Temporary Helper views'; DROP VIEW v_tables; DROP VIEW v_indexes; DROP FUNCTION sch_12x; DROP FUNCTION sch_upd; * Update the schema names as appropriate * Save the Generation Script on your computer ===== Process execution ===== * Create the Update Script (using the Generation Script created in the previous section); this will generate a series of SQL commands to update the schema mysql -N /path/to/update_script.sql Note: temporary functions and views are created by the script in the current schema, then deleted at the end of execution. * If you just want to compare the definitions of the schemas, you can use the sub-select statements at each step. * Align the schema to the reference using the Update Script mysql bugtracker -v * Alternatively, you can combine the two steps mysql -N At this point, the two schemas should be identical. Double-check, test and restore from backup in case of issues. ===== Notes ===== ==== Comparing Indexes ==== SELECT table_schema, table_name, index_name, non_unique, seq_in_index, column_name, sub_part, packed, nullable FROM information_schema.statistics order by 1,2,3,5 ==== Updating mantisbt.org bugtracker ==== Excluding plugin-specific tables, the following differences were found * 2 extra tables (mantis_tasks_table, mantis_upgrade_table) * 49 differences in indexes (extra, missing, different name, different definition, e.g. unique) * 130 column type differences, * 61 default values differences * 1 column which should be "not null" but is not Additionally, when attempting to create a missing unique index on username column, 2 cases of duplicate keys in mantis_user_table were discovered, (users //updater//, 2 records, and //dmok//, 3 records). After carefully checking that these accounts were not referenced anywhere in the database, the extra records were deleted through Manage Users page (keeping only the one that was created first). As of this writing, the official tracker has not yet been updated; a message was sent to the mantisbt-dev mailing list to get feedback on the process first.