使用 SQLObject 将数据从一个 SQLite 数据库迁移到多个 SQLite 数据库
到目前为止,我们的应用程序一直使用一个 SQLite 数据库,并以 SQLObject 作为 ORM。显然,在某些时候我们知道我们必须面对 SQLite 并发问题,所以我们做到了。
我们最终将当前数据库拆分为多个数据库。这意味着每个表模式保持不变,但我们将不同的表分布到多个数据库中,将表紧密耦合在一起。
现在,这在我们应用程序的新版本的全新安装中效果很好,但是将应用程序的以前版本升级到这个新版本需要特殊的数据迁移,然后我们的应用程序才能开始工作。在这种情况下,数据库迁移很简单,将表从这个单一数据库移动到适当的不同数据库中。
举例来说,假设这是旧结构:
single_db.db --- 单个数据库
* A -- Table A
* B -- Table B
* C -- Table C
* D -- Table D
* E -- Table E
* F -- Table F
新结构:
db1.db --- 数据库 1
- A -- Table A
- B -- Table B
- C -- Table C
- D -- Table D
db2.db --- 数据库 2
- E -- Table E
db3.db --- 数据库 3
- F -- Table F
升级时将会发生的情况是,我们的应用程序将使用上述 3 个数据库以及其中的空表创建新结构。此外,包含所有表和实际数据的旧数据库 single_db.db 也将在那里。现在,在我们的应用程序开始工作之前,它应该移动表,或者我应该说将数据从旧数据库的表复制到相应新数据库中的相应表。
我需要为此数据库迁移编写代码。我知道我可以使用较旧的数据库连接查询表,并使用较新的数据库连接将返回的行插入到相应的表中。我应该在这里提到的一个警告是,其中一些表可能包含大量行。也就是说,2/3 表中的行数最多可达 2 - 250 万行。
所以想问我是否可以使用任何其他 SLQObject 技巧,因为我在 SQLite 之上使用 SQLObject,并且以前有人这样做过吗?
感谢您的帮助。
Till now our application has been using one SQLite database with SQLObject as the ORM. Obviously at some point we knew we had to face the SQLite concurrency problem and so we did.
We ended up splitting the current database into multiple databases. Meaning each table schema remained the same but we distributed different tables into multiple databases keeping tightly coupled tables together.
Now this works very well in a clean install of the new version of our application but upgrade to the previous versions of our application to this new version needs a special data migration before our application can start working. In this case the database migration is simple moving the tables from this single database into appropriate different databases.
To exemplify, consider this is the older structure:
single_db.db --- A single db
* A -- Table A
* B -- Table B
* C -- Table C
* D -- Table D
* E -- Table E
* F -- Table F
The new structure:
db1.db --- Database 1
- A -- Table A
- B -- Table B
- C -- Table C
- D -- Table D
db2.db --- Database 2
- E -- Table E
db3.db --- Database 3
- F -- Table F
When the upgrade will happen, our application will create the new structure with the above 3 databases and with empty tables in them. Also the older database single_db.db with all the tables and actual data will be there. Now before our application can begin working, it should move the tables or I should say copy the data from a table from the older database to the corresponding table in the corresponding new database.
I will need to write the code for this database migration. I know I can query a table using the older database connection and insert the returned rows to the corresponding table using the newer database connection. One caveat I should mention here is some of these tables can contain large number of rows. That is rows can be till 2 - 2.5 million in 2/3 tables.
So want to ask if I can use any other SLQObject tricks since I am using SQLObject on top of SQLite and also has anyone done this before?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到你现在可能已经解决了这个问题,但是对于任何人谷歌搜索我都必须做几乎与OP完全相同的事情,这是我使用的代码的核心部分(它是根据我发现的东西修改的,但我找不到它再次感谢原作者,抱歉!)
如果您传递原始数据库的 sqlite 连接以及原始数据库中表的名称,此生成器将返回命令,您可以传递这些命令以在新数据库的 sqlite 对象上执行。
当我这样做时,我还首先对所有表进行了行计数,并在执行 INSERT 行时增加了计数器,以便可以显示迁移的进度。
I realise you probably solved this by now but for anyone googling I had to do almost exactly the same as the OP, this was the core part of the code that I used (it's modified from something I found, but I can't find it again to credit the original author, apologies!)
If you pass the sqlite connection for the original db and the name of the table in the original db this generator will give back commands that you can pass to execute on the sqlite object for the new db.
When I did this I also did a count of rows first on all the tables and incremented a counter as I executed
INSERT
lines so I could show progress on the migration.