我有一个可能特殊的数据迁移问题:
结果应该是根据新模式构建的新数据库,其中包含尽可能多的旧数据库内容。
鉴于SQLite3 ALTER语句和我们的工作流程都有限制,可以安全地假设:
注意:如果新模式与旧模式不兼容(即:任何上述假设不成立),则接受失败。
我试过这个脚本(旧数据库是data.sql3
,新架构是data.schema
):
mkdir tmp
cd tmp
#compute old DB schema
sqlite3 ../data.sql3 .schema >old_s
#purge new schema for any initialization...
grep -v ^INSERT ../data.schema >data.schema
#... create a dew, empty DB...
sqlite3 new.sql3 <data.schema
#... and compute a standard schema
#(this is done to avoid typing differences)
sqlite3 new.sql3 .schema >new_s
#iff the schemas are different
if ! diff -q old_s new_s
then
#save old DB
mv ../data.sql3 .
#dump contents
sqlite3 data.sql3 .dump >old_d
#expunge all statements needed to recreate DB/Tables
#new_d contains only INSERT statements
grep -v -f old_s old_d >new_d
#add old DB content to new DB
sqlite3 new.sql3 <new_d
#move new DB in place
mv new.sql3 ../data.sql3
fi
cd ..
这可以检测更改,但无法重新填充新数据库,因为.dump
不包含列名,因此插入失败(缺少值)。
我正在寻找的是某种方法迫使sqlite3 DB .dump
输出包含所有字段名称的INSERT
语句(通常它依赖于位置),或者,这是不可能的,某种方式告诉sqlite3 DB <new_d
将任何未定义的字段视为null
或默认值(没有失败)。
任何其他方式来实现相同的结果(不需要知道什么,确切地,已被修改)同样受欢迎。
为了能够将具有较少列的转储插入/导入到表中,您可以为新的附加列提供默认值,或者只是将它们设置为NULL
。 CREATE TABLE
和ALTER TABLE
的约束条款是相同的:
http://www.sqlite.org/syntax/column-constraint.html
-- newColumn is set to a default value if not provided with INSERT
alter table myTable
add column newColumn INTEGER NOT NULL default 0;
-- newColumn may be NULL, which is the default if not provided with INSERT
alter table myTable
add column newColumn INTEGER;
-- It is also valid to combine NULL and DEFAULT constraints
alter table myTable
add column newColumn INTEGER default 0;
请注意,为了使INSERT
语句与新列一起使用,它必须提供列名。