src/CRUD.SqliteAdapter.js
/**
     * Verify that all tables for registered entities exist.
     * This starts a promise chain that executes the following steps:
     * - Fetch a list of all tables and indexes
     * - Iterate all known entities and:
     * - Execute their createStatements if they haven't been created.
     * - Execute any migrations in sequence if the localStorage value database.version.<table> is smaller than the highest migration number
     * - Compare the list of indexes and create the ones that don't exist
     * - Insert fixtures if the table has been freshly created
     * @return {Promise} Promise that resolves when all initialization is done
     */
Verify that all tables for registered entities exist.
This starts a promise chain that executes the following steps:
- Fetch a list of all tables and indexes
- Iterate all known entities and:
- Execute their createStatements if they haven't been created.
- Execute any migrations in sequence if the localStorage value database.version. is smaller than the highest migration number
- Compare the list of indexes and create the ones that don't exist
- Insert fixtures if the table has been freshly created
@return {Promise} Promise that resolves when all initialization is done
Verify that all tables for registered entities exist . This starts a promise chain that executes the following steps : - Fetch a list of all tables and indexes - Iterate all known entities and : - Execute their createStatements if they haven ' t been created . - Execute any migrations in sequence if the localStorage value database . version . is smaller than the highest migration number - Compare the list of indexes and create the ones that don ' t exist - Insert fixtures if the table has been freshly created @return { Promise } Promise that resolves when all initialization is done
function verifyTables() {
        CRUD.log('verifying that tables exist');
        var tables = [],
            indexes = {};
        function parseSchemaInfo(resultset) {
            for (var i = 0; i < resultset.rs.rows.length; i++) {
                var row = resultset.rs.rows.item(i);
                if (row.name.indexOf('sqlite_autoindex') > -1 || row.name == '__WebKitDatabaseInfoTable__') continue;
                if (row.type == 'table') {
                    tables.push(row.tbl_name);
                } else if (row.type == 'index') {
                    if (!(row.tbl_name in indexes)) {
                        indexes[row.tbl_name] = [];
                    }
                    indexes[row.tbl_name].push(row.name);
                }
            }
            return;
        }
        function createTables() {
            return Promise.all(Object.keys(CRUD.EntityManager.entities).map(function(entityName) {
                var entity = CRUD.EntityManager.entities[entityName];
                if (tables.indexOf(entity.table) == -1) {
                    if (!entity.createStatement) {
                        throw "No create statement found for " + entity.getType() + ". Don't know how to create table.";
                    }
                    return db.execute(entity.createStatement).then(function() {
                        tables.push(entity.table);
                        localStorage.setItem('database.version.' + entity.table, ('migrations' in entity) ? Math.max.apply(Math, Object.keys(entity.migrations)) : 1);
                        CRUD.log(entity.table + " table created.");
                        return entity;
                    }, function(err) {
                        CRUD.log("Error creating " + entity.table, err, entity.createStatement);
                        throw "Error creating table: " + entity.table;
                    }).then(createFixtures).then(function() {
                        CRUD.log("Table created and fixtures inserted for ", entityName);
                        return;
                    });
                }
                return;
            }));
        }
        function runMigrations() {
            return Promise.all(Object.keys(CRUD.EntityManager.entities).map(function(entityName) {
                var entity = CRUD.EntityManager.entities[entityName];
                if (entity.migrations) {
                    var currentVersion = !localStorage.getItem('database.version.' + entity.table) ? 1 : parseInt(localStorage.getItem('database.version.' + entity.table), 10);
                    if (isNaN(currentVersion)) {
                        currentVersion = 1;
                    }
                    var highestVersion = Math.max.apply(Math, Object.keys(entity.migrations));
                    if (currentVersion == highestVersion) return;
                    return Promise.all(Object.keys(entity.migrations).map(function(version) {
                        if (parseInt(version) > currentVersion) {
                            return Promise.all(entity.migrations[version].map(function(migration) {
                                CRUD.log('Executing migration: ', migration);
                                return db.execute(migration).then(function(result) {
                                    CRUD.log("Migration success!", result);
                                    return true;
                                }, function(err) {
                                    throw "Migration " + version + " failed for entity " + entityName;
                                });
                            })).then(function() {
                                CRUD.log("All migrations executed for version ", version);
                                localStorage.setItem('database.version.' + entity.table, version);
                                return true;
                            });
                        }
                        return true;
                    }));
                }
            }));
        }
        function createIndexes() {
            return Promise.all(Object.keys(CRUD.EntityManager.entities).map(function(entityName) {
                var entity = CRUD.EntityManager.entities[entityName];
                if (('indexes' in entity)) {
                    return Promise.all(entity.indexes.map(function(index) {
                        var indexName = index.replace(/\W/g, '') + '_idx';
                        if (!(entity.table in indexes) || indexes[entity.table].indexOf(indexName) == -1) {
                            return db.execute("create index if not exists " + indexName + " on " + entity.table + " (" + index + ")").then(function(result) {
                                CRUD.log("index created: ", entity.table, index, indexName);
                                if (!(entity.table in indexes)) {
                                    indexes[entity.table] = [];
                                }
                                indexes[entity.table].push(indexName);
                                return;
                            });
                        }
                        return;
                    }));
                }
            }));
        }
        return db.execute("select type,name,tbl_name from sqlite_master")
            .then(parseSchemaInfo)
            .then(createTables)
            .then(runMigrations)
            .then(createIndexes).then(function(result) {
                CRUD.log("All migrations are done!");
                self.initializing = false;
            });
    }
