def sync_table(model):
    """
    Inspects the model and creates / updates the corresponding table and columns.

    Note that the attributes removed from the model are not deleted on the database.
    They become effectively ignored by (will not show up on) the model.
    """

    if not issubclass(model, Model):
        raise CQLEngineException("Models must be derived from base Model.")

    if model.__abstract__:
        raise CQLEngineException("cannot create table from abstract model")


    #construct query string
    cf_name = model.column_family_name()
    raw_cf_name = model.column_family_name(include_keyspace=False)

    ks_name = model._get_keyspace()

    cluster = get_cluster()

    keyspace = cluster.metadata.keyspaces[ks_name]
    tables = keyspace.tables

    #check for an existing column family
    if raw_cf_name not in tables:
        qs = get_create_table(model)

        try:
            execute(qs)
        except CQLEngineException as ex:
            # 1.2 doesn't return cf names, so we have to examine the exception
            # and ignore if it says the column family already exists
            if "Cannot add already existing column family" not in unicode(ex):
                raise
    else:
        # see if we're missing any columns
        fields = get_fields(model)
        field_names = [x.name for x in fields]
        for name, col in model._columns.items():
            if col.primary_key or col.partition_key: continue # we can't mess with the PK
            if col.db_field_name in field_names: continue # skip columns already defined

            # add missing column using the column def
            query = "ALTER TABLE {} add {}".format(cf_name, col.get_column_def())
            logger.debug(query)
            execute(query)

        update_compaction(model)


    table = cluster.metadata.keyspaces[ks_name].tables[raw_cf_name]

    indexes = [c for n,c in model._columns.items() if c.index]

    for column in indexes:
        if table.columns[column.db_field_name].index:
            continue

        qs = ['CREATE INDEX index_{}_{}'.format(raw_cf_name, column.db_field_name)]
        qs += ['ON {}'.format(cf_name)]
        qs += ['("{}")'.format(column.db_field_name)]
        qs = ' '.join(qs)
        execute(qs)
def sync_table ( model ) : if not issubclass ( model , Model ) : raise CQLEngineException ( "Models must be derived from base Model." ) if model . __abstract__ : raise CQLEngineException ( "cannot create table from abstract model" ) cf_name = model . column_family_name ( ) raw_cf_name = model . column_family_name ( include_keyspace = False ) ks_name = model . _get_keyspace ( ) cluster = get_cluster ( ) keyspace = cluster . metadata . keyspaces [ ks_name ] tables = keyspace . tables if raw_cf_name not in tables : qs = get_create_table ( model ) try : execute ( qs ) except CQLEngineException as ex : if "Cannot add already existing column family" not in unicode ( ex ) : raise else : fields = get_fields ( model ) field_names = [ x . name for x in fields ] for name , col in model . _columns . items ( ) : if col . primary_key or col . partition_key : continue if col . db_field_name in field_names : continue query = "ALTER TABLE {} add {}" . format ( cf_name , col . get_column_def ( ) ) logger . debug ( query ) execute ( query ) update_compaction ( model ) table = cluster . metadata . keyspaces [ ks_name ] . tables [ raw_cf_name ] indexes = [ c for n , c in model . _columns . items ( ) if c . index ] for column in indexes : if table . columns [ column . db_field_name ] . index : continue qs = [ 'CREATE INDEX index_{}_{}' . format ( raw_cf_name , column . db_field_name ) ] qs += [ 'ON {}' . format ( cf_name ) ] qs += [ '("{}")' . format ( column . db_field_name ) ] qs = ' ' . join ( qs ) execute ( qs )
Inspects the model and creates / updates the corresponding table and columns.

    Note that the attributes removed from the model are not deleted on the database.
    They become effectively ignored by (will not show up on) the model.