Ho scritto un piccolo script python che converte questo:
LOCK TABLES `actor` WRITE;
/*!40000 ALTER TABLE `actor` DISABLE KEYS */;
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (1,'PENELOPE','GUINESS','2006-02-15 12:34:33');
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (2,'NICK','WAHLBERG','2006-02-15 12:34:33');
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (3,'ED','CHASE','2006-02-15 12:34:33');
in questo:
LOCK TABLES `actor` WRITE;
/*!40000 ALTER TABLE `actor` DISABLE KEYS */;
INSERT INTO `actor` VALUES(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),(2,'NICK','WAHLBERG','2006-02-15 12:34:33'),(3,'ED','CHASE','2006-02-15 12:34:33');
Non è molto carino o ben testato, ma funziona su Sakila testare dump di database , quindi può gestire file di dump non banali.
Ad ogni modo, ecco lo script:
#!/usr/bin/env python
# -*- coding: utf-8 -*- #
import re
import sys
re_insert = re.compile(r'^insert into `(.*)` \(.*\) values (.*);', re.IGNORECASE)
current_table = ''
for line in sys.stdin:
if line.startswith('INSERT INTO'):
m = re_insert.match(line)
table = m.group(1)
values = m.group(2)
if table != current_table:
if current_table != '':
sys.stdout.write(";\n\n")
current_table = table
sys.stdout.write('INSERT INTO `' + table + '` VALUES ' + values)
else:
sys.stdout.write(',' + values)
else:
if current_table != '':
sys.stdout.write(";\n")
current_table = ''
sys.stdout.write(line)
if current_table != '':
sys.stdout.write(';')
Si aspetta un input tramite pipe su stdin e stampa su stdout. Se hai salvato lo script come mysqldump-convert.py
, lo useresti in questo modo:
cat ./sakila-db/sakila-full-dump.sql | python mysqldump-convert.py > test.sql
Fammi sapere come ti trovi!