tags:

views:

73

answers:

4

I am working on a small database application in Python (currently targeting 2.5 and 2.6) using sqlite3.

It would be helpful to be able to provide a series of functions that could setup the database and validate that it matches the current schema. Before I reinvent the wheel, I thought I'd look around for libraries that would provide something similar. I'd love to have something akin to RoR's migrations. xml2ddl doesn't appear to be meant as a library (although it could be used that way), and more importantly doesn't support sqlite3. I'm also worried about the need to move to Python 3 one day given the lack of recent attention to xml2ddl.

Are there other tools around that people are using to handle this?

+1  A: 

I use this to keep schemas in sync.

Keep in mind that it adds a metadata table to keep track of the versions.

Koba
+3  A: 

You can find the schema of a sqlite3 table this way:

import sqlite3
db = sqlite3.connect(':memory:')
c = db.cursor()
c.execute('create table foo (bar integer, baz timestamp)')
c.execute("select sql from sqlite_master where type = 'table' and name = 'foo'")
r=c.fetchone()
print(r)
# (u'CREATE TABLE foo (bar integer, baz timestamp)',)
unutbu
This solves one of the problems (being able to compare existing), but this doesn't give me a way to keep the database schema out of the program itself.
acrosman
@acrosman: Could you elaborate? I don't understand what "keep the database schema out of the program" means.
unutbu
I would like to keep statements like "create table foo (bar integer, baz timestamp)" out of the code. I'd rather have another file (XML or similar) that is parsed into dynamically generated SQL.
acrosman
I would just save the SQL to a file, but then again, perhaps I don't fully understand your requirements.
unutbu
A: 

South is the closest I know to RoR migrations. But just as you need Rails for those migrations, you need django to use south.

Olivier
+2  A: 

Take a look at SQLAlchemy migrate. I see no problem using it as migration tool only, but comparing of configuration to current database state is experimental yet.

Denis Otkidach