forked from johm/infoshopkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbupgradeauthors.py
executable file
·61 lines (49 loc) · 1.71 KB
/
dbupgradeauthors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import MySQLdb
import etc
new_db_conn=MySQLdb.connect (host = etc.dbhost,db=etc.dbname,user=etc.dbuser,passwd=etc.dbpass)
new=new_db_conn.cursor()
new.execute("""
CREATE TABLE `author_title` (
`author_id` int(11) default NULL,
`title_id` int(11) default NULL,
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
""")
new.execute("""
RENAME TABLE author TO old_author
"""
)
new.execute("""
CREATE TABLE `author` (
`title_id` int(11) default NULL,
`author_name` varchar(255) default NULL,
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`id`),
KEY `title_id` (`title_id`),
FULLTEXT KEY `author_name` (`author_name`)
)ENGINE=MyISAM DEFAULT CHARSET=latin1""")
new.execute ("SELECT author_name,title_id,id FROM old_author")
rows = new.fetchall()
for row in rows:
authorname=row[0]
title_id=row[1]
oldauthorid=row[2]
print "Adapting old record for %s, %s" % (authorname,title_id)
new.execute("SELECT id FROM author WHERE author_name=%s",(authorname))
existing_author_records=new.fetchall()
new_author_id=0
if len(existing_author_records)>1:
print "Anomalous author count for %s!" % (oldauthorid)
if len(existing_author_records)==0:
new.execute("INSERT INTO author (author_name) VALUES(%s)",authorname)
new.execute("SELECT LAST_INSERT_ID()")
new_author_id=new.fetchone()[0]
else:
new_author_id=existing_author_records[0][0]
if new_author_id==0:
print "Anomalous new author id for %s!" % (oldauthorid)
else:
new.execute("INSERT INTO author_title (author_id,title_id) VALUES (%s,%s)",(new_author_id,title_id))
print "Processed old record %s into new record %s (%s)" % (oldauthorid,new_author_id,authorname)