Database upgrade: Store section names in the DB

This makes it possible to fix 931 later on - right
now, users cannot edit the section names in the UI,
but only via editing the database file directly.
This commit is contained in:
Thomas Perl 2011-07-27 15:36:04 +02:00
parent a18a718b32
commit 7775202666
5 changed files with 68 additions and 21 deletions

View File

@ -91,7 +91,7 @@ class Database(object):
self._db.text_factory = str
# Check schema version, upgrade if necessary
schema.upgrade(self._db)
schema.upgrade(self._db, self.database_file)
logger.debug('Database opened.')
return self._db
@ -111,7 +111,7 @@ class Database(object):
"""Given a podcast ID, returns the content types"""
with self.lock:
cur = self.cursor()
cur.execute('SELECT DISTINCT mime_type AS mime_type FROM %s WHERE podcast_id = ?' % self.TABLE_EPISODE, (id,))
cur.execute('SELECT mime_type FROM %s WHERE podcast_id = ?' % self.TABLE_EPISODE, (id,))
for (mime_type,) in cur:
yield mime_type
cur.close()

View File

@ -648,10 +648,7 @@ class PodcastListModel(gtk.ListStore):
if config.podcast_list_sections:
def convert(channels):
for channel in channels:
# TODO: Maybe allow user-specified section name
# here (e.g. via hooks or integrated into the DB
# model and determined at first feed update)
yield (channel._get_content_type(), channel)
yield (channel.group_by, channel)
else:
def convert(channels):
for channel in channels:

View File

@ -688,6 +688,8 @@ class PodcastChannel(PodcastModelObject):
self.download_folder = None
self.pause_subscription = False
self.section = _('Other')
def _get_db(self):
return self.parent
@ -808,6 +810,7 @@ class PodcastChannel(PodcastModelObject):
tmp.save()
tmp.update(max_episodes, mimetype_prefs)
tmp.section = tmp._get_content_type()
# Mark episodes as downloaded if files already exist (bug 902)
tmp.import_external_files()
@ -1045,15 +1048,34 @@ class PodcastChannel(PodcastModelObject):
else:
return self.db.get_podcast_statistics(self.id)
@property
def group_by(self):
if not self.section:
self.section = self._get_content_type()
self.save()
return self.section
def _get_content_type(self):
if 'youtube.com' in self.url:
return 'video'
return _('Video')
content_types = self.db.get_content_types(self.id)
result = ' and '.join(sorted(set(x.split('/')[0].lower() for x in content_types if not x.startswith('application'))))
if result == '':
return 'other'
return result
audio, video, other = 0, 0, 0
for content_type in self.db.get_content_types(self.id):
content_type = content_type.lower()
if content_type.startswith('audio'):
audio += 1
elif content_type.startswith('video'):
video += 1
else:
other += 1
if audio >= video:
return _('Audio')
elif video > other:
return _('Video')
return _('Other')
def authenticate_url(self, url):
return util.url_add_authentication(url, self.auth_username, self.auth_password)

View File

@ -222,7 +222,6 @@ class QPodcast(QObject):
QObject.__init__(self)
self._podcast = podcast
self._updating = False
self._section_cached = None
@classmethod
def sort_key(cls, qpodcast):
@ -301,9 +300,7 @@ class QPodcast(QObject):
qdescription = Property(unicode, _description, notify=changed)
def _section(self):
if self._section_cached is None:
self._section_cached = convert(self._podcast._get_content_type())
return self._section_cached
return convert(self._podcast.group_by)
qsection = Property(unicode, _section, notify=changed)

View File

@ -22,6 +22,9 @@
from sqlite3 import dbapi2 as sqlite
import time
import shutil
EpisodeColumns = (
'podcast_id',
'title',
@ -55,8 +58,10 @@ PodcastColumns = (
'auto_archive_episodes',
'download_folder',
'pause_subscription',
'section',
)
CURRENT_VERSION = 2
def initialize_database(db):
# Create table for podcasts
@ -74,7 +79,8 @@ def initialize_database(db):
http_etag TEXT NULL DEFAULT NULL,
auto_archive_episodes INTEGER NOT NULL DEFAULT 0,
download_folder TEXT NOT NULL DEFAULT '',
pause_subscription INTEGER NOT NULL DEFAULT 0
pause_subscription INTEGER NOT NULL DEFAULT 0,
section TEXT NOT NULL DEFAULT ''
)
""")
@ -125,17 +131,42 @@ def initialize_database(db):
# Create table for version info / metadata + insert initial data
db.execute("""CREATE TABLE version (version integer)""")
db.execute("""INSERT INTO version (version) VALUES (1)""")
db.execute("INSERT INTO version (version) VALUES (%d)" % CURRENT_VERSION)
db.commit()
def upgrade(db):
def upgrade(db, filename):
if not list(db.execute('PRAGMA table_info(version)')):
initialize_database(db)
return
result = db.execute('SELECT version FROM version').fetchone()
if result[0] != 1:
version = db.execute('SELECT version FROM version').fetchone()[0]
if version == CURRENT_VERSION:
return
# We are trying an upgrade - save the current version of the DB
backup = '%s_upgraded-v%d_%d' % (filename, int(version), int(time.time()))
try:
shutil.copy(filename, backup)
except Exception, e:
raise Exception('Cannot create DB backup before upgrade: ' + e)
db.execute("DELETE FROM version")
if version == 1:
UPGRADE_V1_TO_V2 = """
ALTER TABLE podcast ADD COLUMN section TEXT NOT NULL DEFAULT ''
"""
for sql in UPGRADE_V1_TO_V2.strip().split('\n'):
db.execute(sql)
version = 2
db.execute("INSERT INTO version (version) VALUES (%d)" % version)
db.commit()
if version != CURRENT_VERSION:
raise Exception('Database schema version unknown')