[ALOY-900] Create a new table with Alloy and pre-built db

GitHub Issuen/a
Resolution Date2014-04-16T14:49:38.000+0000
Affected Version/sn/a
Fix Version/sAlloy 1.4.0, 2014 Sprint 08
Labelsalloy, notable, qe-testadded
ReporterDavid Benko
AssigneeTim Poulsen


Creating a new table with a pre-build db fails because Alloy executes "pragma table_info" to get columns information:
12-11 19:04:30.728: E/TiExceptionHandler(13828): (main) [366,366] ----- Titanium Javascript Runtime Error -----
12-11 19:04:30.728: E/TiExceptionHandler(13828): (main) [0,366] - In alloy/sync/sql.js:11,69
12-11 19:04:30.733: E/TiExceptionHandler(13828): (main) [1,367] - Message: Uncaught TypeError: Cannot call method 'isValidRow' of null
12-11 19:04:30.733: E/TiExceptionHandler(13828): (main) [0,367] - Source: base.install(b,d),d=b.execute('pragma table_info("'+e+'");'),f={};d.isValidRow
12-11 19:04:30.733: E/V8Exception(13828): Exception occurred at alloy/sync/sql.js:11: Uncaught TypeError: Cannot call method 'isValidRow' of null
Detailed problem: http://developer.appcelerator.com/question/160374/alloy--migrations---create-new-table-with-migrations


  1. Ritu Agrawal 2013-12-15 Moving this issue to Alloy project based on Tony's recommendation on the Q&A thread.
  2. Ritu Agrawal 2013-12-15 FYI - JIRA changed the reporter to "me" when I moved this ticket from TC to ALLOY and I don't have permission to change it back. This issue was originally reported by [~davidbenko].
  3. Nils Sdun 2014-02-22 Possible Solution: Place following code in sql.js in line 271. It will create a Table from a Model if it doesn´t exist. If no columns given in the model, the ALLOY_ID_DEFAULT will be taken.
       if(rs == null) {
           var idAttribute = ALLOY_ID_DEFAULT;
           if(config.adapter.idAttribute) {
               idAttribute = config.adapter.idAttribute; 
           var columns = [];
           var found = false;
           for (var k in config.columns) { 
               k === idAttribute && (found = true); 
               columns.push(k + " " + config.columns[k]); 
           found || columns.push(idAttribute + " TEXT UNIQUE");
           db.execute( 'CREATE TABLE ' + table + ' (' + columns.join(", ") + ');' );
           var rs = db.execute('pragma table_info("' + table + '");'); 
  4. Tim Poulsen 2014-03-14 PR https://github.com/appcelerator/alloy/pull/347 Test app included: test/apps/testing/ALOY-900 This change adds support for a few scenarios: * Existing model, which points to an external database, with a migration file used to define a new table in that database * Existing external database, add a new table by simply defining a new model file and instantiating it in a controller, no migration needed * Alloy-created database, add a new table by simply defining a new model file and instantiating it in a controller, no migration needed Functional test: 1. Run the test app in the iOS simulator 2. Open ~/Library/Application Support/iPhone Simulator/VERSION/GUID/Library/Private Documents folder to find the generated sqlite database file 3. Use a SQLite tool, such as the SQLite Manager plugin for Firefox to open that file. It will contain a table named weather with three columns, city, id, and bogusField as defined in the migration 4. Delete the app from the simulator 5. Rename the migrations folder or delete it so that it's not used 6. Repeat steps 1-3, this time the database's table structure will match the table definition in the models/weather.js file 7. Delete the app from the simulator 8. Edit the models/weather.js file to remove the db_file property from the adapter config. 9. Repeat steps 1-3 to create the standard _alloy_.sql file. It will contain the tables as defined in the model
  5. Tim Poulsen 2014-03-14 Sorry, not supposed to Resolve till the PR is merged.
  6. Tim Poulsen 2014-04-16 PR merged
  7. Lokesh Choudhary 2014-05-15 Ran through the functional test steps mentioned by [~skypanther]. All steps worked as expected. Closing. Environment: Appc Studio : Ti SDK : 3.3.0.v20140514163013 Mac OSX : 10.8.5 Alloy : 1.4.0-alpha CLI - 3.3.0-dev iPhone simulator : 7.1 64-bit

JSON Source