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();