-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime): Persist database in local environment
When running locally, persist DBs to the filesystem so that data is present between restarts.
- Loading branch information
Showing
10 changed files
with
462 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
targets: | ||
$default: | ||
sources: | ||
include: | ||
- 'test/runtime/data/**' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tags: | ||
e2e: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
import 'dart:io'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:drift/drift.dart'; | ||
import 'package:drift/native.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
final Logger _logger = Logger('Celest.Data'); | ||
|
||
/// A [QueryExecutor] for an in-memory database. | ||
Future<QueryExecutor> inMemoryExecutor() async => NativeDatabase.memory(); | ||
|
||
/// A [QueryExecutor] with local persistence. | ||
Future<QueryExecutor> localExecutor({ | ||
required String name, | ||
String? path, | ||
}) async { | ||
if (path == null) { | ||
final packageConfig = await Isolate.packageConfig; | ||
if (packageConfig == null) { | ||
_logger.warning( | ||
'Failed to determine package config path. ' | ||
'Falling back to in-memory database.', | ||
); | ||
return inMemoryExecutor(); | ||
} | ||
path = p.join( | ||
p.dirname(p.fromUri(packageConfig)), | ||
'celest', | ||
name, | ||
); | ||
} | ||
_logger.info('Opening database at $path'); | ||
final databaseFile = File(path); | ||
if (!databaseFile.existsSync()) { | ||
_logger.info('Database does not exist. Creating new database.'); | ||
} | ||
return NativeDatabase( | ||
databaseFile, | ||
cachePreparedStatements: true, | ||
enableMigrations: true, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:drift/drift.dart'; | ||
|
||
part 'test_database.g.dart'; | ||
|
||
class Items extends Table { | ||
IntColumn get id => integer().autoIncrement()(); | ||
TextColumn get name => text().nullable()(); | ||
} | ||
|
||
@DriftDatabase(tables: [Items]) | ||
class TestDatabase extends _$TestDatabase { | ||
TestDatabase(super.e); | ||
|
||
@override | ||
int get schemaVersion => 1; | ||
} |
Oops, something went wrong.