newConnection method

DatabaseConnection newConnection()

Creates a new database connection.

All connections returned by this method point to the rawDatabase. However, each call to newConnection returns an independent connection that is considered closed from drift's point of view. This means that the rawDatabase can be used by multiple generated database classes that can independently be opened and closed, albeit not simultaneously.

Example

When generating the schema helpers with the --data-classes and the --companions command-line flags, this method can be used to create drift databases inserting data at specific versions:

import 'generated/schema.dart';
import 'generated/schema_v1.dart' as v1;
import 'generated/schema_v2.dart' as v2;

test('data integrity from v1 to v2', () async {
  final verifier = SchemaVerifier(GeneratedHelper());
  final schema = await verifier.schemaAt(1);

  // Insert some data from the view of the old database on an independent
  // connection!
  final oldDb = v1.DatabaseAtV1(schema.newConnection());
  await oldDb.into(oldDb.users).insert(v1.UsersCompanion(id: Value(1)));
  await oldDb.close();

  // Run the migration on the real database class from your app
  final dbForMigration = Database(schema.newConnection());
  await verifier.migrateAndValidate(dbForMigration, 2);
  await dbForMigration.close();

  // Make sure the user is still here with a new database at v2
  final checkDb = v2.DatabaseAtV2(schema.newConnection());
  final user = await checkDb.select(checkDb.users).getSingle();
  expect(user.id, 1);
  expect(user.name, 'default name from migration');
  await checkDb.close();
});

Implementation

DatabaseConnection newConnection() => _createConnection();