diff --git a/beets/library.py b/beets/library.py index 4cc1f7204..654eab786 100644 --- a/beets/library.py +++ b/beets/library.py @@ -845,7 +845,7 @@ class Library(BaseLibrary): # No table exists. setup_sql = 'CREATE TABLE %s (' % table setup_sql += ', '.join(['%s %s' % f[:2] for f in fields]) - setup_sql += ');' + setup_sql += ');\n' else: # Table exists but is missing fields. @@ -858,6 +858,13 @@ class Library(BaseLibrary): assert False setup_sql += 'ALTER TABLE %s ' % table setup_sql += 'ADD COLUMN %s %s;\n' % field[:2] + + # Special case. If we're moving from a version without + # albumartist, copy all the "artist" values to "albumartist" + # values on the album data structure. + if table == 'albums' and 'artist' in current_fields and \ + 'albumartist' not in current_fields: + setup_sql += "UPDATE ALBUMS SET albumartist=artist;\n" self.conn.executescript(setup_sql) self.conn.commit() diff --git a/beets/ui/commands.py b/beets/ui/commands.py index a22973453..7b3af442c 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -627,10 +627,10 @@ def list_items(lib, query, album): """ if album: for album in lib.albums(query=query): - print_(album.artist + ' - ' + album.album) + print_(album.albumartist + u' - ' + album.album) else: for item in lib.items(query=query): - print_(item.artist + ' - ' + item.album + ' - ' + item.title) + print_(item.artist + u' - ' + item.album + u' - ' + item.title) list_cmd = ui.Subcommand('list', help='query the library', aliases=('ls',)) list_cmd.parser.add_option('-a', '--album', action='store_true', diff --git a/test/test_db.py b/test/test_db.py index 4463cf680..612c7e824 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -379,6 +379,34 @@ class MigrationTest(unittest.TestCase): except sqlite3.OperationalError: self.fail("select failed") + def test_album_data_preserved(self): + conn = sqlite3.connect(self.libfile) + conn.execute('drop table albums') + conn.execute('create table albums (id primary key, album)') + conn.execute("insert into albums values (1, 'blah')") + conn.commit() + conn.close() + + new_lib = beets.library.Library(self.libfile, + item_fields=self.newer_fields) + albums = new_lib.conn.execute('select * from albums').fetchall() + self.assertEqual(len(albums), 1) + self.assertEqual(albums[0][1], 'blah') + + def test_move_artist_to_albumartist(self): + conn = sqlite3.connect(self.libfile) + conn.execute('drop table albums') + conn.execute('create table albums (id primary key, artist)') + conn.execute("insert into albums values (1, 'theartist')") + conn.commit() + conn.close() + + new_lib = beets.library.Library(self.libfile, + item_fields=self.newer_fields) + c = new_lib.conn.execute("select * from albums") + album = c.fetchone() + self.assertEqual(album['albumartist'], 'theartist') + class AlbumInfoTest(unittest.TestCase): def setUp(self): self.lib = beets.library.Library(':memory:') @@ -546,4 +574,3 @@ def suite(): if __name__ == '__main__': unittest.main(defaultTest='suite') - diff --git a/test/test_ui.py b/test/test_ui.py index 6f92a484a..a719bf8a0 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -34,6 +34,7 @@ class ListTest(unittest.TestCase): self.lib = library.Library(':memory:') i = test_db.item() self.lib.add(i) + self.lib.add_album([i]) def tearDown(self): self.io.restore() @@ -42,12 +43,29 @@ class ListTest(unittest.TestCase): commands.list_items(self.lib, '', False) out = self.io.getoutput() self.assertTrue(u'the title' in out) + + def test_list_album_outputs_something(self): + commands.list_items(self.lib, '', True) + out = self.io.getoutput() + self.assertGreater(len(out), 0) def test_list_album_omits_title(self): commands.list_items(self.lib, '', True) out = self.io.getoutput() self.assertTrue(u'the title' not in out) + + def test_list_uses_track_artist(self): + commands.list_items(self.lib, '', False) + out = self.io.getoutput() + self.assertTrue(u'the artist' in out) + self.assertTrue(u'the album artist' not in out) + def test_list_album_uses_album_artist(self): + commands.list_items(self.lib, '', True) + out = self.io.getoutput() + self.assertTrue(u'the artist' not in out) + self.assertTrue(u'the album artist' in out) + class PrintTest(unittest.TestCase): def setUp(self): self.io = _common.DummyIO()