开发者

How to import a mysql dump while renaming some tables/columns and not importing others at all?

开发者 https://www.devze.com 2023-01-08 13:08 出处:网络
I\'m importing a legacy db to a new version of our program, and I\'m wondering if there\'s a way to开发者_开发问答 not import some columns/tables from the dump, and rename other tables/columns as i im

I'm importing a legacy db to a new version of our program, and I'm wondering if there's a way to开发者_开发问答 not import some columns/tables from the dump, and rename other tables/columns as i import them? I'm aware I could edit the dump file in theory, but that seems like a hack, and so far none of my editors can handle opening the 1.3 gb file (Yes, I've read the question about that on here. No, none of the answers worked for me so far.).

Suggestions?


It's possible to not import some tables by denying permissions to do so, and using --force as a command line option.

Not importing some columns, or renaming them is not possible (at least without editing the dump file, or making modifications once imported).

My recommendation would be:

  • Import the tables into another database (1.3G should still be very quick).
  • Do your dropping/renaming.
  • Export the data to create yourself a new dump file.

If you're worried the dump contains multiple databases, the mysql command line tool has a -o flag to only import the one.


I'd say import it into a temporary database and do the changes live - possibly applying a pre-built script that does the necessary operations:

DROP TABLE ....
DROP TABLE ....
DROP TABLE ....
ALTER TABLE ..... DROP column ....

Then copy the finished result into the production database.

This can be very nicely automated as well.

It's likely to work out faster and with less problems than finding a tool that edits dumps (or, as so often with these things, trying out five different tools and finding out none works well).


Assuming you have both databases, you could rename all tables in OldDB (just make sure the prefix isn't used already in any table name, because renaming back has a string-replace) …

USE olddb;

DROP PROCEDURE IF EXISTS rename_tables;
DELIMITER ||
CREATE PROCEDURE rename_tables(
    IN plz_remove BOOLEAN
)
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tab VARCHAR(64);
    DECLARE mycursor CURSOR FOR
        SELECT table_name FROM information_schema.tables
            WHERE table_schema = (SELECT DATABASE() FROM DUAL)
    ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN mycursor;

    myloop: LOOP
        FETCH mycursor INTO tab;
        IF done THEN
            LEAVE myloop;
        END IF;

        IF plz_remove THEN
            SET @sql = CONCAT(
                'RENAME TABLE ', tab, ' TO ', REPLACE(tab, 'olddb_', '')
            );

        ELSE
            SET @sql = CONCAT('RENAME TABLE ', tab, ' TO olddb_', tab);
        END IF;

        -- construct due to RENAME × CONCAT / variables.
        PREPARE s FROM @sql;
        EXECUTE s;

    END LOOP;

    CLOSE mycursor;
END ||
DELIMITER ;

-- append 'olddb_'.
CALL rename_tables(false);

-- […]
-- rename back after dump.
CALL rename_tables(true);

… then dump and import into NewDB.

$ mysqldump -hlocalhost -uroot -p  --complete-insert  --routines  --default-character-set=utf8  olddb > olddb.sql
$ mysql -hlocalhost -uroot -p  --default-character-set=utf8  newdb < olddb.sql

This would give you (for example):

USE newdb;
SHOW TABLES;

+------------------+
| Tables_in_newdb  |
+------------------+
| bar              |
| foo              |
| olddb_company    |
| olddb_department |
| olddb_user       |
| user             |
+------------------+

Further reading / based on:

  • MySQL Manual: Cursors
  • MySQL Manual: Loop
  • MySQL Manual: Prepare
  • SO answer for "MySQL foreach alternative for procedure"
  • SO answer for "MySQL foreach loop"
  • SO answer for "How do you mysqldump specific table(s)?"
0

精彩评论

暂无评论...
验证码 换一张
取 消