3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v72;
22 import java.sql.SQLException;
23 import org.sonar.api.utils.System2;
24 import org.sonar.api.utils.log.Logger;
25 import org.sonar.api.utils.log.Loggers;
26 import org.sonar.core.util.UuidFactory;
27 import org.sonar.db.Database;
28 import org.sonar.server.platform.db.migration.step.DataChange;
29 import org.sonar.server.platform.db.migration.step.MassUpdate;
32 * The goal of this migration is to sanitize disabled USERS, regarding new way of authentication users.
33 * Indeed, authentication will search for user but LOGIN but also buy using EXTERNAL_ID and EXTERNAL_PROVIDER.
35 * As a consequence, these columns must be set as NOT NULL in order to add a UNIQUE index on them.
37 * Unfortunately, these columns were previously set as null when disabling a user, that's why we need to populate them.
39 public class UpdateNullValuesFromExternalColumnsAndLoginOfUsers extends DataChange {
41 private static final Logger LOG = Loggers.get(UpdateNullValuesFromExternalColumnsAndLoginOfUsers.class);
43 private final System2 system2;
44 private UuidFactory uuidFactory;
46 public UpdateNullValuesFromExternalColumnsAndLoginOfUsers(Database db, System2 system2, UuidFactory uuidFactory) {
48 this.system2 = system2;
49 this.uuidFactory = uuidFactory;
53 public void execute(Context context) throws SQLException {
54 MassUpdate massUpdate = context.prepareMassUpdate().rowPluralName("users");
55 massUpdate.select("SELECT id, login FROM users WHERE login IS NULL OR external_login IS NULL OR external_identity_provider IS NULL");
56 massUpdate.update("UPDATE users SET login=?, external_login=?, external_identity_provider=?, updated_at=? WHERE id=?");
58 long now = system2.now();
59 massUpdate.execute((row, update) -> {
60 long id = row.getLong(1);
61 String login = row.getString(2);
63 LOG.warn("No login has been found for user id '{}'. A UUID has been generated to not have null value.", id);
64 login = uuidFactory.create();
66 update.setString(1, login);
67 update.setString(2, login);
68 update.setString(3, "sonarqube");
69 update.setLong(4, now);
70 update.setLong(5, id);