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
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import json
|
||||
from playhouse.db_url import connect
|
||||
|
||||
from playhouse.shortcuts import model_to_dict
|
||||
from tinydb import TinyDB
|
||||
from stacosys.conf import config
|
||||
|
||||
|
||||
|
@ -15,27 +17,21 @@ def setup():
|
|||
from stacosys.model.comment import Comment
|
||||
|
||||
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
|
||||
import json
|
||||
|
||||
|
||||
def tojson_model(comment):
|
||||
def _tojson_model(comment):
|
||||
dcomment = model_to_dict(comment)
|
||||
del dcomment["site"]
|
||||
# del dcomment["site"]
|
||||
tcomment = json.dumps(dcomment, indent=4, sort_keys=True, default=str)
|
||||
return json.loads(tcomment)
|
||||
|
||||
|
||||
def dump_db():
|
||||
from tinydb import TinyDB, Query
|
||||
from stacosys.model.comment import Comment
|
||||
|
||||
db = TinyDB("db.json", sort_keys=True, indent=4, separators=(",", ": "))
|
||||
def _backup_db(db_file, Comment):
|
||||
db = TinyDB(db_file, sort_keys=True, indent=4, separators=(",", ": "))
|
||||
db.drop_tables()
|
||||
table = db.table("comments")
|
||||
for comment in Comment.select():
|
||||
cc = tojson_model(comment)
|
||||
print(cc)
|
||||
cc = _tojson_model(comment)
|
||||
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