Skip to content

Instantly share code, notes, and snippets.

@koxt
Forked from leotop/fix-fias.json
Created July 13, 2016 08:27
Show Gist options
  • Save koxt/fc329a5927ef555e2390e2d4f03836de to your computer and use it in GitHub Desktop.
Save koxt/fc329a5927ef555e2390e2d4f03836de to your computer and use it in GitHub Desktop.
Импорт DBF ФИАС в PostgreSQL
{
"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"
}
}
}
#!/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)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment