如何防止过时的代码在较新的数据库上运行? (在姜戈中)
我正在开发一个包含多个共享公共数据库的服务的项目。我想最大限度地减少操作错误导致数据损坏的风险,我想防止的情况之一是针对新数据库运行过时的代码。
也就是说,我想以某种方式在数据库上标记一个版本,然后让所有早于该版本的服务在启动时出错。这样,一旦数据库迁移,旧代码就无法针对新数据库启动。其他人是否有任何现成的方法来解决这个问题?我正在使用 Django,但我也对其他框架使用的解决方案感兴趣。
I'm working on a project with multiple services that share a common database. I'd like to minimize the risk that operational errors cause data corruption and one of the cases I'd like to guard against is running stale code against a new DB.
That is, I'd like to somehow stamp the DB with a version and then have all services older than that version error out on startup. That way, once the DB is migrated, old code can't be started against the newer DB. Are there any off-the-shelf ways other folks have been tackling this? I'm using Django, but I'm also interested in solutions used by other frameworks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以有一个简单的表格,存储应用程序与最低应用程序版本号。
部分升级数据库脚本将填充此表。当应用程序启动时,它会根据表中的最低版本检查其版本,如果版本太低,则会爆炸。
我不知道任何现有的包可以做到这一点,但它似乎不需要太多代码来实现。我喜欢这个主意。
You could have a simple table the stores application vs minimum application version number.
Part of your upgrade DB scripts would populate this table. When the application starts up, it would check its version against the minimum version in the table and explode if its version is too low.
I am not aware of any existing package that does this, but it doesn't seem like much code to achieve. I like the idea.
如果您使用的是 South,您可以编写一个中间件来检查所有应用程序中的所有迁移是否已应用,如果没有,您可以让它使用硬编码的维护警告来响应每个请求。
当然,您只想运行一次。
If you are using south you could write a middleware that checks to see if all the migrations in all the applications have been applied, if they haven't you could have it respond to every request with a hard coded maintenance warning.
You would, of course, want to run this only once.