From f14947b92a83e4835ae8f6d474e7b5ada3c41c1d Mon Sep 17 00:00:00 2001 From: R0drig0-P Date: Tue, 5 Nov 2024 22:39:23 +0000 Subject: [PATCH] updated restaurant database --- .../database/app_restaurant_database.dart | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart index 1a3b8428f..9b81df36f 100644 --- a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart +++ b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'package:intl/intl.dart'; import 'package:sqflite/sqflite.dart'; @@ -10,15 +11,22 @@ class RestaurantDatabase extends AppDatabase> { RestaurantDatabase() : super( 'restaurant.db', - [ - ''' + [ createScript, _createScript ], + onUpgrade: migrate, + version: 2, + ); + + static const createScript = + ''' CREATE TABLE RESTAURANTS( id INTEGER PRIMARY KEY, ref TEXT, name TEXT, meals TEXT) - ''', - ''' + '''; + + static const _createScript = + ''' CREATE TABLE MEALS( id INTEGER PRIMARY KEY AUTOINCREMENT, day TEXT, @@ -27,9 +35,7 @@ class RestaurantDatabase extends AppDatabase> { name TEXT, id_restaurant INTEGER, FOREIGN KEY (id_restaurant) REFERENCES RESTAURANTS(id)) - ''' - ], - ); + '''; /// Get all restaurants and meals, if day is null, all meals are returned Future> restaurants({DayOfWeek? day}) async { @@ -129,6 +135,19 @@ class RestaurantDatabase extends AppDatabase> { await txn.delete('restaurants'); } + static FutureOr migrate( + Database db, + int oldVersion, + int newVersion, + ) async { + final batch = db.batch() + ..execute('DROP TABLE IF EXISTS RESTAURANTS') + ..execute('DROP TABLE IF EXISTS MEALS') + ..execute(createScript) + ..execute(_createScript); + await batch.commit(); + } + @override Future saveToDatabase(List data) async { final db = await getDatabase();