Added sqlite
This commit is contained in:
parent
73a7c93e64
commit
f01186df79
14 changed files with 784 additions and 382 deletions
61
lib/services/db_schema.dart
Normal file
61
lib/services/db_schema.dart
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
const createVehiclesTableSql = '''
|
||||
CREATE TABLE vehicles (
|
||||
id TEXT PRIMARY KEY,
|
||||
make TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
color TEXT NOT NULL,
|
||||
license_plate TEXT NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
deleted_at INTEGER,
|
||||
dirty INTEGER NOT NULL DEFAULT 1
|
||||
)
|
||||
''';
|
||||
|
||||
const createFuelEntriesTableSql = '''
|
||||
CREATE TABLE fuel_entries (
|
||||
id TEXT PRIMARY KEY,
|
||||
vehicle_id TEXT NOT NULL,
|
||||
date INTEGER NOT NULL,
|
||||
gallons REAL NOT NULL,
|
||||
price_per_gallon REAL NOT NULL,
|
||||
total_cost REAL NOT NULL,
|
||||
receipt_image_path TEXT,
|
||||
receipt_drive_file_id TEXT,
|
||||
updated_at INTEGER NOT NULL,
|
||||
deleted_at INTEGER,
|
||||
dirty INTEGER NOT NULL DEFAULT 1
|
||||
)
|
||||
''';
|
||||
|
||||
const createFuelEntriesIndexSql =
|
||||
'CREATE INDEX idx_fuel_entries_vehicle_id ON fuel_entries(vehicle_id)';
|
||||
|
||||
/// Merges attached `remote_db` rows into `main` (the live local database):
|
||||
/// any remote row that's new to us, or newer than our copy, replaces ours.
|
||||
/// Rows this doesn't touch — including our own not-yet-pushed edits — are
|
||||
/// left alone, since the WHERE clause only matches rows remote should win;
|
||||
/// no separate "keep local" statement is needed.
|
||||
const mergeVehiclesSql = '''
|
||||
INSERT OR REPLACE INTO main.vehicles
|
||||
(id, make, model, color, license_plate, updated_at, deleted_at, dirty)
|
||||
SELECT r.id, r.make, r.model, r.color, r.license_plate, r.updated_at, r.deleted_at, 0
|
||||
FROM remote_db.vehicles r
|
||||
LEFT JOIN main.vehicles l ON l.id = r.id
|
||||
WHERE l.id IS NULL OR r.updated_at > l.updated_at
|
||||
''';
|
||||
|
||||
/// Same rule as [mergeVehiclesSql]. `receipt_image_path` is always forced
|
||||
/// to NULL on the merged-in copy — it's a local filesystem path, meaningless
|
||||
/// (and never valid) on another device, and rows are only ever pushed to
|
||||
/// Drive once their pending receipt has already been uploaded there, so a
|
||||
/// remote row should never legitimately have one anyway.
|
||||
const mergeFuelEntriesSql = '''
|
||||
INSERT OR REPLACE INTO main.fuel_entries
|
||||
(id, vehicle_id, date, gallons, price_per_gallon, total_cost,
|
||||
receipt_image_path, receipt_drive_file_id, updated_at, deleted_at, dirty)
|
||||
SELECT r.id, r.vehicle_id, r.date, r.gallons, r.price_per_gallon, r.total_cost,
|
||||
NULL, r.receipt_drive_file_id, r.updated_at, r.deleted_at, 0
|
||||
FROM remote_db.fuel_entries r
|
||||
LEFT JOIN main.fuel_entries l ON l.id = r.id
|
||||
WHERE l.id IS NULL OR r.updated_at > l.updated_at
|
||||
''';
|
||||
Loading…
Add table
Add a link
Reference in a new issue