def find_or_insert_format(conn, name):
    with conn.begin():
        query = sql.select([schema.format.c.id], schema.format.c.name == name)
        id = conn.execute(query).scalar()
        if id is None:
            insert_stmt = schema.format.insert().values(name=name)
            id = conn.execute(insert_stmt).inserted_primary_key[0]
            logger.info("Inserted format %d with name %s", id, name)
    return id
Find a format in the database, create it if it doesn't exist yet.
dataset/ETHPy150Open lalinsky/acoustid-server/acoustid/data/format.py/find_or_insert_format