generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from bson.json_util import dumps | ||
import json | ||
|
||
def save_db(db, filename="dump.json"): | ||
collections = db.collection_names() | ||
db_dump_str = "{" | ||
delim = "" | ||
for i, collection_name in enumerate(collections): | ||
col = getattr(db, collections[i]) | ||
collection = col.find() | ||
db_dump_str += delim + "{\"" + collection_name + "\":[" | ||
db_dump_str += dumps(collection) + "]" | ||
delim = "," | ||
|
||
db_dump_str += "}" | ||
try: | ||
with open(filename, 'wb') as jsonfile: | ||
jsonfile.write(str.encode(db_dump_str)) | ||
except (IOError, OSError): | ||
print("ошибка записи") | ||
return False | ||
except (FileNotFoundError, PermissionError, OSError): | ||
print("ошибка открытия файла") | ||
return False | ||
|
||
return True | ||
|
||
|
||
def restore_db(db, import_json): | ||
json_colls = json.loads(import_json) | ||
for col_name in json_colls: | ||
col = db[col_name] | ||
col.insert_many(json_colls[col_name]) | ||
max_uid = len(json_colls["users"]) + 1 | ||
max_gid = len(json_colls["games"]) + 1 | ||
max_eid = len(json_colls["events"]) + 1 | ||
|
||
return max_uid, max_gid, max_eid |