function verifyTables ( ) { CRUD . log ( 'verifying that tables exist' ) ; var tables = [ ] , indexes = { } ; function parseSchemaInfo ( resultset ) { for ( var i = 0 ; i < resultset . rs . rows . length ; i ++ ) { var row = resultset . rs . rows . item ( i ) ; if ( row . name . indexOf ( 'sqlite_autoindex' ) > - 1 || row . name == '__WebKitDatabaseInfoTable__' ) continue ; if ( row . type == 'table' ) { tables . push ( row . tbl_name ) ; } else if ( row . type == 'index' ) { if ( ! ( row . tbl_name in indexes ) ) { indexes [ row . tbl_name ] = [ ] ; } indexes [ row . tbl_name ] . push ( row . name ) ; } } return ; } function createTables ( ) { return Promise . all ( Object . keys ( CRUD . EntityManager . entities ) . map ( function ( entityName ) { var entity = CRUD . EntityManager . entities [ entityName ] ; if ( tables . indexOf ( entity . table ) == - 1 ) { if ( ! entity . createStatement ) { throw "No create statement found for " + entity . getType ( ) + ". Don't know how to create table." ; } return db . execute ( entity . createStatement ) . then ( function ( ) { tables . push ( entity . table ) ; localStorage . setItem ( 'database.version.' + entity . table , ( 'migrations' in entity ) ? Math . max . apply ( Math , Object . keys ( entity . migrations ) ) : 1 ) ; CRUD . log ( entity . table + " table created." ) ; return entity ; } , function ( err ) { CRUD . log ( "Error creating " + entity . table , err , entity . createStatement ) ; throw "Error creating table: " + entity . table ; } ) . then ( createFixtures ) . then ( function ( ) { CRUD . log ( "Table created and fixtures inserted for " , entityName ) ; return ; } ) ; } return ; } ) ) ; } function runMigrations ( ) { return Promise . all ( Object . keys ( CRUD . EntityManager . entities ) . map ( function ( entityName ) { var entity = CRUD . EntityManager . entities [ entityName ] ; if ( entity . migrations ) { var currentVersion = ! localStorage . getItem ( 'database.version.' + entity . table ) ? 1 : parseInt ( localStorage . getItem ( 'database.version.' + entity . table ) , 10 ) ; if ( isNaN ( currentVersion ) ) { currentVersion = 1 ; } var highestVersion = Math . max . apply ( Math , Object . keys ( entity . migrations ) ) ; if ( currentVersion == highestVersion ) return ; return Promise . all ( Object . keys ( entity . migrations ) . map ( function ( version ) { if ( parseInt ( version ) > currentVersion ) { return Promise . all ( entity . migrations [ version ] . map ( function ( migration ) { CRUD . log ( 'Executing migration: ' , migration ) ; return db . execute ( migration ) . then ( function ( result ) { CRUD . log ( "Migration success!" , result ) ; return true ; } , function ( err ) { throw "Migration " + version + " failed for entity " + entityName ; } ) ; } ) ) . then ( function ( ) { CRUD . log ( "All migrations executed for version " , version ) ; localStorage . setItem ( 'database.version.' + entity . table , version ) ; return true ; } ) ; } return true ; } ) ) ; } } ) ) ; } function createIndexes ( ) { return Promise . all ( Object . keys ( CRUD . EntityManager . entities ) . map ( function ( entityName ) { var entity = CRUD . EntityManager . entities [ entityName ] ; if ( ( 'indexes' in entity ) ) { return Promise . all ( entity . indexes . map ( function ( index ) { var indexName = index . replace ( / \W / g , '' ) + '_idx' ; if ( ! ( entity . table in indexes ) || indexes [ entity . table ] . indexOf ( indexName ) == - 1 ) { return db . execute ( "create index if not exists " + indexName + " on " + entity . table + " (" + index + ")" ) . then ( function ( result ) { CRUD . log ( "index created: " , entity . table , index , indexName ) ; if ( ! ( entity . table in indexes ) ) { indexes [ entity . table ] = [ ] ; } indexes [ entity . table ] . push ( indexName ) ; return ; } ) ; } return ; } ) ) ; } } ) ) ; } return db . execute ( "select type,name,tbl_name from sqlite_master" ) . then ( parseSchemaInfo ) . then ( createTables ) . then ( runMigrations ) . then ( createIndexes ) . then ( function ( result ) { CRUD . log ( "All migrations are done!" ) ; self . initializing = false ; } ) ; }
Verify that all tables for registered entities exist.
Verify that all tables for registered entities exist .
/**
         * Pre-Parse database schema info for further processing. Finds tables and indexes.
         * @param  {resultSet} database description
         * @return {void} void
         */ /**
         * Iterate the list of registered entities and creates their tables if they don't exist.
         * Run the migrations when needed if the table version is out of sync
         * @return {void} void
         */ /**
         * Run migrations for a table if the version stored in localStorage is out of sync.
         * All tables created by CreateReadUpdateDelete.js are versioned this way.
         * For more info on migrations see the docs.
         * @return {void} void
         */ /**
         * Iterate the list of existing and non-existing indexes for each entity and create the ones that don't exist.
         * @return {void} void
         */ // create listed indexes if they don't already exist. // fetch schema info and perform setup sequence.