init method
Initialize the persistence layer and the database.
Implementation
Future<void> init([SmartphoneDeployment? deployment]) async {
info('Initializing $runtimeType...');
_databasePath ??= await getDatabasesPath();
// open the database - make sure to use the same database across app (re)start
database = await openDatabase(
databaseName,
version: 1,
singleInstance: true,
onCreate: (Database db, int version) async {
// when creating the database, create the tables
await db.execute('CREATE TABLE $DEPLOYMENT_TABLE_NAME ('
'$STUDY_ID_COLUMN TEXT, '
'$STUDY_DEPLOYMENT_ID_COLUMN TEXT PRIMARY KEY, '
'$DEVICE_ROLE_NAME_COLUMN TEXT, '
'$PARTICIPANT_ID_COLUMN TEXT, '
'$PARTICIPANT_ROLE_NAME_COLUMN TEXT, '
'$DEPLOYMENT_STATUS_COLUMN INTEGER, '
'$UPDATED_AT_COLUMN TEXT, '
'$DEPLOYED_AT_COLUMN TEXT, '
'$DEPLOYMENT_COLUMN TEXT)');
await db.execute('CREATE TABLE $TASK_QUEUE_TABLE_NAME ('
'$ID_COLUMN INTEGER PRIMARY KEY, '
'$STUDY_DEPLOYMENT_ID_COLUMN TEXT, '
'$TASK_ID_COLUMN TEXT, '
'$TASK_COLUMN TEXT)');
debug('$runtimeType - $databaseName DB created');
},
);
// save the deployment if specified
if (deployment != null) saveDeployment(deployment);
// listen to changes to the app task queue so we can save them
AppTaskController().userTaskEvents.listen((task) => saveUserTask(task));
info('$runtimeType - SQLite DB initialized - name: $databaseName');
}