Merge lp:~mvo/pkgme-devportal/schemas-via-storm into lp:~mvo/pkgme-devportal/more-architectures

Proposed by Michael Vogt
Status: Work in progress
Proposed branch: lp:~mvo/pkgme-devportal/schemas-via-storm
Merge into: lp:~mvo/pkgme-devportal/more-architectures
Diff against target: 67 lines (+17/-22)
2 files modified
devportalbinary/database.py (+13/-22)
devportalbinary/migrations/patch_0.py (+4/-0)
To merge this branch: bzr merge lp:~mvo/pkgme-devportal/schemas-via-storm
Reviewer Review Type Date Requested Status
Michael Vogt Pending
Review via email: mp+116652@code.launchpad.net

Description of the change

This branch uses storm to manage the db schema and adds a migration patch from the previous schema without the architecture column to the new schema that adds it.

Please note that this needs running:
"""
CREATE TABLE patch (version INTEGER NOT NULL PRIMARY KEY)
"""
on the database first to ensure that storm does not assumes the DB is totally empty.

This will allow us to do schema updates using devportalbinary/migrations/patch_N.py based files that can alter the DB via a apply() function to do the required schema changes. It internally keeps track of what is applied and what is missing etc.

To post a comment you must log in.
70. By Michael Vogt

devportalbinary/database.py: add comment, remove adding the patches table if its missing and leave that to the webops via the MP

Revision history for this message
Michael Vogt (mvo) wrote :

This branch is on hold for now as discussed on irc:

<jml> mvo: in any case, we won't be using it for this branch though. Maybe best to shelve it until we have another schema change?

But it should be re-considered for the next schema update.

Unmerged revisions

70. By Michael Vogt

devportalbinary/database.py: add comment, remove adding the patches table if its missing and leave that to the webops via the MP

69. By Michael Vogt

use storm.schema to manage the db and add migrate script

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'devportalbinary/database.py'
2--- devportalbinary/database.py 2012-07-24 14:45:01 +0000
3+++ devportalbinary/database.py 2012-07-25 14:10:24 +0000
4@@ -18,7 +18,10 @@
5 )
6 from launchpadlib.launchpad import Launchpad
7 from pkgme.run_script import run_subprocess
8-from storm.locals import create_database, Store
9+from storm.locals import create_database, Store, StormError
10+from storm.schema import Schema
11+
12+import devportalbinary.migrations
13
14 from .configuration import (
15 CONF_FILE_ENV_VAR,
16@@ -318,32 +321,20 @@
17 if store is None:
18 options = load_configuration().options
19 store, store_type = cls.get_store_from_config(options)
20- if store_type == 'sqlite':
21- store.execute(
22- "CREATE TABLE IF NOT EXISTS libdep ("
23- "source_package_name TEXT, "
24- "library TEXT, "
25- "dependency TEXT, "
26- "architecture TEXT, "
27- "CONSTRAINT libdep_uniq UNIQUE (source_package_name, "
28- "library, dependency))")
29- elif store_type == 'postgres':
30- # Postgres only supports IF NOT EXISTS starting from 9.1
31- exists = store.execute(
32- "SELECT COUNT(relname) FROM pg_class WHERE "
33- "relname = 'libdep'").get_one()[0]
34- if not exists:
35- store.execute(
36- "CREATE TABLE libdep ("
37+ # use storm to manage the db schema, this allows us to do migrations
38+ # using devportalbinary.migrations.patch_N.py files
39+ creates = [ "CREATE TABLE libdep ("
40 "source_package_name TEXT, "
41 "library TEXT, "
42 "dependency TEXT, "
43 "architecture TEXT, "
44 "CONSTRAINT libdep_uniq UNIQUE (source_package_name, "
45- "library, dependency))")
46- else:
47- raise AssertionError("Unsupported database type: %s" % store_type)
48- store.commit()
49+ "library, dependency))"
50+ ]
51+ drops = [ "DROP TABLE libdep" ]
52+ deletes = [ "DELETE FROM libdep" ]
53+ schema = Schema(creates, drops, deletes, devportalbinary.migrations)
54+ schema.upgrade(store)
55 return cls(store)
56
57 def get_dependencies(self, library_name, arch='i386'):
58
59=== added directory 'devportalbinary/migrations'
60=== added file 'devportalbinary/migrations/patch_0.py'
61--- devportalbinary/migrations/patch_0.py 1970-01-01 00:00:00 +0000
62+++ devportalbinary/migrations/patch_0.py 2012-07-25 14:10:24 +0000
63@@ -0,0 +1,4 @@
64+def apply(store):
65+ store.execute('ALTER TABLE libdep ADD architecture TEXT;')
66+ store.execute('UPDATE libdep SET architecture = "i386";')
67+

Subscribers

People subscribed via source and target branches

to all changes: