(curs, name)
Create new object name for case the old exists.

    Needed when creating a new table besides old one.
Create new object name for case the old exists.
Create new object name for case the old exists .
def find_new_name(curs, name):
    """Create new object name for case the old exists.

    Needed when creating a new table besides old one.
    """
    # cut off previous numbers
    m = re.search('_[0-9]+$', name)
    if m:
        name = name[:m.start()]

    # now loop
    for i in range(1, 1000):
        tname = "%s_%d" % (name, i)
        q = "select count(1) from pg_class where relname = %s"
        curs.execute(q, [tname])
        if curs.fetchone()[0] == 0:
            return tname

    # failed
    raise Exception('find_new_name failed')
def find_new_name ( curs , name ) : # cut off previous numbers m = re . search ( '_[0-9]+$' , name ) if m : name = name [ : m . start ( ) ] # now loop for i in range ( 1 , 1000 ) : tname = "%s_%d" % ( name , i ) q = "select count(1) from pg_class where relname = %s" curs . execute ( q , [ tname ] ) if curs . fetchone ( ) [ 0 ] == 0 : return tname # failed raise Exception ( 'find_new_name failed' )