Skip to content

Commit 52b8b7e

Browse files
committed
Add meta table to database
1 parent 049861f commit 52b8b7e

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ The SQLite schema lives in `schema/V1__initial_schema.sql` and is optimized for
5353

5454
### Tables and Columns
5555

56+
**meta**
57+
58+
| Column | Type | Notes |
59+
| :--- | :--- | :--- |
60+
| key | TEXT | Primary key |
61+
| value | TEXT | Required |
62+
63+
Keys stored include `schema_version`, `database_version`, `generated_at`, and `motor_count`.
64+
5665
**manufacturers**
5766

5867
| Column | Type | Notes |

schema/V1__initial_schema.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
-- Enable foreign keys
22
PRAGMA foreign_keys = ON;
33

4+
CREATE TABLE IF NOT EXISTS meta (
5+
key TEXT PRIMARY KEY,
6+
value TEXT NOT NULL
7+
);
8+
49
CREATE TABLE IF NOT EXISTS manufacturers (
510
id INTEGER PRIMARY KEY AUTOINCREMENT,
611
name TEXT NOT NULL UNIQUE,
@@ -36,4 +41,4 @@ CREATE TABLE IF NOT EXISTS thrust_data (
3641
-- Indices for fast lookups in OpenRocket
3742
CREATE INDEX idx_motor_mfr ON motors(manufacturer_id);
3843
CREATE INDEX idx_motor_diameter ON motors(diameter);
39-
CREATE INDEX idx_motor_impulse ON motors(impulse);
44+
CREATE INDEX idx_motor_impulse ON motors(impulse);

scripts/build_database.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,22 @@ def build():
196196
if motor_count == 0:
197197
print("Warning: No motors imported. Check your data directory contents.")
198198

199-
# --- FIX START ---
200-
# You must commit the transaction before running VACUUM
199+
schema_version = 1
200+
database_version = int(datetime.now().strftime("%Y%m%d%H%M%S"))
201+
generated_at = datetime.now().isoformat()
202+
203+
cursor.executemany(
204+
"INSERT OR REPLACE INTO meta (key, value) VALUES (?, ?)",
205+
[
206+
("schema_version", str(schema_version)),
207+
("database_version", str(database_version)),
208+
("generated_at", generated_at),
209+
("motor_count", str(motor_count)),
210+
],
211+
)
212+
213+
# Commit the transaction before running VACUUM
201214
conn.commit()
202-
# --- FIX END ---
203215

204216
# Optimize
205217
conn.execute("VACUUM")
@@ -216,9 +228,9 @@ def build():
216228
sha256.update(f.read())
217229

218230
meta = {
219-
"schema_version": 1,
220-
"database_version": int(datetime.now().strftime("%Y%m%d%H%M%S")),
221-
"generated_at": datetime.now().isoformat(),
231+
"schema_version": schema_version,
232+
"database_version": database_version,
233+
"generated_at": generated_at,
222234
"motor_count": motor_count,
223235
"sha256": sha256.hexdigest(),
224236
"download_url": "https://openrocket.github.io/motor-database/motors.db.gz"

scripts/fetch_updates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
def load_state():
23-
# Fix: Handle empty or corrupt JSON files gracefully
23+
# Handle empty or corrupt JSON files gracefully
2424
if os.path.exists(STATE_FILE):
2525
try:
2626
with open(STATE_FILE, 'r') as f:

0 commit comments

Comments
 (0)