anki-sync-server/tests/helpers/db_utils.py

69 lines
1.8 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
import os
import sqlite3
import subprocess
2017-11-01 11:04:48 +08:00
def from_sql(path, sql):
"""
Creates an SQLite db and executes the passed sql statements on it.
2017-11-01 11:04:48 +08:00
:param path: the path to the created db file
:param sql: the sql statements to execute on the newly created db
"""
2017-11-01 11:04:48 +08:00
connection = sqlite3.connect(path)
cursor = connection.cursor()
cursor.executescript(sql)
connection.commit()
connection.close()
2017-11-01 11:04:48 +08:00
def to_sql(database):
"""
Returns a string containing the sql export of the database. Used for
debugging.
2017-11-01 11:04:48 +08:00
:param database: either the path to the SQLite db file or an open
connection to it
:return: a string representing the sql export of the database
"""
2017-11-01 11:04:48 +08:00
if type(database) == str:
connection = sqlite3.connect(database)
else:
connection = database
2022-10-15 03:07:21 +08:00
res = "\n".join(connection.iterdump())
2017-11-01 11:04:48 +08:00
if type(database) == str:
connection.close()
2017-11-01 11:04:48 +08:00
return res
def diff(left_db_path, right_db_path):
"""
Uses the sqldiff cli tool to compare two sqlite files for equality.
Returns True if the databases differ, False if they don't.
:param left_db_path: path to the left db file
:param right_db_path: path to the right db file
:return: True if the specified databases differ, False else
"""
2017-11-01 11:06:06 +08:00
command = ["sqldiff", left_db_path, right_db_path]
2017-11-01 11:04:48 +08:00
2022-10-15 03:07:21 +08:00
child_process = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE)
2017-11-01 11:04:48 +08:00
stdout, stderr = child_process.communicate()
exit_code = child_process.returncode
if exit_code != 0 or stderr is not None:
2022-10-15 03:07:21 +08:00
raise RuntimeError(
"Command {} encountered an error, exit "
"code: {}, stderr: {}".format(" ".join(command), exit_code, stderr)
)
2017-11-01 11:04:48 +08:00
2022-10-15 03:07:21 +08:00
# Any output from sqldiff means the databases differ.
2017-11-01 11:04:48 +08:00
return stdout != ""