简体   繁体   English

使用Python部署数据库更改

[英]Deploying database changes with Python

I'm wondering if someone can recommend a good pattern for deploying database changes via python. 我想知道是否有人可以通过python推荐一个好的模式来部署数据库更改。

In my scenario, I've got one or more PostgreSQL databases and I'm trying to deploy a code base to each one. 在我的场景中,我有一个或多个PostgreSQL数据库,我正在尝试为每个数据库部署代码库。 Here's an example of the directory structure for my SQL scripts: 这是我的SQL脚本的目录结构示例:


my_db/
    main.sql
    some_directory/
        foo.sql
        bar.sql
    some_other_directory/
        baz.sql

Here's an example of what's in main.sql 这是main.sql中的一个例子


/* main.sql has the following contents: */
BEGIN TRANSACTION
\i some_directory/bar.sql
\i some_directory/foo.sql
\i some_other_directory/baz.sql
COMMIT;

As you can see, main.sql defines a specific order of operations and a transaction for the database updates. 如您所见,main.sql定义了特定的操作顺序和数据库更新的事务。

I've also got a python / twisted service monitoring SVN for changes in this db code, and I'd like to automatically deploy this code upon discovery of new stuff from the svn repository. 我还有一个python / twisted服务监视SVN以更改此db代码,我想在从svn存储库中发现新内容时自动部署此代码。

Can someone recommend a good pattern to use here? 有人可以推荐一个好的模式在这里使用吗?

Should I be parsing each file? 我应该解析每个文件吗? Should I be shelling out to psql? 我应该炮轰psql吗? ... ...

What you're doing is actually a decent approach if you control all the servers and they're all postgresql servers. 如果您控制所有服务器并且它们都是postgresql服务器,那么您所做的实际上是一种不错的方法。

A more general approach is to have a directory of "migrations" which are generally classes with an apply() and undo() that actually do the work in your database, and often come with abstractions like .create_table() that generate the DDL instructions specific to whatever RDBMS you're using. 更通用的方法是创建一个“迁移”目录,这些目录通常是带有apply()和undo()的类,它们实际上在数据库中完成工作,并且通常带有生成DDL指令的.create_table()等抽象。特定于您正在使用的任何RDBMS。

Generally, you have some naming convention that ensures the migrations run in the order they were created. 通常,您有一些命名约定,可确保迁移按创建顺序运行。

There's a migration library for python called South, though it appears to be geared specifically toward django development. 有一个名为South的python迁移库,虽然它似乎专门针对django开发。 http://south.aeracode.org/docs/about.html http://south.aeracode.org/docs/about.html

We just integrated sqlalchemy-migrate which has some pretty Rails-like conventions but with the power of SQLAlchemy. 我们刚刚集成了sqlalchemy-migrate ,它具有一些非常漂亮的Rails惯例,但具有SQLAlchemy的强大功能。 It's shaping up to be a really awesome product, but it does have some downfalls. 它正在成为一个非常棒的产品,但它确实有一些垮台。 It's pretty seamless to integrate though. 虽然整合起来非常无缝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM