Skip to content

Instantly share code, notes, and snippets.

@leotop
Forked from dpwiz/fix-fias.json
Created July 12, 2016 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leotop/22c099a32e2670a278a83095dcf74813 to your computer and use it in GitHub Desktop.
Save leotop/22c099a32e2670a278a83095dcf74813 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