initialize method
Prepares context, which has its environment and bean definitions
but no beans yet.
Implementation
@override
void initialize(ConfigurableApplicationContext context) {
final environment = context.environment;
final databases = _databases(environment);
if (databases.isEmpty) {
return;
}
if (!hasSqliteDriver) {
final names = [
for (final database in databases)
database.prefix.substring(0, database.prefix.length - 1),
];
throw StateError(
'A @DataTest replaces the database of ${names.join(', ')} '
'with an in-memory SQLite one, and the application has no SQLite '
'driver: add the sqlite3 package to its dependencies (pubspec.yaml) '
'and run the code generator again, or write '
'@DataTest(replaceDatabase: false) to test on the configured '
'database',
);
}
final resolver = patternResolverOf(context);
final properties = <String, Object?>{};
for (final database in databases) {
final p = database.prefix;
properties.addAll({
'${p}url': url,
'${p}driver': 'sqlite3',
'${p}pool.max-size': '1',
'${p}pool.min-idle': '0',
'${p}pool.idle-timeout': '0',
'${p}pool.max-lifetime': '0',
});
final schemaKey = '${database.settingsPrefix}data.jdbc.schema';
if ((environment.getProperty(schemaKey) ?? '').trim().isEmpty &&
!_ownsSchema(environment, resolver, database.settingsPrefix)) {
properties[schemaKey] = 'create';
}
_log.debug(() => 'Replaced the database of ${p}url by $url');
}
environment.propertySources.addFirst(
MapPropertySource(propertySourceName, properties),
);
}