backup sqlite db to json with tinyDB
This commit is contained in:
parent
9b578637ba
commit
ab54ab981a
2 changed files with 10 additions and 34 deletions
|
@ -1,8 +1,10 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
import json
|
||||||
from playhouse.db_url import connect
|
from playhouse.db_url import connect
|
||||||
|
from playhouse.shortcuts import model_to_dict
|
||||||
|
from tinydb import TinyDB
|
||||||
from stacosys.conf import config
|
from stacosys.conf import config
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,27 +17,21 @@ def setup():
|
||||||
from stacosys.model.comment import Comment
|
from stacosys.model.comment import Comment
|
||||||
|
|
||||||
get_db().create_tables([Site, Comment], safe=True)
|
get_db().create_tables([Site, Comment], safe=True)
|
||||||
|
if config.exists(config.DB_BACKUP_JSON_FILE):
|
||||||
|
_backup_db(config.DB_BACKUP_JSON_FILE, Comment)
|
||||||
|
|
||||||
|
|
||||||
from playhouse.shortcuts import model_to_dict
|
def _tojson_model(comment):
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
def tojson_model(comment):
|
|
||||||
dcomment = model_to_dict(comment)
|
dcomment = model_to_dict(comment)
|
||||||
del dcomment["site"]
|
# del dcomment["site"]
|
||||||
tcomment = json.dumps(dcomment, indent=4, sort_keys=True, default=str)
|
tcomment = json.dumps(dcomment, indent=4, sort_keys=True, default=str)
|
||||||
return json.loads(tcomment)
|
return json.loads(tcomment)
|
||||||
|
|
||||||
|
|
||||||
def dump_db():
|
def _backup_db(db_file, Comment):
|
||||||
from tinydb import TinyDB, Query
|
db = TinyDB(db_file, sort_keys=True, indent=4, separators=(",", ": "))
|
||||||
from stacosys.model.comment import Comment
|
|
||||||
|
|
||||||
db = TinyDB("db.json", sort_keys=True, indent=4, separators=(",", ": "))
|
|
||||||
db.drop_tables()
|
db.drop_tables()
|
||||||
table = db.table("comments")
|
table = db.table("comments")
|
||||||
for comment in Comment.select():
|
for comment in Comment.select():
|
||||||
cc = tojson_model(comment)
|
cc = _tojson_model(comment)
|
||||||
print(cc)
|
|
||||||
table.insert(cc)
|
table.insert(cc)
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: UTF-8 -*-
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def persistence():
|
|
||||||
from stacosys.conf import config
|
|
||||||
|
|
||||||
config.params = {"main.db_file": None}
|
|
||||||
from stacosys.core import persistence
|
|
||||||
|
|
||||||
return persistence.Persistence()
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_persistence(persistence):
|
|
||||||
assert persistence is not None
|
|
||||||
assert persistence.get_db() is not None
|
|
||||||
assert persistence.get_table_comments() is not None
|
|
Loading…
Add table
Reference in a new issue