mirror of https://github.com/AMT-Cheif/drift.git
Deprecate special connect constructor
This commit is contained in:
parent
83fad8426e
commit
0d354b3bec
|
@ -50,7 +50,10 @@ targets:
|
|||
drift_dev:analyzer:
|
||||
enabled: true
|
||||
options: &options
|
||||
generate_connect_constructor: true
|
||||
sql:
|
||||
dialect: sqlite
|
||||
options:
|
||||
version: "3.39"
|
||||
generate_for:
|
||||
include: &modular
|
||||
- "lib/snippets/modular/**"
|
||||
|
|
|
@ -17,10 +17,7 @@ part 'isolates.g.dart';
|
|||
@DriftDatabase(/*...*/)
|
||||
class TodoDb extends _$TodoDb {
|
||||
// Your existing constructor, whatever it may be...
|
||||
TodoDb() : super(NativeDatabase.memory());
|
||||
|
||||
// this is the new constructor
|
||||
TodoDb.connect(DatabaseConnection connection) : super.connect(connection);
|
||||
TodoDb(QueryExecutor executor) : super(executor);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
@ -28,23 +25,16 @@ class TodoDb extends _$TodoDb {
|
|||
// #enddocregion database
|
||||
|
||||
// #docregion isolate
|
||||
|
||||
// This needs to be a top-level method because it's run on a background isolate
|
||||
DatabaseConnection _backgroundConnection() {
|
||||
// Construct the database to use. This example uses a non-persistent in-memory
|
||||
// database each time. You can use your existing NativeDatabase with a file as
|
||||
// well, or a `LazyDatabase` if you need to construct it asynchronously. When
|
||||
// using a Flutter plugin like `path_provider` to determine the path, also see
|
||||
// the "Initialization on the main thread" section below!
|
||||
final database = NativeDatabase.memory();
|
||||
return DatabaseConnection(database);
|
||||
}
|
||||
|
||||
void main() async {
|
||||
// create a drift executor in a new background isolate. If you want to start
|
||||
// the isolate yourself, you can also call DriftIsolate.inCurrent() from the
|
||||
// background isolate
|
||||
final isolate = await DriftIsolate.spawn(_backgroundConnection);
|
||||
final isolate = await DriftIsolate.spawn(() {
|
||||
// This callback needs to return the database connection used by the drift
|
||||
// isolate. This example uses a non-persistent in-memory database, but you
|
||||
// can also use your existing NativeDatabase with a file as well.
|
||||
return DatabaseConnection(NativeDatabase.memory());
|
||||
});
|
||||
|
||||
// we can now create a database connection that will use the isolate
|
||||
// internally. This is NOT what we returned from _backgroundConnection, drift
|
||||
|
@ -53,7 +43,7 @@ void main() async {
|
|||
// use `singleClientMode` to dispose the isolate after closing the connection.
|
||||
final connection = await isolate.connect(singleClientMode: true);
|
||||
|
||||
final db = TodoDb.connect(connection);
|
||||
final db = TodoDb(connection);
|
||||
|
||||
// you can now use your database exactly like you regularly would, it
|
||||
// transparently uses a background isolate internally
|
||||
|
@ -66,9 +56,11 @@ void main() async {
|
|||
|
||||
void connectSynchronously() {
|
||||
// #docregion delayed
|
||||
TodoDb.connect(
|
||||
TodoDb(
|
||||
DatabaseConnection.delayed(Future.sync(() async {
|
||||
final isolate = await DriftIsolate.spawn(_backgroundConnection);
|
||||
final isolate = await DriftIsolate.spawn(() {
|
||||
return DatabaseConnection(NativeDatabase.memory());
|
||||
});
|
||||
return isolate.connect(singleClientMode: true);
|
||||
})),
|
||||
);
|
||||
|
|
|
@ -53,10 +53,9 @@ At the moment, drift supports these options:
|
|||
(so a column named `user_name` would also use `user_name` as a json key instead of `userName`).
|
||||
You can always override the json key by using a `JSON KEY` column constraint
|
||||
(e.g. `user_name VARCHAR NOT NULL JSON KEY userName`).
|
||||
* `generate_connect_constructor`: Generates a named `connect()` constructor on database classes
|
||||
that takes a `DatabaseConnection` instead of a `QueryExecutor` - this allows sharing stream queries
|
||||
between two drift database instances, which can be helpful for some [isolate setups]({{ "isolates.md" | pageUrl }}).
|
||||
The option is enabled by default.
|
||||
* `generate_connect_constructor` (deprecated): Generates a named `connect()` constructor on database classes
|
||||
that takes a `DatabaseConnection` instead of a `QueryExecutor`.
|
||||
This option was deprecated in drift 2.5 because `DatabaseConnection` now implements `QueryExecutor`.
|
||||
* `data_class_to_companions` (defaults to `true`): Controls whether drift will write the `toCompanion` method in generated
|
||||
data classes.
|
||||
* `mutable_classes` (defaults to `false`): The fields generated in generated data, companion and result set classes are final
|
||||
|
|
|
@ -58,30 +58,16 @@ In most other cases, simply using `NativeDatabase.createInbackground` works
|
|||
great! It implements the same approach shared in this article, except that all
|
||||
the complicated bits are hidden behind a simple method.
|
||||
|
||||
## Preparations
|
||||
## Using drift in a background isolate {#using-moor-in-a-background-isolate}
|
||||
|
||||
When a drift database is opened multiple times, the two instances need to
|
||||
synchronize query streams, so that updates made in one instance are reflected
|
||||
in queries watched by the other.
|
||||
|
||||
By default, drift databases are created from a `QueryExecutor` - an interface
|
||||
responsible for running the SQL statements generated by higher-level drift APIs.
|
||||
To share both the underlying database and a drift-specific mechanism for
|
||||
streams, you can instead use a `DatabaseConnection` instance. For that, you need
|
||||
to use the generated `connect()` constructor in your database class:
|
||||
The rest of this article assumes that your database class can be constructed
|
||||
with a `QueryExecutor`, e.g. because it defines a constructor like this:
|
||||
|
||||
{% include "blocks/snippet" snippets = snippets name = 'database' %}
|
||||
|
||||
Having a second constructor taking a `DatabaseConnection` is necessary for
|
||||
backwards compatibility since the default constructor only takes a
|
||||
`QueryExecutor` which is insufficient to synchronize streams.
|
||||
If you only want to open your database with a `DatabaseConnection`, you can
|
||||
remove the default constructor though.
|
||||
|
||||
After adding the new constructor, you can create instances of your database
|
||||
that will transparently run queries on a background isolate:
|
||||
|
||||
## Using drift in a background isolate {#using-moor-in-a-background-isolate}
|
||||
Of course, you can also move the approaches described here into the `super()`
|
||||
constructor invocation if you want to have a zero-argument constructor that
|
||||
starts an isolate by default.
|
||||
|
||||
With the database class ready, let's open it on a background isolate
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ class Users extends Table {
|
|||
|
||||
@DriftDatabase(tables: [Users])
|
||||
class Database extends _$Database {
|
||||
Database.connect(DatabaseConnection c) : super.connect(c);
|
||||
Database.connect(DatabaseConnection c) : super(c);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
- Add `isExp`, `isValue`, `isNotExp` and `isNotValue` methods to `Expression`
|
||||
to generate the `IS` operator in SQL.
|
||||
- Add `all()` extension on tables and views to quickly query all rows.
|
||||
- The `DatabaseConnection` class now implements `QueryExecutor`, meaning that
|
||||
you no longer need a special `.connect()` constructor to use it.
|
||||
|
||||
## 2.4.2
|
||||
|
||||
|
|
|
@ -683,7 +683,6 @@ class $TodoItemWithCategoryNameViewView extends ViewInfo<
|
|||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(e);
|
||||
_$Database.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $TodoCategoriesTable todoCategories = $TodoCategoriesTable(this);
|
||||
late final $TodoItemsTable todoItems = $TodoItemsTable(this);
|
||||
late final $TodoCategoryItemCountView todoCategoryItemCount =
|
||||
|
|
|
@ -73,8 +73,7 @@ class NativeDatabase extends DelegatedDatabase {
|
|||
static QueryExecutor createInBackground(File file,
|
||||
{bool logStatements = false, DatabaseSetup? setup}) {
|
||||
return createBackgroundConnection(file,
|
||||
logStatements: logStatements, setup: setup)
|
||||
.executor;
|
||||
logStatements: logStatements, setup: setup);
|
||||
}
|
||||
|
||||
/// Like [createInBackground], except that it returns the whole
|
||||
|
|
|
@ -5,7 +5,7 @@ part of 'runtime_api.dart';
|
|||
/// - a [QueryExecutor], which runs sql statements.
|
||||
/// - a [StreamQueryStore], which dispatches table changes to listening queries,
|
||||
/// on which the auto-updating queries are based.
|
||||
class DatabaseConnection {
|
||||
class DatabaseConnection implements QueryExecutor {
|
||||
/// The executor to use when queries are executed.
|
||||
final QueryExecutor executor;
|
||||
|
||||
|
@ -63,4 +63,41 @@ class DatabaseConnection {
|
|||
DatabaseConnection withExecutor(QueryExecutor executor) {
|
||||
return DatabaseConnection(executor, streamQueries: streamQueries);
|
||||
}
|
||||
|
||||
@override
|
||||
TransactionExecutor beginTransaction() => executor.beginTransaction();
|
||||
|
||||
@override
|
||||
Future<void> close() => executor.close();
|
||||
|
||||
@override
|
||||
SqlDialect get dialect => executor.dialect;
|
||||
|
||||
@override
|
||||
Future<bool> ensureOpen(QueryExecutorUser user) => executor.ensureOpen(user);
|
||||
|
||||
@override
|
||||
Future<void> runBatched(BatchedStatements statements) =>
|
||||
executor.runBatched(statements);
|
||||
|
||||
@override
|
||||
Future<void> runCustom(String statement, [List<Object?>? args]) =>
|
||||
executor.runCustom(statement, args);
|
||||
|
||||
@override
|
||||
Future<int> runDelete(String statement, List<Object?> args) =>
|
||||
executor.runDelete(statement, args);
|
||||
|
||||
@override
|
||||
Future<int> runInsert(String statement, List<Object?> args) =>
|
||||
executor.runInsert(statement, args);
|
||||
|
||||
@override
|
||||
Future<List<Map<String, Object?>>> runSelect(
|
||||
String statement, List<Object?> args) =>
|
||||
executor.runSelect(statement, args);
|
||||
|
||||
@override
|
||||
Future<int> runUpdate(String statement, List<Object?> args) =>
|
||||
executor.runUpdate(statement, args);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,9 @@ abstract class DatabaseConnectionUser {
|
|||
/// streams, wrap the underlying executor and perform type mapping.
|
||||
DatabaseConnectionUser(QueryExecutor executor,
|
||||
{StreamQueryStore? streamQueries})
|
||||
: connection = DatabaseConnection(executor, streamQueries: streamQueries);
|
||||
: connection = executor is DatabaseConnection
|
||||
? executor
|
||||
: DatabaseConnection(executor, streamQueries: streamQueries);
|
||||
|
||||
/// Creates another [DatabaseConnectionUser] by referencing the implementation
|
||||
/// from the [other] user.
|
||||
|
|
|
@ -14,7 +14,7 @@ void main() {
|
|||
executor = MockExecutor();
|
||||
streamQueries = MockStreamQueries();
|
||||
|
||||
db = TodoDb.connect(createConnection(executor, streamQueries));
|
||||
db = TodoDb(createConnection(executor, streamQueries));
|
||||
});
|
||||
|
||||
test('runs generated statements', () async {
|
||||
|
|
|
@ -6,12 +6,12 @@ import '../../test_utils/test_utils.dart';
|
|||
|
||||
void main() {
|
||||
group('with default options', () {
|
||||
_testDateTimes(() => TodoDb.connect(testInMemoryDatabase()));
|
||||
_testDateTimes(() => TodoDb(testInMemoryDatabase()));
|
||||
});
|
||||
|
||||
group('storing date times as text', () {
|
||||
_testDateTimes(
|
||||
() => TodoDb.connect(testInMemoryDatabase())
|
||||
() => TodoDb(testInMemoryDatabase())
|
||||
..options = const DriftDatabaseOptions(storeDateTimeAsText: true),
|
||||
dateTimeAsText: true,
|
||||
);
|
||||
|
@ -21,7 +21,7 @@ void main() {
|
|||
late TodoDb db;
|
||||
|
||||
setUp(() async {
|
||||
db = TodoDb.connect(testInMemoryDatabase());
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
|
||||
// we selectOnly from users for the lack of a better option. Insert one
|
||||
// row so that getSingle works
|
||||
|
|
|
@ -17,7 +17,7 @@ void main() {
|
|||
streamQueries = MockStreamQueries();
|
||||
|
||||
final connection = createConnection(executor, streamQueries);
|
||||
db = TodoDb.connect(connection);
|
||||
db = TodoDb(connection);
|
||||
});
|
||||
|
||||
group('compiled custom queries', () {
|
||||
|
|
|
@ -17,7 +17,7 @@ void main() {
|
|||
streamQueries = MockStreamQueries();
|
||||
|
||||
final connection = createConnection(executor, streamQueries);
|
||||
db = TodoDb.connect(connection);
|
||||
db = TodoDb(connection);
|
||||
});
|
||||
|
||||
group('Generates DELETE statements', () {
|
||||
|
|
|
@ -15,7 +15,7 @@ void main() {
|
|||
streamQueries = MockStreamQueries();
|
||||
|
||||
final connection = createConnection(executor, streamQueries);
|
||||
db = TodoDb.connect(connection);
|
||||
db = TodoDb(connection);
|
||||
});
|
||||
|
||||
test('generates insert statements', () async {
|
||||
|
|
|
@ -17,7 +17,7 @@ void main() {
|
|||
streamQueries = MockStreamQueries();
|
||||
|
||||
final connection = createConnection(executor, streamQueries);
|
||||
db = TodoDb.connect(connection);
|
||||
db = TodoDb(connection);
|
||||
});
|
||||
|
||||
group('generates update statements', () {
|
||||
|
|
|
@ -18,7 +18,7 @@ void main() {
|
|||
streamQueries = MockStreamQueries();
|
||||
|
||||
final connection = createConnection(executor, streamQueries);
|
||||
db = TodoDb.connect(connection);
|
||||
db = TodoDb(connection);
|
||||
});
|
||||
|
||||
test('streams in transactions are isolated and scoped', () async {
|
||||
|
|
|
@ -39,7 +39,7 @@ void main() {
|
|||
write: background.executor,
|
||||
));
|
||||
|
||||
final db = TodoDb.connect(foreground);
|
||||
final db = TodoDb(foreground);
|
||||
|
||||
await db
|
||||
.into(db.categories)
|
||||
|
|
|
@ -6,7 +6,7 @@ import '../test_utils/test_utils.dart';
|
|||
|
||||
void main() {
|
||||
test('fts5 integration test', () async {
|
||||
final db = CustomTablesDb.connect(testInMemoryDatabase());
|
||||
final db = CustomTablesDb(testInMemoryDatabase());
|
||||
|
||||
await db.into(db.email).insert(EmailCompanion.insert(
|
||||
sender: 'foo@example.org', title: 'Hello world', body: 'Test email'));
|
||||
|
|
|
@ -10,7 +10,7 @@ import '../test_utils/test_utils.dart';
|
|||
|
||||
void main() {
|
||||
test('json1 integration test', () async {
|
||||
final db = TodoDb.connect(testInMemoryDatabase());
|
||||
final db = TodoDb(testInMemoryDatabase());
|
||||
const jsonObject = {
|
||||
'foo': 'bar',
|
||||
'array': [
|
||||
|
|
|
@ -32,7 +32,7 @@ void main() {
|
|||
});
|
||||
|
||||
test('containsCase integration test', () async {
|
||||
final db = TodoDb.connect(testInMemoryDatabase());
|
||||
final db = TodoDb(testInMemoryDatabase());
|
||||
// insert exactly one row so that we can evaluate expressions from Dart
|
||||
await db.into(db.pureDefaults).insert(PureDefaultsCompanion.insert());
|
||||
|
||||
|
@ -59,7 +59,7 @@ void main() {
|
|||
late TodoDb db;
|
||||
|
||||
setUp(() async {
|
||||
db = TodoDb.connect(testInMemoryDatabase());
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
// insert exactly one row so that we can evaluate expressions from Dart
|
||||
await db.into(db.pureDefaults).insert(PureDefaultsCompanion.insert());
|
||||
});
|
||||
|
|
|
@ -19,11 +19,6 @@ class CustomTablesDb extends _$CustomTablesDb {
|
|||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
}
|
||||
|
||||
CustomTablesDb.connect(DatabaseConnection connection)
|
||||
: super.connect(connection) {
|
||||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
}
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
|
|
|
@ -1589,7 +1589,6 @@ class MyView extends ViewInfo<MyView, MyViewData> implements HasResultSet {
|
|||
|
||||
abstract class _$CustomTablesDb extends GeneratedDatabase {
|
||||
_$CustomTablesDb(QueryExecutor e) : super(e);
|
||||
_$CustomTablesDb.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final NoIds noIds = NoIds(this);
|
||||
late final WithDefaults withDefaults = WithDefaults(this);
|
||||
late final WithConstraints withConstraints = WithConstraints(this);
|
||||
|
|
|
@ -209,10 +209,6 @@ class TodoDb extends _$TodoDb {
|
|||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
}
|
||||
|
||||
TodoDb.connect(DatabaseConnection connection) : super.connect(connection) {
|
||||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
}
|
||||
|
||||
@override
|
||||
MigrationStrategy migration = MigrationStrategy();
|
||||
|
||||
|
|
|
@ -1663,7 +1663,6 @@ class $TodoWithCategoryViewView
|
|||
|
||||
abstract class _$TodoDb extends GeneratedDatabase {
|
||||
_$TodoDb(QueryExecutor e) : super(e);
|
||||
_$TodoDb.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $CategoriesTable categories = $CategoriesTable(this);
|
||||
late final $TodosTableTable todosTable = $TodosTableTable(this);
|
||||
late final $UsersTable users = $UsersTable(this);
|
||||
|
|
|
@ -42,7 +42,7 @@ void main() {
|
|||
final isolate = await DriftIsolate.spawn(createConnection);
|
||||
addTearDown(isolate.shutdownAll);
|
||||
|
||||
final db = EmptyDb.connect(await isolate.connect());
|
||||
final db = EmptyDb(await isolate.connect());
|
||||
await runTest(db);
|
||||
});
|
||||
}, skip: 'todo: Cancellations are currently broken on Dart 2.15');
|
||||
|
|
|
@ -35,7 +35,7 @@ DatabaseConnection createConnection() {
|
|||
}
|
||||
|
||||
class EmptyDb extends GeneratedDatabase {
|
||||
EmptyDb.connect(DatabaseConnection c) : super.connect(c);
|
||||
EmptyDb(DatabaseConnection c) : super(c);
|
||||
@override
|
||||
final List<TableInfo> allTables = const [];
|
||||
@override
|
||||
|
|
|
@ -11,7 +11,7 @@ void main() {
|
|||
late CustomTablesDb db;
|
||||
|
||||
setUp(() {
|
||||
db = CustomTablesDb.connect(testInMemoryDatabase());
|
||||
db = CustomTablesDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
|
|
@ -9,7 +9,7 @@ void main() {
|
|||
late TodoDb db;
|
||||
|
||||
setUp(() {
|
||||
db = TodoDb.connect(testInMemoryDatabase());
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
|
|
@ -8,7 +8,7 @@ void main() {
|
|||
late CustomTablesDb db;
|
||||
|
||||
setUp(() {
|
||||
db = CustomTablesDb.connect(testInMemoryDatabase());
|
||||
db = CustomTablesDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
|
|
@ -7,7 +7,7 @@ import '../test_utils/test_utils.dart';
|
|||
void main() {
|
||||
test('regression test for #1232', () async {
|
||||
// replace with generated table
|
||||
final db = TodoDb.connect(testInMemoryDatabase());
|
||||
final db = TodoDb(testInMemoryDatabase());
|
||||
final someTables = {db.todosTable};
|
||||
|
||||
await db.customStatement('create table tbl (x int)');
|
||||
|
|
|
@ -6,7 +6,7 @@ import 'package:test/test.dart';
|
|||
import '../test_utils/test_utils.dart';
|
||||
|
||||
class _TestDb extends GeneratedDatabase {
|
||||
_TestDb() : super.connect(testInMemoryDatabase());
|
||||
_TestDb() : super(testInMemoryDatabase());
|
||||
@override
|
||||
final List<TableInfo> allTables = const [];
|
||||
@override
|
||||
|
|
|
@ -7,7 +7,7 @@ import '../test_utils/test_utils.dart';
|
|||
|
||||
void main() {
|
||||
test('Dart queries on views update correctly', () async {
|
||||
final db = CustomTablesDb.connect(testInMemoryDatabase());
|
||||
final db = CustomTablesDb(testInMemoryDatabase());
|
||||
addTearDown(db.close);
|
||||
|
||||
expect(
|
||||
|
|
|
@ -6,7 +6,7 @@ import '../test_utils/test_utils.dart';
|
|||
|
||||
void main() {
|
||||
test('exists subqueries properly reference columns', () async {
|
||||
final db = TodoDb.connect(testInMemoryDatabase());
|
||||
final db = TodoDb(testInMemoryDatabase());
|
||||
addTearDown(db.close);
|
||||
|
||||
final nonEmptyId = await db.categories
|
||||
|
|
|
@ -18,7 +18,7 @@ Future<int?> _getCategoryIdByDescription(
|
|||
|
||||
void main() {
|
||||
test('type inference for nullable call in async function', () async {
|
||||
final db = TodoDb.connect(testInMemoryDatabase());
|
||||
final db = TodoDb(testInMemoryDatabase());
|
||||
addTearDown(db.close);
|
||||
|
||||
final categoryDescription = 'category description';
|
||||
|
|
|
@ -35,7 +35,7 @@ void _defineTest(
|
|||
final isolate = useIsolate ? await _spawnIsolate() : null;
|
||||
|
||||
final db = useIsolate
|
||||
? _SomeDb.connect(await isolate!.connect())
|
||||
? _SomeDb(await isolate!.connect())
|
||||
: _SomeDb(NativeDatabase.memory());
|
||||
|
||||
addTearDown(() async {
|
||||
|
@ -92,8 +92,6 @@ class _SomeTable extends Table {
|
|||
class _SomeDb extends _$_SomeDb {
|
||||
_SomeDb(super.executor);
|
||||
|
||||
_SomeDb.connect(DatabaseConnection connection) : super.connect(connection);
|
||||
|
||||
@override
|
||||
final schemaVersion = 1;
|
||||
}
|
||||
|
|
|
@ -183,7 +183,6 @@ class _SomeTableCompanion extends UpdateCompanion<_SomeTableData> {
|
|||
|
||||
abstract class _$_SomeDb extends GeneratedDatabase {
|
||||
_$_SomeDb(QueryExecutor e) : super(e);
|
||||
_$_SomeDb.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $_SomeTableTable someTable = $_SomeTableTable(this);
|
||||
@override
|
||||
Iterable<TableInfo<Table, Object?>> get allTables =>
|
||||
|
|
|
@ -19,7 +19,7 @@ void main() {
|
|||
final isolate = await DriftIsolate.spawn(createConnection);
|
||||
addTearDown(isolate.shutdownAll);
|
||||
|
||||
final db = EmptyDb.connect(await isolate.connect());
|
||||
final db = EmptyDb(await isolate.connect());
|
||||
await db.customSelect('select 1').getSingle();
|
||||
|
||||
final filter = BehaviorSubject<int>();
|
||||
|
|
|
@ -9,7 +9,7 @@ void main() {
|
|||
late TodoDb db;
|
||||
|
||||
setUp(() {
|
||||
db = TodoDb.connect(testInMemoryDatabase());
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
|
|
@ -76,7 +76,7 @@ void main() {
|
|||
final writer = await Isolate.spawn(_writeTodoEntryInBackground,
|
||||
_BackgroundEntryMessage(driftIsolate, receiveDone.sendPort));
|
||||
|
||||
final db = TodoDb.connect(await driftIsolate.connect());
|
||||
final db = TodoDb(await driftIsolate.connect());
|
||||
final expectedEntry = const TypeMatcher<TodoEntry>()
|
||||
.having((e) => e.content, 'content', 'Hello from background');
|
||||
|
||||
|
@ -98,7 +98,7 @@ void main() {
|
|||
|
||||
test('errors propagate across isolates', () async {
|
||||
final isolate = await DriftIsolate.spawn(_backgroundConnection);
|
||||
final db = TodoDb.connect(await isolate.connect());
|
||||
final db = TodoDb(await isolate.connect());
|
||||
|
||||
try {
|
||||
await db.customStatement('UPDATE non_existing_table SET foo = bar');
|
||||
|
@ -145,7 +145,7 @@ void main() {
|
|||
expect(done.first, completion(anything));
|
||||
|
||||
final drift = await spawned.first as DriftIsolate;
|
||||
final db = TodoDb.connect(await drift.connect(singleClientMode: true));
|
||||
final db = TodoDb(await drift.connect(singleClientMode: true));
|
||||
await db.close();
|
||||
}, tags: 'background_isolate');
|
||||
|
||||
|
@ -167,7 +167,7 @@ void _runTests(FutureOr<DriftIsolate> Function() spawner, bool terminateIsolate,
|
|||
setUp(() async {
|
||||
isolate = await spawner();
|
||||
|
||||
database = TodoDb.connect(
|
||||
database = TodoDb(
|
||||
DatabaseConnection.delayed(isolate.connect()),
|
||||
);
|
||||
});
|
||||
|
@ -355,7 +355,7 @@ DatabaseConnection _backgroundConnection() {
|
|||
|
||||
Future<void> _writeTodoEntryInBackground(_BackgroundEntryMessage msg) async {
|
||||
final connection = await msg.isolate.connect();
|
||||
final database = TodoDb.connect(connection);
|
||||
final database = TodoDb(connection);
|
||||
|
||||
await database
|
||||
.into(database.todosTable)
|
||||
|
|
|
@ -27,8 +27,7 @@ void main() {
|
|||
test('work with connections', () async {
|
||||
final file = File(d.path('test.db'));
|
||||
|
||||
final db =
|
||||
TodoDb.connect(NativeDatabase.createBackgroundConnection(file));
|
||||
final db = TodoDb(NativeDatabase.createBackgroundConnection(file));
|
||||
await db.todosTable.select().get(); // Open the database
|
||||
await db.close();
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ void main() {
|
|||
final client = await connectToRemoteAndInitialize(
|
||||
controller.local.expectedToClose,
|
||||
singleClientMode: true);
|
||||
final db = TodoDb.connect(client);
|
||||
final db = TodoDb(client);
|
||||
|
||||
await db.todosTable.select().get();
|
||||
await db.close();
|
||||
|
@ -61,7 +61,7 @@ void main() {
|
|||
singleClientMode: true,
|
||||
);
|
||||
|
||||
final db = TodoDb.connect(client);
|
||||
final db = TodoDb(client);
|
||||
await db.todosTable.select().get();
|
||||
await db.close();
|
||||
},
|
||||
|
@ -125,7 +125,7 @@ void main() {
|
|||
.changeStream(_checkStreamOfSimple)
|
||||
.expectedToClose,
|
||||
serialize: true);
|
||||
final db = TodoDb.connect(connection);
|
||||
final db = TodoDb(connection);
|
||||
|
||||
await db.customSelect('SELECT ?, ?, ?, ?', variables: [
|
||||
Variable.withBigInt(BigInt.one),
|
||||
|
@ -165,8 +165,7 @@ void main() {
|
|||
server.serve(controller.foreign);
|
||||
addTearDown(server.shutdown);
|
||||
|
||||
final db =
|
||||
TodoDb.connect(await connectToRemoteAndInitialize(controller.local));
|
||||
final db = TodoDb(await connectToRemoteAndInitialize(controller.local));
|
||||
addTearDown(db.close);
|
||||
|
||||
await db.transaction(() async {
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
- Support `MAPPED BY` for individual columns in queries or in views defined with SQL.
|
||||
- Consistently interpret `CAST (x AS DATETIME)` and `CAST(x AS TEXT)` in drift files.
|
||||
- Support a `CAST` to an enum type in drift types.
|
||||
- The `generate_connect_constructor` option is now enabled by default.
|
||||
- The `generate_connect_constructor` option is now deprecated, as a `DatabaseConnection`
|
||||
can be passed whereever a `QueryExecutor` is used too.
|
||||
- Support two different queries using `LIST()` columns having the same result class name.
|
||||
- Fix table classes not extending defining Dart classes with modular generation.
|
||||
- Fix `@UseDataClass` with `extending` not working with modular generation.
|
||||
|
|
|
@ -50,7 +50,9 @@ class DriftOptions {
|
|||
/// This makes drift generate a constructor for database classes that takes a
|
||||
/// `DatabaseConnection` instead of just a `QueryExecutor` - meaning that
|
||||
/// stream queries can also be shared across multiple database instances.
|
||||
@JsonKey(name: 'generate_connect_constructor', defaultValue: true)
|
||||
/// Starting from drift 2.5, the database connection class implements the
|
||||
/// `QueryExecutor` interface, making this option unecessary.
|
||||
@JsonKey(name: 'generate_connect_constructor', defaultValue: false)
|
||||
final bool generateConnectConstructor;
|
||||
|
||||
@JsonKey(name: 'sqlite_modules', defaultValue: [])
|
||||
|
@ -107,7 +109,7 @@ class DriftOptions {
|
|||
this.skipVerificationCode = false,
|
||||
this.useDataClassNameForCompanions = false,
|
||||
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = true,
|
||||
this.generateConnectConstructor = true,
|
||||
this.generateConnectConstructor = false,
|
||||
this.dataClassToCompanions = true,
|
||||
this.generateMutableClasses = false,
|
||||
this.rawResultSetData = false,
|
||||
|
@ -182,9 +184,6 @@ class DriftOptions {
|
|||
/// Whether the [module] has been enabled in this configuration.
|
||||
bool hasModule(SqlModule module) => effectiveModules.contains(module);
|
||||
|
||||
/// Checks whether a deprecated option is enabled.
|
||||
bool get enabledDeprecatedOption => false;
|
||||
|
||||
SqlDialect get effectiveDialect => dialect?.dialect ?? SqlDialect.sqlite;
|
||||
|
||||
/// The assumed sqlite version used when analyzing queries.
|
||||
|
|
|
@ -174,11 +174,16 @@ class _DriftBuildRun {
|
|||
/// are applied to this builder.
|
||||
Future<void> _warnAboutDeprecatedOptions() async {
|
||||
final flags = await buildStep.fetchResource(_flags);
|
||||
if (!flags.didWarnAboutDeprecatedOptions &&
|
||||
options.enabledDeprecatedOption) {
|
||||
print('You have the eagerly_load_dart_ast option enabled. The option is '
|
||||
'no longer necessary and will be removed in a future drift version. '
|
||||
'Consider removing the option from your build.yaml.');
|
||||
if (!flags.didWarnAboutDeprecatedOptions) {
|
||||
if (options.generateConnectConstructor) {
|
||||
log.warning(
|
||||
'You enabled the `generate_connect_constructor` build option. This '
|
||||
'option is no longer necessary in drift 2.5, as a '
|
||||
'`DatabaseConnection` can now be passed to the default constructor '
|
||||
'for generated databases. Consider removing this option.',
|
||||
);
|
||||
}
|
||||
|
||||
flags.didWarnAboutDeprecatedOptions = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ DriftOptions _$DriftOptionsFromJson(Map json) => $checkedCreate(
|
|||
'use_column_name_as_json_key_when_defined_in_moor_file',
|
||||
(v) => v as bool? ?? true),
|
||||
generateConnectConstructor: $checkedConvert(
|
||||
'generate_connect_constructor', (v) => v as bool? ?? true),
|
||||
'generate_connect_constructor', (v) => v as bool? ?? false),
|
||||
dataClassToCompanions: $checkedConvert(
|
||||
'data_class_to_companions', (v) => v as bool? ?? true),
|
||||
generateMutableClasses:
|
||||
|
|
|
@ -10,10 +10,9 @@ part 'database.g.dart';
|
|||
|
||||
@DriftDatabase(tables: [TodoEntries, Categories], include: {'sql.drift'})
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase() : super.connect(impl.connect());
|
||||
AppDatabase() : super(impl.connect());
|
||||
|
||||
AppDatabase.forTesting(DatabaseConnection connection)
|
||||
: super.connect(connection);
|
||||
AppDatabase.forTesting(DatabaseConnection connection) : super(connection);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 3;
|
||||
|
|
|
@ -620,7 +620,6 @@ class TextEntriesCompanion extends UpdateCompanion<TextEntrie> {
|
|||
|
||||
abstract class _$AppDatabase extends GeneratedDatabase {
|
||||
_$AppDatabase(QueryExecutor e) : super(e);
|
||||
_$AppDatabase.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $CategoriesTable categories = $CategoriesTable(this);
|
||||
late final $TodoEntriesTable todoEntries = $TodoEntriesTable(this);
|
||||
late final TextEntries textEntries = TextEntries(this);
|
||||
|
|
|
@ -14,7 +14,7 @@ class Database extends _$Database {
|
|||
@override
|
||||
int get schemaVersion => latestSchemaVersion;
|
||||
|
||||
Database(DatabaseConnection connection) : super.connect(connection);
|
||||
Database(DatabaseConnection connection) : super(connection);
|
||||
|
||||
@override
|
||||
MigrationStrategy get migration {
|
||||
|
|
|
@ -868,7 +868,6 @@ class GroupCount extends ViewInfo<GroupCount, GroupCountData>
|
|||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(e);
|
||||
_$Database.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $UsersTable users = $UsersTable(this);
|
||||
late final Groups groups = Groups(this);
|
||||
late final Notes notes = Notes(this);
|
||||
|
|
|
@ -4,7 +4,7 @@ part 'database.g.dart';
|
|||
|
||||
@DriftDatabase(include: {'src/tables.drift'})
|
||||
class MyDatabase extends _$MyDatabase {
|
||||
MyDatabase(DatabaseConnection conn) : super.connect(conn);
|
||||
MyDatabase(DatabaseConnection conn) : super(conn);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
|
|
@ -174,7 +174,6 @@ class EntriesCompanion extends UpdateCompanion<Entrie> {
|
|||
|
||||
abstract class _$MyDatabase extends GeneratedDatabase {
|
||||
_$MyDatabase(QueryExecutor e) : super(e);
|
||||
_$MyDatabase.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final Entries entries = Entries(this);
|
||||
Selectable<Entrie> allEntries() {
|
||||
return customSelect('SELECT * FROM entries', variables: [], readsFrom: {
|
||||
|
|
|
@ -88,7 +88,7 @@ class Database extends _$Database {
|
|||
@override
|
||||
final int schemaVersion;
|
||||
|
||||
Database(DatabaseConnection e, {this.schemaVersion = 2}) : super.connect(e);
|
||||
Database(DatabaseConnection e, {this.schemaVersion = 2}) : super(e);
|
||||
|
||||
Database.executor(QueryExecutor db) : this(DatabaseConnection(db));
|
||||
|
||||
|
|
|
@ -539,7 +539,6 @@ class FriendshipsCompanion extends UpdateCompanion<Friendship> {
|
|||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(e);
|
||||
_$Database.connect(DatabaseConnection c) : super.connect(c);
|
||||
late final $UsersTable users = $UsersTable(this);
|
||||
late final $FriendshipsTable friendships = $FriendshipsTable(this);
|
||||
Selectable<User> mostPopularUsers(int amount) {
|
||||
|
|
Loading…
Reference in New Issue