-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate from PysonDB to SQLite for database management
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
- Loading branch information
Showing
9 changed files
with
150 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,59 @@ | ||
"""basic interface to the json database""" | ||
"""Interface to the database which stores the package information""" | ||
|
||
import sqlite3 | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from pysondb import PysonDB | ||
|
||
|
||
def get_database(publish_path: Path, production: bool = False) -> sqlite3.Connection: | ||
def get_database(db_path: Path, production: bool = False) -> sqlite3.Connection: | ||
""" | ||
Open the sqlite database at the given path. | ||
The database should be located in a subfolder `/data` of the root path. | ||
The database name is determined by the production flag as `all_packages[_test].db` | ||
""" | ||
db_path = Path(db_path) | ||
if db_path.stem == "publish": | ||
db_path = db_path / ".." / "data" # go up one level to find the database | ||
|
||
publish_path = Path(publish_path) | ||
db_path = publish_path / f"all_packages{'' if production else '_test'}.db" | ||
if not db_path.exists(): | ||
raise FileNotFoundError("Database file not found") | ||
db_path = db_path / f"all_packages{'' if production else '_test'}.db" | ||
if db_path.exists(): | ||
conn = sqlite3.connect(db_path) | ||
|
||
conn = sqlite3.connect(db_path) | ||
else: | ||
print(FileNotFoundError(f"Database file not found in path: {db_path}")) | ||
conn = create_database(db_path) | ||
|
||
conn.row_factory = sqlite3.Row # return rows as dicts | ||
return conn | ||
|
||
|
||
def create_database(db_path: Path) -> sqlite3.Connection: | ||
""" | ||
Create a new database at the given path. | ||
The database should be located in a subfolder `/data` of the root path. | ||
""" | ||
db_path = Path(db_path) | ||
if not db_path.parent.exists(): | ||
db_path.parent.mkdir(parents=True) | ||
conn = sqlite3.connect(db_path) | ||
SCHEMA = """ | ||
CREATE TABLE "packages" ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT, | ||
description TEXT, | ||
mpy_version TEXT, | ||
pkg_version TEXT, | ||
publish BOOLEAN, | ||
stub_sources TEXT, -- json string | ||
path TEXT, | ||
hash TEXT, | ||
stub_hash TEXT, | ||
port TEXT DEFAULT "", | ||
board TEXT DEFAULT "", | ||
variant TEXT DEFAULT "" | ||
) | ||
""" | ||
conn.execute(SCHEMA) | ||
conn.commit | ||
return conn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.