Reputation: 12549
I'm about to release an app update of an iPhone app that I've written in Titanium Alloy. I've added a new column to the database, so I've written a migration for it. The upwards migration is simple enough, just altering the table to add a new column. However, the downwards migration has be a little worried as it involved creating a temporary database, storing the data I need, and then dropping the existing data base and creating a new one with the stored data, in order to keep remove column.
How do I test that this code is correct and will work?
Here are my migrations:
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;');
};
Upvotes: 0
Views: 448
Reputation: 353
You should be fine as long as your first migration file (the one you created when you created the model) matches up with the downward migration here.
Like this:
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();
};
This migration file has to have a timestamp that is less than the one you listed in the original question.
Upvotes: 1