Skip to content

Instantly share code, notes, and snippets.

@Dem0n3D
Forked from dpwiz/fix-fias.json
Last active May 29, 2016 15:40
Show Gist options
  • Save Dem0n3D/1291008556f68de1380a to your computer and use it in GitHub Desktop.
Save Dem0n3D/1291008556f68de1380a to your computer and use it in GitHub Desktop.
{
"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 -s cp866 $file | psql fias
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment