Skip to content

Commit

Permalink
chore: bring back sqflite dependency, improve database initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hjiangsu committed Nov 1, 2024
1 parent daa18f3 commit f92770a
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 71 deletions.
26 changes: 25 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ PODS:
- sqflite_darwin (0.0.4):
- Flutter
- FlutterMacOS
- sqlite3 (3.47.0):
- sqlite3/common (= 3.47.0)
- sqlite3/common (3.47.0)
- sqlite3/dbstatvtab (3.47.0):
- sqlite3/common
- sqlite3/fts5 (3.47.0):
- sqlite3/common
- sqlite3/perf-threadsafe (3.47.0):
- sqlite3/common
- sqlite3/rtree (3.47.0):
- sqlite3/common
- sqlite3_flutter_libs (0.0.1):
- Flutter
- sqlite3 (~> 3.47.0)
- sqlite3/dbstatvtab
- sqlite3/fts5
- sqlite3/perf-threadsafe
- sqlite3/rtree
- uni_links (0.0.1):
- Flutter
- url_launcher_ios (0.0.1):
Expand Down Expand Up @@ -84,6 +102,7 @@ DEPENDENCIES:
- share_plus (from `.symlinks/plugins/share_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite_darwin (from `.symlinks/plugins/sqflite_darwin/darwin`)
- sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/ios`)
- uni_links (from `.symlinks/plugins/uni_links/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/darwin`)
Expand All @@ -92,6 +111,7 @@ DEPENDENCIES:
SPEC REPOS:
trunk:
- OrderedSet
- sqlite3

EXTERNAL SOURCES:
background_fetch:
Expand Down Expand Up @@ -136,6 +156,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
sqflite_darwin:
:path: ".symlinks/plugins/sqflite_darwin/darwin"
sqlite3_flutter_libs:
:path: ".symlinks/plugins/sqlite3_flutter_libs/ios"
uni_links:
:path: ".symlinks/plugins/uni_links/ios"
url_launcher_ios:
Expand Down Expand Up @@ -168,11 +190,13 @@ SPEC CHECKSUMS:
share_plus: 8b6f8b3447e494cca5317c8c3073de39b3600d1f
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
sqflite_darwin: a553b1fd6fe66f53bbb0fe5b4f5bab93f08d7a13
sqlite3: 0aa20658a9b238a3b1ff7175eb7bdd863b0ab4fd
sqlite3_flutter_libs: b55ef23cfafea5318ae5081e0bf3fbbce8417c94
uni_links: d97da20c7701486ba192624d99bffaaffcfc298a
url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe
video_player_avfoundation: 7c6c11d8470e1675df7397027218274b6d2360b3
webview_flutter_wkwebview: 0982481e3d9c78fd5c6f62a002fcd24fc791f1e4

PODFILE CHECKSUM: 8d23d5c4d896af3a5f2a08e0206462ca9882e556

COCOAPODS: 1.15.2
COCOAPODS: 1.16.0
23 changes: 7 additions & 16 deletions lib/core/database/connection/native.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import 'dart:io';

import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';
import 'package:drift_dev/api/migrations.dart';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:drift/native.dart';
import '../database.dart';

Future<File> get databaseFile async {
// We use `path_provider` to find a suitable path to store our data in.
final appDir = await getApplicationDocumentsDirectory();
final dbPath = p.join(appDir.path, 'todo-app.sqlite');
return File(dbPath);
}
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(join(dbFolder.path, 'thunder.sqlite'));

AppDatabase constructDb() {
final db = LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return NativeDatabase(file);
});
return AppDatabase(db);
return file;
}

Future<void> validateDatabaseSchema(GeneratedDatabase database) async {
// This method validates that the actual schema of the opened database matches
// the tables, views, triggers and indices for which drift_dev has generated
// code.
//
// Validating the database's schema after opening it is generally a good idea,
// since it allows us to get an early warning if we change a table definition
// without writing a schema migration for it.
Expand Down
5 changes: 0 additions & 5 deletions lib/core/database/connection/unsupported.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import 'package:drift/drift.dart';
import '../database.dart';

Never _unsupported() {
throw UnsupportedError('No suitable database implementation was found on this platform.');
}

// Depending on the platform the app is compiled to, the following stubs will
// be replaced with the methods in native.dart or web.dart
AppDatabase constructDb() => throw UnimplementedError();

Future<void> validateDatabaseSchema(GeneratedDatabase database) async {
_unsupported();
}
46 changes: 17 additions & 29 deletions lib/core/database/connection/web.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';
import 'package:drift/wasm.dart';
import '../database.dart';

Future<void> validateDatabaseSchema(GeneratedDatabase database) async {
// Unfortunately, validating database schemas only works for native platforms
// right now.
// As we also have migration tests (see the `Testing migrations` section in
// the readme of this example), this is not a huge issue.
}

DatabaseConnection connectOnWeb() {
return DatabaseConnection.delayed(Future(() async {
final result = await WasmDatabase.open(
databaseName: 'thunder', // prefer to only use valid identifiers here
sqlite3Uri: Uri.parse('sqlite3.wasm'),
driftWorkerUri: Uri.parse('drift_worker.dart.js'),
);

if (result.missingFeatures.isNotEmpty) {
// Depending how central local persistence is to your app, you may want
// to show a warning to the user if only unrealiable implemetentations
// are available.
print('Using ${result.chosenImplementation} due to missing browser '
'features: ${result.missingFeatures}');
}

return result.resolvedExecutor;
}));
}
// This method validates that the actual schema of the opened database matches
// the tables, views, triggers and indices for which drift_dev has generated
// code.
//
// Validating the database's schema after opening it is generally a good idea,
// since it allows us to get an early warning if we change a table definition
// without writing a schema migration for it.
//
// For details, see: https://drift.simonbinder.eu/docs/advanced-features/migrations/#verifying-a-database-schema-at-runtime
if (kDebugMode) {
// TODO: Add validation when updating to drift 2.22
// final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('/sqlite3.wasm'));
// sqlite.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true);

AppDatabase constructDb() {
return AppDatabase(connectOnWeb());
// await VerifySelf(database).validateDatabaseSchema(sqlite3: sqlite);
}
}
21 changes: 19 additions & 2 deletions lib/core/database/database.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:io';

import 'package:drift/drift.dart';
import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';
import 'package:drift_flutter/drift_flutter.dart';
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

import 'package:thunder/core/database/schema_versions.dart';
import 'package:thunder/core/database/tables.dart';
import 'package:thunder/core/database/type_converters.dart';
Expand All @@ -16,7 +19,21 @@ part 'database.g.dart';

@DriftDatabase(tables: [Accounts, Favorites, LocalSubscriptions, UserLabels, Drafts])
class AppDatabase extends _$AppDatabase {
AppDatabase(super.e);
AppDatabase()
: super(
driftDatabase(
name: 'thunder',
web: DriftWebOptions(
sqlite3Wasm: Uri.parse('sqlite3.wasm'),
driftWorker: Uri.parse('drift_worker.js'),
onResult: (result) {
if (result.missingFeatures.isNotEmpty) {
debugPrint('Using ${result.chosenImplementation} due to unsupported browser features: ${result.missingFeatures}');
}
},
),
),
);

@override
int get schemaVersion => 5;
Expand Down
21 changes: 12 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ bool _isDatabaseInitialized = false;
Future<void> initializeDatabase() async {
if (_isDatabaseInitialized) return;

debugPrint('Initializing drift db.');
if (kIsWeb) {
database = AppDatabase();
return;
}

database = constructDb();
// There is a specific ordering here.
// We're checking to see if the drift database exists. If it doesn't exist, we perform migration from the old SQLite database.
// The ordering matters here as database = AppDatabase() will create the database if it doesn't exist.
File dbFile = File(join((await getApplicationDocumentsDirectory()).path, 'thunder.sqlite'));

// Bypass migrateToSQList for web.
if (!kIsWeb) {
File dbFile = File(join((await getApplicationDocumentsDirectory()).path, 'thunder.sqlite'));
database = AppDatabase();

if (!await dbFile.exists()) {
debugPrint('Migrating from SQLite db.');
await migrateToSQLite(database);
}
if (!await dbFile.exists()) {
debugPrint('Migrating from SQLite db.');
await migrateToSQLite(database);
}

_isDatabaseInitialized = true;
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <dynamic_color/dynamic_color_plugin.h>
#include <file_selector_linux/file_selector_plugin.h>
#include <sqlite3_flutter_libs/sqlite3_flutter_libs_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
Expand All @@ -17,6 +18,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) sqlite3_flutter_libs_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "Sqlite3FlutterLibsPlugin");
sqlite3_flutter_libs_plugin_register_with_registrar(sqlite3_flutter_libs_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
dynamic_color
file_selector_linux
sqlite3_flutter_libs
url_launcher_linux
)

Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import path_provider_foundation
import share_plus
import shared_preferences_foundation
import sqflite_darwin
import sqlite3_flutter_libs
import url_launcher_macos
import video_player_avfoundation
import webview_flutter_wkwebview
Expand All @@ -32,6 +33,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
Sqlite3FlutterLibsPlugin.register(with: registry.registrar(forPlugin: "Sqlite3FlutterLibsPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
FLTWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "FLTWebViewFlutterPlugin"))
Expand Down
26 changes: 25 additions & 1 deletion macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ PODS:
- sqflite_darwin (0.0.4):
- Flutter
- FlutterMacOS
- sqlite3 (3.47.0):
- sqlite3/common (= 3.47.0)
- sqlite3/common (3.47.0)
- sqlite3/dbstatvtab (3.47.0):
- sqlite3/common
- sqlite3/fts5 (3.47.0):
- sqlite3/common
- sqlite3/perf-threadsafe (3.47.0):
- sqlite3/common
- sqlite3/rtree (3.47.0):
- sqlite3/common
- sqlite3_flutter_libs (0.0.1):
- FlutterMacOS
- sqlite3 (~> 3.47.0)
- sqlite3/dbstatvtab
- sqlite3/fts5
- sqlite3/perf-threadsafe
- sqlite3/rtree
- url_launcher_macos (0.0.1):
- FlutterMacOS
- video_player_avfoundation (0.0.1):
Expand All @@ -51,13 +69,15 @@ DEPENDENCIES:
- share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`)
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite_darwin (from `Flutter/ephemeral/.symlinks/plugins/sqflite_darwin/darwin`)
- sqlite3_flutter_libs (from `Flutter/ephemeral/.symlinks/plugins/sqlite3_flutter_libs/macos`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
- video_player_avfoundation (from `Flutter/ephemeral/.symlinks/plugins/video_player_avfoundation/darwin`)
- webview_flutter_wkwebview (from `Flutter/ephemeral/.symlinks/plugins/webview_flutter_wkwebview/darwin`)

SPEC REPOS:
trunk:
- OrderedSet
- sqlite3

EXTERNAL SOURCES:
connectivity_plus:
Expand All @@ -84,6 +104,8 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin
sqflite_darwin:
:path: Flutter/ephemeral/.symlinks/plugins/sqflite_darwin/darwin
sqlite3_flutter_libs:
:path: Flutter/ephemeral/.symlinks/plugins/sqlite3_flutter_libs/macos
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
video_player_avfoundation:
Expand All @@ -105,10 +127,12 @@ SPEC CHECKSUMS:
share_plus: fd717ef89a2801d3491e737630112b80c310640e
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
sqflite_darwin: a553b1fd6fe66f53bbb0fe5b4f5bab93f08d7a13
sqlite3: 0aa20658a9b238a3b1ff7175eb7bdd863b0ab4fd
sqlite3_flutter_libs: f0b7a85544d8bac7b8bac12eac7d05bcfdd786d0
url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404
video_player_avfoundation: 7c6c11d8470e1675df7397027218274b6d2360b3
webview_flutter_wkwebview: 0982481e3d9c78fd5c6f62a002fcd24fc791f1e4

PODFILE CHECKSUM: c2e95c8c0fe03c5c57e438583cae4cc732296009

COCOAPODS: 1.15.2
COCOAPODS: 1.16.0
Loading

0 comments on commit f92770a

Please sign in to comment.