@cexi/hoonfca 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +363 -2
- package/dist/cjs.cjs +9 -0
- package/dist/global-types.d.ts +2 -0
- package/dist/index.d.mts +1254 -0
- package/dist/index.d.ts +1254 -0
- package/dist/index.js +17481 -0
- package/dist/index.mjs +17435 -0
- package/docs/ARCHITECTURE.md +467 -0
- package/docs/DOCS.md +709 -0
- package/docs/sqlite-cache-recovery.md +49 -0
- package/fca-config.example.json +33 -0
- package/npm-shrinkwrap.json +4859 -0
- package/package.json +111 -4
- package/scripts/check-package-exports.cjs +84 -0
- package/scripts/recover-sqlite-cache.cjs +504 -0
- package/scripts/sqlite-cache-dump.py +123 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import pathlib
|
|
3
|
+
import re
|
|
4
|
+
import sqlite3
|
|
5
|
+
import sys
|
|
6
|
+
import urllib.parse
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def quote_identifier(identifier):
|
|
10
|
+
return '"' + identifier.replace('"', '""') + '"'
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def open_source(source):
|
|
14
|
+
source_path = pathlib.Path(source).resolve()
|
|
15
|
+
source_uri = "file:" + urllib.parse.quote(str(source_path), safe="/") + "?mode=ro"
|
|
16
|
+
connection = sqlite3.connect(source_uri, uri=True)
|
|
17
|
+
connection.execute("PRAGMA query_only=ON")
|
|
18
|
+
return connection
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def open_destination(source, destination):
|
|
22
|
+
source_path = pathlib.Path(source).resolve()
|
|
23
|
+
destination_path = pathlib.Path(destination).resolve()
|
|
24
|
+
if source_path == destination_path:
|
|
25
|
+
raise ValueError("Source and destination paths must differ")
|
|
26
|
+
if not destination_path.is_file() or destination_path.stat().st_size != 0:
|
|
27
|
+
raise ValueError("Destination must be a new empty file")
|
|
28
|
+
return sqlite3.connect(destination_path)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def dump_sql(source, destination):
|
|
32
|
+
source_db = open_source(source)
|
|
33
|
+
target_db = open_destination(source, destination)
|
|
34
|
+
try:
|
|
35
|
+
target_db.execute("PRAGMA foreign_keys=OFF")
|
|
36
|
+
for statement in source_db.iterdump():
|
|
37
|
+
target_db.execute(statement)
|
|
38
|
+
target_db.commit()
|
|
39
|
+
target_db.execute("PRAGMA foreign_keys=ON")
|
|
40
|
+
except Exception:
|
|
41
|
+
target_db.rollback()
|
|
42
|
+
raise
|
|
43
|
+
finally:
|
|
44
|
+
target_db.close()
|
|
45
|
+
source_db.close()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def salvage_rows(source, destination):
|
|
49
|
+
source_db = open_source(source)
|
|
50
|
+
target_db = open_destination(source, destination)
|
|
51
|
+
try:
|
|
52
|
+
target_db.execute("PRAGMA foreign_keys=OFF")
|
|
53
|
+
tables = source_db.execute(
|
|
54
|
+
"SELECT name FROM sqlite_master WHERE type='table' "
|
|
55
|
+
"AND name NOT LIKE 'sqlite_%' ORDER BY name"
|
|
56
|
+
).fetchall()
|
|
57
|
+
target_db.execute("BEGIN")
|
|
58
|
+
|
|
59
|
+
for (table_name,) in tables:
|
|
60
|
+
escaped_table = quote_identifier(table_name)
|
|
61
|
+
columns = source_db.execute(
|
|
62
|
+
f"PRAGMA table_info({escaped_table})"
|
|
63
|
+
).fetchall()
|
|
64
|
+
if not columns:
|
|
65
|
+
raise ValueError(f"Could not read columns for table {table_name}")
|
|
66
|
+
|
|
67
|
+
definitions = []
|
|
68
|
+
for column in columns:
|
|
69
|
+
declared_type = column[2] or ""
|
|
70
|
+
if not re.fullmatch(r"[A-Za-z0-9_ ()]*", declared_type):
|
|
71
|
+
declared_type = ""
|
|
72
|
+
definitions.append(
|
|
73
|
+
f"{quote_identifier(column[1])} {declared_type}".rstrip()
|
|
74
|
+
)
|
|
75
|
+
target_db.execute(
|
|
76
|
+
f"CREATE TABLE {escaped_table} ({', '.join(definitions)})"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
column_names = ", ".join(
|
|
80
|
+
quote_identifier(column[1])
|
|
81
|
+
for column in columns
|
|
82
|
+
)
|
|
83
|
+
placeholders = ", ".join("?" for _ in columns)
|
|
84
|
+
cursor = source_db.execute(
|
|
85
|
+
f"SELECT {column_names} FROM {escaped_table}"
|
|
86
|
+
)
|
|
87
|
+
insert = (
|
|
88
|
+
f"INSERT INTO {escaped_table} ({column_names}) "
|
|
89
|
+
f'VALUES ({placeholders})'
|
|
90
|
+
)
|
|
91
|
+
while True:
|
|
92
|
+
rows = cursor.fetchmany(500)
|
|
93
|
+
if not rows:
|
|
94
|
+
break
|
|
95
|
+
target_db.executemany(insert, rows)
|
|
96
|
+
|
|
97
|
+
target_db.commit()
|
|
98
|
+
except Exception:
|
|
99
|
+
target_db.rollback()
|
|
100
|
+
raise
|
|
101
|
+
finally:
|
|
102
|
+
target_db.close()
|
|
103
|
+
source_db.close()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
if len(sys.argv) not in (3, 4):
|
|
108
|
+
print(
|
|
109
|
+
"Usage: sqlite-cache-dump.py [--salvage-rows] "
|
|
110
|
+
"<read-only-source-copy> <new-empty-destination>",
|
|
111
|
+
file=sys.stderr
|
|
112
|
+
)
|
|
113
|
+
sys.exit(2)
|
|
114
|
+
try:
|
|
115
|
+
if len(sys.argv) == 4 and sys.argv[1] == "--salvage-rows":
|
|
116
|
+
salvage_rows(sys.argv[2], sys.argv[3])
|
|
117
|
+
elif len(sys.argv) == 3:
|
|
118
|
+
dump_sql(sys.argv[1], sys.argv[2])
|
|
119
|
+
else:
|
|
120
|
+
raise ValueError("Unknown recovery mode")
|
|
121
|
+
except Exception as error:
|
|
122
|
+
print(f"{type(error).__name__}: {error}", file=sys.stderr)
|
|
123
|
+
sys.exit(1)
|