如何更新涡轮齿轮应用生产数据库
我在生产中有一个 postgres 生产数据库(其中包含大量数据)。 现在我需要修改 tg-app 的模型以向数据库添加几个新表。
我该怎么做呢? 我正在使用 sqlAlchemy。
I am having a postgres production database in production (which contains a lot of Data). now I need to modify the model of the tg-app to add couple of new tables to the database.
How do i do this? I am using sqlAlchemy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这总是有效的,并且不需要太多思考——只需要耐心。
进行备份。
实际进行备份。 每个人都会跳过第 1 步,认为他们有备份,但他们永远找不到它或使用它。 不要相信任何无法恢复的备份。
创建新的数据库架构。
在新架构中从头开始定义新结构。 理想情况下,您将运行构建新架构的 DDL 脚本。 没有构建架构的脚本? 创建一个并将其置于版本控制之下。
使用 SA,您可以定义表,它可以为您构建架构。 这是理想的,因为您的架构处于 Python 的版本控制之下。
移动数据。
a. 对于未更改结构的表,使用简单的 INSERT/SELECT 语句将数据从旧模式移动到新模式。
b. 对于确实改变结构的表,开发 INSERT/SELECT 脚本将数据从旧的移动到新的。 通常,这可以是每个新表的单个 SQL 语句。 在某些情况下,它必须是具有两个打开连接的 Python 循环。
c. 对于新表,加载数据。
停止使用旧架构。 开始使用新架构。 找到每个使用旧架构的程序并修复配置。
没有应用程序列表? 做一个。 说真的——这很重要。
应用程序有硬编码的数据库配置吗? 当你在做的时候,也解决这个问题。 创建一个通用配置文件,或者使用一些通用环境变量或其他东西来(a)确保一致性和(b)集中“生产”的概念。
您可以在任何时候进行大手术时进行此类手术。 除了提取数据之外,它从不接触旧数据库。
This always works and requires little thinking -- only patience.
Make a backup.
Actually make a backup. Everyone skips step 1 thinking that they have a backup, but they can never find it or work with it. Don't trust any backup that you can't recover from.
Create a new database schema.
Define your new structure from the ground up in the new schema. Ideally, you'll run a DDL script that builds the new schema. Don't have a script to build the schema? Create one and put it under version control.
With SA, you can define your tables and it can build your schema for you. This is ideal, since you have your schema under version control in Python.
Move data.
a. For tables which did not change structure, move data from old schema to new schema using simple INSERT/SELECT statements.
b. For tables which did change structure, develop INSERT/SELECT scripts to move the data from old to new. Often, this can be a single SQL statement per new table. In some cases, it has to be a Python loop with two open connections.
c. For new tables, load the data.
Stop using the old schema. Start using the new schema. Find every program that used the old schema and fix the configuration.
Don't have a list of applications? Make one. Seriously -- it's important.
Applications have hard-coded DB configurations? Fix that, too, while you're at it. Either create a common config file, or use some common environment variable or something to (a) assure consistency and (b) centralize the notion of "production".
You can do this kind of procedure any time you do major surgery. It never touches the old database except to extract the data.
最简单的方法是简单地编写一些sql更新脚本并使用它们来更新数据库。 显然,这是一种相当低级(可以说是)的方法。
如果您认为自己会经常这样做并且想坚持使用 Python,您可能需要查看 sqlalchemy-迁移。 最近的 Python 杂志上有一篇关于它的文章。
The simplest approach is to simply write some sql update scripts and use those to update the database. Obviously that's a fairly low-level (as it were) approach.
If you think you will be doing this a lot and want to stick in Python you might want to look at sqlalchemy-migrate. There was an article about it in the recent Python Magazine.
我总体上同意 John 的观点。 一次性选择和插入对于大型数据库来说并不实用,并且设置复制或多轮差异选择/插入可能会更困难且更容易出错。
就个人而言,我在 TurboGears 下使用 SQLAlchemy 作为 ORM。 为了进行模式迁移,我运行:
要查看实时模式和开发模式之间的差异,然后手动编写(和版本控制)DDL 脚本以进行所需的更改。
对于那些独立使用 SQLAlchemy(即不在 TurboGears 下)的人来说,
sql status
功能非常简单,可以在 TG 源中找到:http://svn.turbogears.org/branches/1.1/turbogears/command/sacommand.py (有旧版本Python / SA 也在 1.0 分支中发布)。I'd agree in general with John. One-pass SELECTing and INSERTing would not be practical for a large database, and setting up replication or multi-pass differential SELECT / INSERTs would probably be harder and more error-prone.
Personally, I use SQLAlchemy as an ORM under TurboGears. To do schema migrations I run:
To see the difference between the live and development schemas, then manually write (and version control) DDL scripts to make the required changes.
For those using SQLAlchemy standalone (i.e. not under TurboGears), the
sql status
functionality is pretty simple and can be found here in the TG source: http://svn.turbogears.org/branches/1.1/turbogears/command/sacommand.py (there's versions for older Python / SA releases in the 1.0 branch, too).如果您只是添加表,而不修改任何其中包含现有数据的表,则只需将新的 sqlAlchemy 表定义添加到 model.py 中,然后运行:
这不会覆盖任何现有表。
对于架构迁移,您可以查看 http://code.google.com/p /sqlalchemy-migrate/ 虽然我自己还没有使用过它。
在迁移活动之前始终对生产数据库进行备份。
If you are just adding tables, and not modifying any of the tables which have the existing data in it, you can simply add the new sqlAlchemy table definitions to model.py, and run:
This will not overwrite any of your existing tables.
For schema migration, you might take a look at http://code.google.com/p/sqlalchemy-migrate/ although I haven't used it yet myself.
Always take a backup of the production database before migration activity.