Skip to content

Commit

Permalink
DB schema: Cleaner upgrades, fix column order
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed Jul 13, 2012
1 parent beaf1a6 commit f069abc
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/gpodder/schema.py
Expand Up @@ -65,6 +65,25 @@

CURRENT_VERSION = 3


# SQL commands to upgrade old database versions to new ones
# Each item is a tuple (old_version, new_version, sql_commands) that should be
# applied to the database to migrate from old_version to new_version.
UPGRADE_SQL = [
# Version 2: Section labels for the podcast list
(1, 2, """
ALTER TABLE podcast ADD COLUMN section TEXT NOT NULL DEFAULT ''
"""),

# Version 3: Flattr integration (+ invalidate http_* fields to force
# a feed update, so that payment URLs are parsed during the next check)
(2, 3, """
ALTER TABLE podcast ADD COLUMN payment_url TEXT NULL DEFAULT NULL
ALTER TABLE episode ADD COLUMN payment_url TEXT NULL DEFAULT NULL
UPDATE podcast SET http_last_modified=NULL, http_etag=NULL
"""),
]

def initialize_database(db):
# Create table for podcasts
db.execute("""
Expand All @@ -75,15 +94,15 @@ def initialize_database(db):
link TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL DEFAULT '',
cover_url TEXT NULL DEFAULT NULL,
payment_url TEXT NULL DEFAULT NULL,
auth_username TEXT NULL DEFAULT NULL,
auth_password TEXT NULL DEFAULT NULL,
http_last_modified TEXT NULL DEFAULT NULL,
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,
section TEXT NOT NULL DEFAULT ''
section TEXT NOT NULL DEFAULT '',
payment_url TEXT NULL DEFAULT NULL
)
""")

Expand All @@ -103,7 +122,6 @@ def initialize_database(db):
title TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL DEFAULT '',
url TEXT NOT NULL,
payment_url TEXT NULL DEFAULT NULL,
published INTEGER NOT NULL DEFAULT 0,
guid TEXT NOT NULL,
link TEXT NOT NULL DEFAULT '',
Expand All @@ -116,7 +134,8 @@ def initialize_database(db):
total_time INTEGER NOT NULL DEFAULT 0,
current_position INTEGER NOT NULL DEFAULT 0,
current_position_updated INTEGER NOT NULL DEFAULT 0,
last_playback INTEGER NOT NULL DEFAULT 0
last_playback INTEGER NOT NULL DEFAULT 0,
payment_url TEXT NULL DEFAULT NULL
)
""")

Expand Down Expand Up @@ -157,27 +176,13 @@ def upgrade(db, filename):

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

if version == 2:
UPGRADE_V2_TO_V3 = """
ALTER TABLE podcast ADD COLUMN payment_url TEXT NULL DEFAULT NULL;
ALTER TABLE episode ADD COLUMN payment_url TEXT NULL DEFAULT NULL;
UPDATE podcast SET http_last_modified=NULL, http_etag=NULL;
"""

for sql in UPGRADE_V2_TO_V3.strip().split('\n'):
db.execute(sql)
for old_version, new_version, upgrade in UPGRADE_SQL:
if version == old_version:
for sql in upgrade.strip().split('\n'):
db.execute(sql)
version = new_version

version = 3
assert version == CURRENT_VERSION

db.execute("INSERT INTO version (version) VALUES (%d)" % version)
db.commit()
Expand Down Expand Up @@ -218,6 +223,7 @@ def convert_gpodder2_db(old_db, new_db):
row['foldername'],
not row['feed_update_enabled'],
'',
None,
)
new_db.execute("""
INSERT INTO podcast VALUES (%s)
Expand Down Expand Up @@ -248,6 +254,7 @@ def convert_gpodder2_db(old_db, new_db):
row['current_position'],
row['current_position_updated'],
0,
None,
)
new_db.execute("""
INSERT INTO episode VALUES (%s)
Expand Down

0 comments on commit f069abc

Please sign in to comment.