Esempio n. 1
0
def update_table(conn, table):
    cur = conn.cursor()
    dbfields = {}
    otable = pginspect.get_relname_oid(conn, table.name)
    print "Table:", otable.namespace, otable.name
    for ocol in pginspect.get_table_columns(conn, otable):
        dbfields[ocol.name] = ocol
        
    for field in table.fields:
        ocol = dbfields.get(field.name,None)
        print "Field:", field.name, "|", field.sql_type, "|", field.sql_nullable, "|", field.default
        default = None
        if field.default:
            default = field.default
        elif not field.nullable:
            default = pginspect.default4type(field.sql_type)
            
        if ocol is None:
            # TODO: Agregar el campo
            try:
                if default is not None:
                    cur.execute("""ALTER TABLE "%s" ADD COLUMN "%s" %s DEFAULT %%s""" % (otable.name, field.name, field.sql_definition),[default])
                else:
                    cur.execute("""ALTER TABLE "%s" ADD COLUMN "%s" %s """ % (otable.name, field.name, field.sql_definition))
            except Exception, e:
                print "ALTER TABLE ERROR::" , e
            continue
        pginspect.field2serial(ocol)
        if ocol.format_type != field.sql_type:
            print "Column type has changed:"
            print "Column:", ocol.name, "|", ocol.format_type, "|", ocol.sql_nullable, "|", ocol.format_extra
            try:
                cur.execute("""ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s USING "%s"::%s""" % (otable.name, field.name, field.sql_type, field.name, field.sql_type))
            except Exception, e:
                print "ALTER TABLE ERROR::" , e
Esempio n. 2
0
def update_table(conn, table):
    cur = conn.cursor()
    dbfields = {}
    otable = pginspect.get_relname_oid(conn, table.name)
    print "Table:", otable.namespace, otable.name
    for ocol in pginspect.get_table_columns(conn, otable):
        dbfields[ocol.name] = ocol
        
    for field in table.fields:
        ocol = dbfields.get(field.name,None)
        print "Field:", field.name, "|", field.sql_type, "|", field.sql_nullable, "|", field.default
        if ocol is None:
            # TODO: Agregar el campo
            try:
                cur.execute("""ALTER TABLE "%s" ADD COLUMN "%s" %s """ % (otable.name, field.name, field.sql_definition))
            except Exception, e:
                print "ALTER TABLE ERROR::" , e
            continue
        print "Column:", ocol.name, "|", ocol.format_type, "|", ocol.sql_nullable, "|", ocol.format_extra