简体   繁体   English

存储SQLAlchemy查询的结果以合并到另一个会话中

[英]Storing the results of a SQLAlchemy query to merge into another session

I have a SQLAlchemy-based tool for selectively copying data between two different databases for testing purposes. 我有一个基于SQLAlchemy的工具,可以选择性地在两个不同的数据库之间复制数据以进行测试。 I use the merge() function to take model objects from one session and store them in another session. 我使用merge()函数从一个会话中获取模型对象,并将其存储在另一个会话中。 I'd like to be able to store the source objects in some intermediate form and then merge() them at some later point in time. 我希望能够以某种中间形式存储源对象,然后在以后的某个时间点将它们merge()

It seems like there are a few options to accomplish this: 似乎有一些选择可以实现此目的:

  • Exporting DELETE / INSERT SQL statements. 导出DELETE / INSERT SQL语句。 Seems pretty straightforward, I think I can get SQLAlchemy to give me the INSERT statements, and maybe even the DELETE s. 看起来很简单,我想我可以让SQLAlchemy给我INSERT语句,甚至给我DELETE
  • Exproting the data to a SQLite database file with the same (or similar) schema, that could then be read in as a source at a later point in time. 将数据扩展到具有相同(或相似)模式的SQLite数据库文件,然后可以在以后的时间点将其作为源读取。
  • Serializing the data in some manner and then reading them back into memory for the merge. 以某种方式序列化数据,然后将其读回内存以进行合并。 I don't know if SQLAlchemy has something like this built-in or not. 我不知道SQLAlchemy是否具有这种内置功能。 I'm not sure what the challenges would be in rolling this myself. 我不确定自己推出此书会遇到什么挑战。

Has anyone tackled this problem before? 有人解决过这个问题吗? If so, what was your solution? 如果是这样,您的解决方案是什么?

EDIT: I found a tool built on top of SQLAlchemy called dataset that provides the freeze functionality I'm looking for, but there seems to be no corresponding thaw functionality for restoring the data. 编辑:我发现建立在SQLAlchemy之上的名为数据集的工具,该工具提供了我正在寻找的冻结功能,但是似乎没有相应的解冻功能来恢复数据。

I haven't used it before, but the dogpile caching techniques described in the documentation might be what you want. 我以前没有使用过它,但是文档中描述的Dogpile缓存技术可能就是您想要的。 This allows you to query to and from a cache using the SQLAlchemy API: 这使您可以使用SQLAlchemy API在高速缓存中进行查询:

http://docs.sqlalchemy.org/en/rel_0_9/orm/examples.html#module-examples.dogpile_caching http://docs.sqlalchemy.org/en/rel_0_9/orm/examples.html#module-examples.dogpile_caching

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

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