syncTableSchemas method

Future<void> syncTableSchemas(
  1. List<String> tables
)

Implementation

Future<void> syncTableSchemas(List<String> tables) async {
  for (final tableName in tables) {
    _validateSqlIdentifier(tableName, 'table');
  }

  LOG.printInfo([
    'TABLE_SYNC_SERVICE',
    'metadata request',
    'tables=${tables.join(',')}',
  ]);
  final scripts = await _getTablesQueries(tables);
  LOG.printInfo([
    'TABLE_SYNC_SERVICE',
    'metadata received',
    'count=${scripts.length}',
  ]);
  final (conn, _) = await LiteConnection.getConnectionWithStatus();

  for (final script in scripts) {
    _validateSqlIdentifier(script.tableName, 'table');
    LOG.printInfo([
      'TABLE_SYNC_SERVICE',
      script.tableName,
      'metadata',
      'readOnly=${script.readOnly}',
      'writeOnly=${script.writeOnly}',
      'retentionDays=${script.retentionDays}',
    ]);

    if (!await _existsTable(script.tableName)) {
      LOG.printInfo(
          ['TABLE_SYNC_SERVICE', script.tableName, 'creating table']);
      await _executeScript(conn, script.script);
    }

    final existingInfo = await conn.query(
      'sync_info',
      where: 'table_name = ?',
      whereArgs: [script.tableName],
      limit: 1,
    );
    if (existingInfo.isEmpty) {
      await conn.insert(
        'sync_info',
        SyncInfo(
          tableName: script.tableName,
          syncAt: script.startSync,
          retentionDays: script.retentionDays,
          readOnly: script.readOnly,
          writeOnly: script.writeOnly,
        ).toMap(),
        conflictAlgorithm: ConflictAlgorithm.replace,
      );
    } else {
      await conn.update(
        'sync_info',
        {
          'retention_days': script.retentionDays,
          'read_only': script.readOnly ? 1 : 0,
          'write_only': script.writeOnly ? 1 : 0,
        },
        where: 'table_name = ?',
        whereArgs: [script.tableName],
      );
    }

    LOG.printInfo(['table sync initialized:', script.tableName]);
  }
}