[英]How do I checkpoint a django sqlite3 database?
I have a sqlite3 django database that is using WAL. 我有一个正在使用WAL的sqlite3 django数据库。 In operation there are typically three files present:
db.sqlite3, db.sqlite3-shm, db.sqlite3-wal
. 在操作中,通常存在三个文件:
db.sqlite3, db.sqlite3-shm, db.sqlite3-wal
。
To conveniently back up the database (when the service is stopped), I'd like to create a management command to checkpoint the sqlite database in order to write all the changes into db.sqlite3
and delete the other two files. 为了方便地备份数据库(在服务停止时),我想创建一个管理命令来检查sqlite数据库,以便将所有更改写入
db.sqlite3
并删除其他两个文件。
While sqlite3 has a checkpoint API , I don't know how to access this from django (or python's sqlite3 module ) 虽然sqlite3具有检查点API ,但我不知道如何从Django(或python的sqlite3模块 )访问此API
After much digging, you can use a pragma thus: 经过大量挖掘后,您可以这样使用一种编译指示:
from django.core.management.base import BaseCommand
from django.db import connection
class Command(BaseCommand):
help = 'Checkpoint the database, updating <db>.sqlite3 and removing <db>.sqlite3-wal and <db>.sqlite3-shm files'
def add_arguments(self, parser):
parser.add_argument('-cm', '--checkpoint_mode', default='TRUNCATE',
choices=['PASSIVE', 'FULL', 'RESTART', 'TRUNCATE'],
help='Checkpoint mode - See sqlite3 documentation for options (default is TRUNCATE)')
def handle(self, *args, **options):
with connection.cursor() as cursor:
cursor.execute(f"PRAGMA wal_checkpoint({options['checkpoint_mode']});")
result = cursor.fetchone()
print(f'{result}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.