import 'package:vitalapp/database/db.dart';
import 'interface/database_patch.dart';
import 'package:fis_common/logger/logger.dart';

///新增表patients、diagnosis中的userCode字段
class DatabasePatchAddUserCode extends IDatabasePatch {
  @override
  Future<int> performTask(int version) async {
    if (version == 0) {
      logger.w("DatabasePatchAddUserCode performTask version:$version.");
      try {
        // 新增userCode字段
        String alterTableQueryPatients = '''
          ALTER TABLE patients
          ADD COLUMN userCode VARCHAR(100) NOT NULL DEFAULT ''
        ''';
        await db.database.execute(alterTableQueryPatients);
        String alterTableQueryDiagnosis = '''
          ALTER TABLE diagnosis
          ADD COLUMN userCode VARCHAR(100) NOT NULL DEFAULT ''
        ''';
        await db.database.execute(alterTableQueryDiagnosis);
        return await super.performTask(version + 1);
      } catch (e) {
        logger.e("DatabasePatchAddUserCode performTask version:$version.", e);
        return version;
      }
    } else {
      return version;
    }
  }
}
