I just committed some new experimental code for metadata/database comparison (issue #12)

280 views
Skip to first unread message

Christian Simms

unread,
Mar 22, 2008, 10:18:12 AM3/22/08
to migrat...@googlegroups.com
I added three new shell commands to the migrate script:

1. compare_db - Compare the current model (assumed to be a module
level variable of type sqlalchemy.MetaData) against the current
database.
2. db_schema_dump - Dump the current database as a Python model.
3. script_python_changes - Create a script changing the current (old)
database to the current (new) Python model.

Two commands (compare_db and script_python_changes) require a model
parameter -- when you run the "manage" command to create a script, you
can now specify the Python module variable that has the MetaData for
your Python model (using the --model parameter), like this:

migrate manage manage.py --repository=repo --url=sqlite:///proj.db
--model=proj.model.metadata

Above, it assumes that "proj.model" is your module and "metadata" is a
variable in that module.

The code is experimental because I'm not completely happy with the
command names and I didn't implement checking foreign keys and
indices. Also, a lot of code for generating the python model was
borrowed from the sqlautocode project. In fact, I believe the
db_schema_dump is basically the sqlautocode project. I couldn't see
any easy way to use the code from that project, plus it seemed kind of
weird to make that project be a dependency of sqlalchemy-migrate.

I'd love any feedback.

Cheers,
Christian

Mark Ramm

unread,
Mar 22, 2008, 7:29:57 PM3/22/08
to migrat...@googlegroups.com
Wow! That is very cool stuff. Thanks for working on it.

Perhaps the commands could be named:

compare_to_db model.py
create_model model.py
make_update_script_for model.py

It also seems like you can pass a model to all three, and create_model
would output the model code to the file you name. That provides a
nice symmetry between the three commands...

Again, thanks for the contribution.

--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

Christian Simms

unread,
Mar 24, 2008, 10:45:21 AM3/24/08
to migrat...@googlegroups.com
Thanks to Mark's advice, I renamed the commands and added a new one:

- compare_db -> compare_model_to_db
- db_schema_dump -> create_model
- script_python_changes -> make_update_script_for_model
- new command: update_db_from_model

The new command update_db_from_model tries to modify the database so
that it matches your current Python model -- hopefully subsuming the
ModelUpdate usage recipe (
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/ModelUpdate ). I
even wrote code for sqlite (one of my favorite open source dbs) so
that if a column needs to be dropped or modified (which sqlite doesn't
support) then the code goes to the trouble of creating a temp table,
copying the data over, dropping + recreating the table with the new
structure, moving the data and dropping the temp table.

Cheers,
Christian

Reply all
Reply to author
Forward
0 new messages