Exemplo 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)
        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 as e:
                print("ALTER TABLE ERROR::" , e)
            continue
        print("Column:", ocol.name, "|", ocol.format_type, "|", ocol.sql_nullable, "|", ocol.format_extra)
    print()
Exemplo 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 as e:
                print("ALTER TABLE ERROR::", e)
            continue
        print("Column:", ocol.name, "|", ocol.format_type, "|",
              ocol.sql_nullable, "|", ocol.format_extra)
    print()