如何使用数据迁移更新sqlite3架构

问题描述 投票:0回答:1

我有一个可能特殊的数据迁移问题:

  1. 我有一个现有的和填充的SQLite3数据库。
  2. 我确实收到一个(希望兼容)数据库的新架构。

结果应该是根据新模式构建的新数据库,其中包含尽可能多的旧数据库内容。

鉴于SQLite3 ALTER语句和我们的工作流程都有限制,可以安全地假设:

  • 正常情况下将新列添加到表的末尾。
  • 添加的列(字段)将具有默认值或可以保留为NULL。
  • 很少会添加一些表格。
  • 很少有一些表或列可能被丢弃。
  • 不会发生表/列重命名。
  • 没有列重组会发生。

注意:如果新模式与旧模式不兼容(即:任何上述假设不成立),则接受失败。

我试过这个脚本(旧数据库是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或默认值(没有失败)。

任何其他方式来实现相同的结果(不需要知道什么,确切地,已被修改)同样受欢迎。

database sqlite database-migration data-migration
1个回答
0
投票

为了能够将具有较少列的转储插入/导入到表中,您可以为新的附加列提供默认值,或者只是将它们设置为NULLCREATE TABLEALTER 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语句与新列一起使用,它必须提供列名。

© www.soinside.com 2019 - 2024. All rights reserved.