Example #1
0
def get_or_create_tables(session, prefix=None, create=True, **tables):
    """
    Load or create canonical ORM KB Core table classes.

    Parameters
    ----------
    session : sqlalchemy.orm.Session
    prefix : str
        Prefix for canonical table names, e.g. 'myaccount.' or 'TA_' (no owner/schema).
        Canonical table names are used if no prefix is provided.
    create : bool
        If True, create tables that don't yet exist.

    Canonical table keyword/tablename string pairs, such as site='other.site',
    can override prefix.  For example, you may want to use a different Lastid
    table, so that ids don't start from 1.

    Returns
    -------
    tables : dict
        A mapping between canonical table names and SQLA declarative classes
        with the correct __tablename__.
        e.g. {'origin': Origin, ...}

    """
    # The Plan:
    # 1. For each core table, build or get the table name
    # 2. If it's a vanilla table name, just use a pre-packaged table class
    # 3. If not, try to autoload it.
    # 4. If it doesn't exist, make it from a prototype and create it in the database.

    # TODO: check options for which tables to produce.

    if not prefix:
        prefix = ''

    tabledict = {}
    for coretable in CORETABLES.values():
        # build the table name
        fulltablename = tables.get(coretable.name, prefix + coretable.name)

        # put table classes into the tables dictionary
        if fulltablename == coretable.name:
            # it's a vanilla table name. just use a pre-packaged table class instead of making one.
            tabledict[coretable.name] = coretable.table
        else:
            tabledict[coretable.name] = ps.make_table(fulltablename,
                                                      coretable.prototype)

        tabledict[coretable.name].__table__.create(session.bind,
                                                   checkfirst=True)

    session.commit()

    return tabledict
Example #2
0
def get_or_create_tables(session, prefix=None, create=True, **tables):
    """
    Load or create canonical ORM KB Core table classes.

    Parameters
    ----------
    session : sqlalchemy.orm.Session
    prefix : str
        Prefix for canonical table names, e.g. 'myaccount.' or 'TA_' (no owner/schema).
        Canonical table names are used if no prefix is provided.
    create : bool
        If True, create tables that don't yet exist.

    Canonical table keyword/tablename string pairs, such as site='other.site',
    can override prefix.  For example, you may want to use a different Lastid
    table, so that ids don't start from 1.

    Returns
    -------
    tables : dict
        A mapping between canonical table names and SQLA declarative classes
        with the correct __tablename__.
        e.g. {'origin': Origin, ...}

    """
    # The Plan:
    # 1. For each core table, build or get the table name
    # 2. If it's a vanilla table name, just use a pre-packaged table class
    # 3. If not, try to autoload it.
    # 4. If it doesn't exist, make it from a prototype and create it in the database.

    # TODO: check options for which tables to produce.

    if not prefix:
        prefix = ''

    tabledict = {}
    for coretable in CORETABLES.values():
        # build the table name
        fulltablename = tables.get(coretable.name, prefix + coretable.name)

        # put table classes into the tables dictionary
        if fulltablename == coretable.name:
            # it's a vanilla table name. just use a pre-packaged table class instead of making one.
            tabledict[coretable.name] = coretable.table
        else:
            tabledict[coretable.name] = ps.make_table(fulltablename, coretable.prototype)

        tabledict[coretable.name].__table__.create(session.bind, checkfirst=True)

    session.commit()

    return tabledict
Example #3
0
def get_or_create_tables(options, session, create=True):
    """
    Load or create canonical ORM KB Core table classes.

    Parameters
    ----------
    options : argparse.ArgumentParser
    session : sqlalchemy.orm.Session

    Returns
    -------
    tables : dict
        Mapping between canonical table names and SQLA ORM classes.
        e.g. {'origin': MyOrigin, ...}

    """
    # The Plan:
    # 1. For each core table, build or get the table name
    # 2. If it's a vanilla table name, just use a pre-packaged table class
    # 3. If not, try to autoload it.
    # 4. If it doesn't exist, make it from a prototype and create it in the database.

    # TODO: check options for which tables to produce.

    dbout = options.prefix

    tables = {}
    for coretable in CORETABLES:
        # build the table name
        if getattr(options, coretable.name, None):
            fulltablename = getattr(options, coretable.name)
        else:
            fulltablename = dbout + coretable.name

        # fulltablename is either an arbitrary string or dbout + core name, but not None

        # put table classes into the tables dictionary
        if fulltablename == coretable.name:
            # it's a vanilla table name. just use a pre-packaged table class instead of making one.
            tables[coretable.name] = coretable.table
        else:
            tables[coretable.name] = ps.make_table(fulltablename,
                                                   coretable.prototype)

        tables[coretable.name].__table__.create(session.bind, checkfirst=True)

    session.commit()

    return tables
Example #4
0
def get_or_create_tables(options, session, create=True):
    """
    Load or create canonical ORM KB Core table classes.

    Parameters
    ----------
    options : argparse.ArgumentParser
    session : sqlalchemy.orm.Session

    Returns
    -------
    tables : dict
        Mapping between canonical table names and SQLA ORM classes.
        e.g. {'origin': MyOrigin, ...}

    """
    # The Plan:
    # 1. For each core table, build or get the table name
    # 2. If it's a vanilla table name, just use a pre-packaged table class
    # 3. If not, try to autoload it.
    # 4. If it doesn't exist, make it from a prototype and create it in the database.

    # TODO: check options for which tables to produce.

    dbout = options.prefix

    tables = {}
    for coretable in CORETABLES:
        # build the table name
        if getattr(options, coretable.name, None):
            fulltablename = getattr(options, coretable.name)
        else:
            fulltablename = dbout + coretable.name

        # fulltablename is either an arbitrary string or dbout + core name, but not None

        # put table classes into the tables dictionary
        if fulltablename == coretable.name:
            # it's a vanilla table name. just use a pre-packaged table class instead of making one.
            tables[coretable.name] = coretable.table
        else:
            tables[coretable.name] = ps.make_table(fulltablename, coretable.prototype)

        tables[coretable.name].__table__.create(session.bind, checkfirst=True)

    session.commit()

    return tables
Example #5
0
def reflect_or_create_tables(options):
    """
    returns a dict of classes
     make 'em if they don't exist
     "tables" is {'wfdisc': mapped table class, ...}
     """
    tables = {}
    # this list should mirror the command line table options
    for table in mapfns.keys() + ['lastid']:
        #if options.all_tables:
        fulltabnm = getattr(options, table, None)
        if fulltabnm:
            try:
                tables[table] = ps.get_tables(session.bind, [fulltabnm])[0]
            except NoSuchTableError:
                print "{0} doesn't exist. Adding it.".format(fulltabnm)
                tables[table] = ps.make_table(fulltabnm, PROTOTYPES[table])
                tables[table].__table__.create(session.bind, checkfirst=True)

    return tables
Example #6
0
def reflect_or_create_tables(options):
    """
    returns a dict of classes
     make 'em if they don't exist
     "tables" is {'wfdisc': mapped table class, ...}
     """
    tables = {}
    # this list should mirror the command line table options
    for table in mapfns.keys() + ['lastid']:
        #if options.all_tables:
        fulltabnm = getattr(options, table, None)
        if fulltabnm:
            try:
                tables[table] = ps.get_tables(session.bind, [fulltabnm])[0]
            except NoSuchTableError:
                print "{0} doesn't exist. Adding it.".format(fulltabnm)
                tables[table] = ps.make_table(fulltabnm, PROTOTYPES[table])
                tables[table].__table__.create(session.bind, checkfirst=True)

    return tables