1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

Fixed mysql migration removing not null constraints

This commit is contained in:
spl0k 2017-12-31 16:24:46 +01:00
parent d807f26ccb
commit 9de0008d13

View File

@ -26,20 +26,26 @@ parser.add_argument('database')
parser.add_argument('-H', '--host', default = 'localhost', help = 'default: localhost') parser.add_argument('-H', '--host', default = 'localhost', help = 'default: localhost')
args = parser.parse_args() args = parser.parse_args()
def process_table(connection, table, fields): def process_table(connection, table, fields, nullable_fields = ()):
to_update = { field: set() for field in fields } to_update = { field: set() for field in fields + nullable_fields }
c = connection.cursor() c = connection.cursor()
c.execute('SELECT {1} FROM {0}'.format(table, ','.join(fields))) c.execute('SELECT {1} FROM {0}'.format(table, ','.join(fields + nullable_fields)))
for row in c: for row in c:
for field, value in zip(fields, row): for field, value in zip(fields + nullable_fields, row):
if value is None or not isinstance(value, basestring): if value is None or not isinstance(value, basestring):
continue continue
to_update[field].add(value) to_update[field].add(value)
for field, values in to_update.iteritems(): for field, values in to_update.iteritems():
if not values:
continue
sql = 'UPDATE {0} SET {1}=%s WHERE {1}=%s'.format(table, field) sql = 'UPDATE {0} SET {1}=%s WHERE {1}=%s'.format(table, field)
c.executemany(sql, map(lambda v: (UUID(v).bytes, v), values)) c.executemany(sql, map(lambda v: (UUID(v).bytes, v), values))
for field in fields:
sql = 'ALTER TABLE {0} MODIFY {1} BINARY(16) NOT NULL'.format(table, field)
c.execute(sql)
for field in nullable_fields:
sql = 'ALTER TABLE {0} MODIFY {1} BINARY(16)'.format(table, field) sql = 'ALTER TABLE {0} MODIFY {1} BINARY(16)'.format(table, field)
c.execute(sql) c.execute(sql)
@ -49,11 +55,11 @@ filterwarnings('ignore', category = provider.Warning)
conn = provider.connect(host = args.host, user = args.username, passwd = args.password, db = args.database) conn = provider.connect(host = args.host, user = args.username, passwd = args.password, db = args.database)
conn.cursor().execute('SET FOREIGN_KEY_CHECKS = 0') conn.cursor().execute('SET FOREIGN_KEY_CHECKS = 0')
process_table(conn, 'folder', ('id', 'parent_id')) process_table(conn, 'folder', ('id',), ('parent_id',))
process_table(conn, 'artist', ('id',)) process_table(conn, 'artist', ('id',))
process_table(conn, 'album', ('id', 'artist_id')) process_table(conn, 'album', ('id', 'artist_id'))
process_table(conn, 'track', ('id', 'album_id', 'artist_id', 'root_folder_id', 'folder_id')) process_table(conn, 'track', ('id', 'album_id', 'artist_id', 'root_folder_id', 'folder_id'))
process_table(conn, 'user', ('id', 'last_play_id')) process_table(conn, 'user', ('id',), ('last_play_id',))
process_table(conn, 'client_prefs', ('user_id',)) process_table(conn, 'client_prefs', ('user_id',))
process_table(conn, 'starred_folder', ('user_id', 'starred_id')) process_table(conn, 'starred_folder', ('user_id', 'starred_id'))
process_table(conn, 'starred_artist', ('user_id', 'starred_id')) process_table(conn, 'starred_artist', ('user_id', 'starred_id'))