Barolina
5/31/2016 - 9:54 AM

Импорт DBF ФИАС в PostgreSQL

Импорт DBF ФИАС в PostgreSQL

mkdir house normdoc
mv house*.dbf house/
mv normdoc*.dbf normdoc/

for file in *.dbf; do
    echo "Importing $file..."
    pgdbf $file | iconv -f cp866 -t utf-8 | psql fias
done
#!/usr/bin/env python

convert_sql = """
BEGIN;
    alter table {table} rename column {column} to {column}_x;
    alter table {table} add column {column} {cast};
    update {table} set {column} = {column}_x;
    alter table {table} drop column {column}_x;
COMMIT;
"""

def convert(table, column, cast):
    return convert_sql.format(table=table, column=column, cast=cast)

def null(table, column):
    return "UPDATE {table} SET {column} = NULL WHERE {column} = '';".format(table=table, column=column)

if __name__=='__main__':
    import sys, json

    spec = json.load(open(sys.argv[1]))
    for table, task in spec.iteritems():
        for column in task.get('null', []):
            print null(table, column)
        for column, cast in sorted(task.get('convert', {}).iteritems()):
            print convert(table, column, cast)
{
    "actstat": {
        "convert": {
            "actstatid": "int"
        }
    },
    "addrobj": {
        "null": [
            "actstat",
            "nextid",
            "previd",
            "terrifnsfl",
            "terrifnsul",
            "ifnsfl",
            "ifnsul",
            "normdoc",
            "parentguid",
            "okato",
            "oktmo",
            "enddate",
            "startdate",
            "postalcode"
        ],
        "convert": {
            "aoguid": "uuid",
            "aoid": "uuid",
            "parentguid": "uuid",
            "nextid": "uuid",
            "previd": "uuid",
            "normdoc": "uuid",

            "actstatus": "int",
            "aolevel": "int",
            "centstatus": "int",
            "currstatus": "int",
            "operstatus": "int",
            "livestatus": "int"
        }
    },
    "centerst": {
        "convert": {
            "centerstid": "int"
        }
    },
    "curentst": {
        "convert": {
            "curentstid": "int"
        }
    },
    "eststat": {
        "convert": {
            "eststatid": "int"
        }
    },
    "hststat": {
        "convert": {
            "hststatid": "int"
        }
    },
    "intvstat": {
        "convert": {
            "intvstatid": "int"
        }
    },
    "ndoctype": {
        "convert": {
            "ndtypeid": "int"
        }
    },
    "operstat": {
        "convert": {
            "operstatid": "int"
        }
    },
    "socrbase": {
        "convert": {
            "level": "int"
        }
    },
    "strstat": {
        "convert": {
            "strstatid": "int"
        }
    }
}