我即将发布我用Titanium Alloy编写的iPhone应用程序的应用程序更新。我已经在数据库中添加了一个新列,因此我已经为它编写了一个迁移。向上迁移很简单,只需更改表以添加新列。但是,向下迁移有点担心,因为它涉及创建临时数据库,存储我需要的数据,然后删除现有数据库并使用存储的数据创建新数据库,以便保留删除列。
如何测试此代码是否正确且有效?
以下是我的迁移:
migration.up = function(migrator) {
migrator.db.execute('ALTER TABLE ' + migrator.table + ' ADD COLUMN is_sample BOOLEAN;');
};
migration.down = function(migrator) {
var db = migrator.db;
var table = migrator.table;
db.execute('CREATE TEMPORARY TABLE beers_backup(alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite);');
db.execute('INSERT INTO beers_backup SELECT alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite FROM ' + table + ';');
migrator.dropTable();
migrator.createTable({
columns: {
"name": "text",
"brewery": "text",
"rating": "integer",
"percent": "integer",
"establishment": "text",
"location": "text",
"notes": "text",
"date": "text",
"date_string": "text",
"beer_image": "text",
"latitude": "integer",
"longitude": "integer",
"favourite": "boolean"
},
});
db.execute('INSERT INTO ' + table + ' SELECT alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite FROM beers_backup;');
db.execute('DROP TABLE beers_backup;');
};
答案 0 :(得分:1)
只要您的第一个迁移文件(创建模型时创建的文件)与此处的向下迁移匹配,您就可以了。
像这样:
migration.up = function(migrator){
migrator.createTable({
columns: {
"name": "text",
"brewery": "text",
"rating": "integer",
"percent": "integer",
"establishment": "text",
"location": "text",
"notes": "text",
"date": "text",
"date_string": "text",
"beer_image": "text",
"latitude": "integer",
"longitude": "integer",
"favourite": "boolean"
}
});
};
migration.down = function(migrator) {
migrator.droptTable();
};
此迁移文件的时间戳必须小于原始问题中列出的时间戳。