Migrate data from scriptDb to Datastore

This shows how to easily copy data between back ends as described in Database abstraction with google apps script 

The library reference is Mj61W-201_t_zC9fJg1IzYiz3TLx7pV4j  

Example

If you are using Database abstraction with google apps script, changing back ends is easy. Let's say you are using scriptDB, but want to migrate your data to Datastore it's pretty straightforward. Your applications won't need to change much since your queries will all have been abstracted. Here's how to copy over.  We're using DataStore so we need an Oauth2 pattern and your datastore setup as described in Datastore driver

function conversionFromScriptDB () {
  return doGetPattern({} , constructConsentScreen, scriptDBCopyToDataStore,'googleDatastore');
}

function scriptDBCopyToDataStore (accessToken) {

  //get some data from - no Siloid in original data
  var handler = new cDataHandler.DataHandler (
      '',                        
      cDataHandler.dhConstants.DB.SCRIPTDB,      
      undefined,
      'myddb',
      ScriptDb.getMyDb(), 
      undefined,                       
      true,             
      'bruce',
      undefined,
      true);
  assert(handler.isHappy(), 'unable to get scriptdb handler','handler');


   // get a datastore handler
   var dsHandler = new cDataHandler.DataHandler (
     'crap',                              // Kind
     cDataHandler.dhConstants.DB.DATASTORE,    // Datastore
     undefined, 
     'xliberationdatastore',                   // project id
     undefined, 
     undefined,
     true,                                     // analytics opt out
     'bruce',                                  // analytics debugging tracking 
     accessToken,                              // the access token
     true);                                    // disable caching for testing  
      
  assert(dsHandler.isHappy(), 'unable to get scriptdb handler','handler');

  // do the copy
  var data = handler.query();
  assert (data.handleCode >=0, data, 'getting data from script');
   
  // delete everything in current datastore
  var result = dsHandler.remove();
  assert (result.handleCode >=0, result, 'deleting data from datastore');
 
  // copy it to datastore  
  var result = dsHandler.save(data.data);
  assert (result.handleCode >=0, result, 'writing data to datastore');

  // read it back and make sure it all made it
  var result = dsHandler.query();
  assert (result.handleCode >=0, result, 'querying data from datastore');

}



See more like this in Database abstraction with google apps script and Datastore driver



For help and more information join our forum,follow the blog or follow me on twitter .



Comments