Titanium JIRA Archive
Alloy (ALOY)

[ALOY-468] add insertRow() and deleteRow() function in sql migrations

GitHub Issuen/a
TypeNew Feature
Resolution Date2013-01-17T20:08:18.000+0000
Affected Version/sn/a
Fix Version/sAlloy 0.3.5, 2013 Sprint 02
ReporterTony Lukasavage



The sql adapter has the createTable() and dropTable() functions for, well, creating and deleting tables in migrations. To assist in adding and deleting rows, we should also have insertRow() and deleteRow() function. insertRow() will take a single parameter which is an object that defines the name and values of columns to add. This is the same exact form as the *columns* object in the model definition. deleteRow() will also take a single object. This object will include a list of columns (keys) and corresponding values (values) that will be used in the where clause of the delete that alloy performs. These are simple, shorthand functions. More complex inserts and deletes can be done using the Titanium.Database API.

example usage

var rows = [
    { firstname: 'tony', lastname: 'lukasavage' },
    { firstname: 'chris', lastname: 'barber' },
    { firstname: 'bryan', lastname: 'hughes' }

migration.up = function(migrationObj) {
    for (var i = 0; i < rows.length; i++) {
        // will insert the given rows

migration.down = function(migrationObj) {
    for (var i = 0; i < rows.length; i++) {
        // Will delete from the model's table using the given object
        // to construct the delete query. For example, assuming the
        // table name is 'myTable', the query generated for the 
        // first object would be:
        //   DELETE FROM myTable WHERE firstname ='tony' AND lastname = 'lukasavage'; 


* If no columns are given to deleteRow(), all rows will be deleted * If an up() and down() migration plan to add and remove rows respectively, developers may want to insert their rows' unique ids explicitly, even if they are autoincrement. This will ensure that when the deleteRow() is called in the down(), it will delete the correct, unique row based on the id. This is really only relevant in the case where there are legit reasons for having duplicates in your table.


  1. Tony Lukasavage 2013-01-17

    used in test app: https://github.com/appcelerator/alloy/tree/master/test/apps/models/sql_queries

JSON Source