from typing import Union
from pathlib import Path
import sqlite3


def is_database_locked(db: Union[str, Path]) -> bool:
   """Checks is database locked

   Notice:
      This function will block the thread for a while (the timeout is set to 0)
   """

   conn = sqlite3.connect(str(db), timeout=0)

   try:
      conn.execute('PRAGMA user_version')
   except sqlite3.OperationalError as err:
      if str(err) == 'database is locked':
         return True

      raise err from None

   return False