diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-12-10 18:22:55 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-12-10 18:22:55 +0100 |
commit | 01624e779ab58c043edf1367dc1831bb5fa89b57 (patch) | |
tree | 53f6259559c646fbc37c0592a8d26e5fba4b3317 | |
parent | 2cb2c8f4c55e201dda7dca0f27d85059feeb6b5e (diff) | |
download | sonarqube-01624e779ab58c043edf1367dc1831bb5fa89b57.tar.gz sonarqube-01624e779ab58c043edf1367dc1831bb5fa89b57.zip |
SONAR-5919 Add USERS.SCM_ACCOUNTS column
10 files changed, 264 insertions, 7 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java index 9bf8e7ce189..7fc069e5fd1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java @@ -32,6 +32,7 @@ import org.sonar.server.db.migrations.v451.DeleteUnescapedActivities; import org.sonar.server.db.migrations.v50.*; import org.sonar.server.db.migrations.v51.FeedIssueTags; import org.sonar.server.db.migrations.v51.FeedUsersLongDates; +import org.sonar.server.db.migrations.v51.FeedUsersScmAccounts; import java.util.List; @@ -78,7 +79,8 @@ public interface DatabaseMigrations { // 5.1 FeedIssueTags.class, - FeedUsersLongDates.class - ); + FeedUsersLongDates.class, + FeedUsersScmAccounts.class + ); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccounts.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccounts.java new file mode 100644 index 00000000000..84cc595d220 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccounts.java @@ -0,0 +1,70 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.db.migrations.v51; + +import org.sonar.api.utils.System2; +import org.sonar.api.utils.text.CsvWriter; +import org.sonar.core.persistence.Database; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; + +import java.io.StringWriter; +import java.sql.SQLException; + +public class FeedUsersScmAccounts extends BaseDataChange { + + private final System2 system; + + public FeedUsersScmAccounts(Database db, System2 system) { + super(db); + this.system = system; + } + + @Override + public void execute(Context context) throws SQLException { + final long now = system.now(); + + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT u.id, u.login, u.email FROM users u WHERE scm_accounts IS NULL"); + massUpdate.update("UPDATE users SET scm_accounts=?, updated_at=? WHERE id=?"); + massUpdate.rowPluralName("users"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + String login = row.getString(2); + String email = row.getString(3); + + StringWriter writer = new StringWriter(2); + CsvWriter csv = CsvWriter.of(writer); + csv.values(login, email); + csv.close(); + + update.setString(1, writer.toString()); + update.setLong(2, now); + update.setLong(3, id); + return true; + } + }); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest.java new file mode 100644 index 00000000000..299a03836cb --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest.java @@ -0,0 +1,74 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.db.migrations.v51; + +import org.apache.commons.dbutils.DbUtils; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.TestDatabase; +import org.sonar.server.db.migrations.DatabaseMigration; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedUsersScmAccountsTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(FeedUsersScmAccountsTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(3000000000000L); + DatabaseMigration migration = new FeedUsersScmAccounts(db.database(), system); + migration.execute(); + + // Cannot use db.assertDbUnit with users table as there's a conflict with the h2 users table + assertThat(db.count("select count(*) from users where updated_at='3000000000000'")).isEqualTo(3); + assertThat(getScmAccountsFromUsers(1)).contains("user1,user1@mail.com"); + assertThat(getScmAccountsFromUsers(3)).contains("user3"); + assertThat(getScmAccountsFromUsers(4)).contains("user4,\"user4,@mail.com\""); + } + + private String getScmAccountsFromUsers(int id) throws SQLException { + PreparedStatement pstmt = db.openConnection().prepareStatement("select scm_accounts from users where id = ?"); + pstmt.setInt(1, id); + ResultSet rs = pstmt.executeQuery(); + try { + while (rs.next()) { + return rs.getString(1); + } + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(pstmt); + } + return null; + } + +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/before.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/before.xml new file mode 100644 index 00000000000..6d948efeef1 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/before.xml @@ -0,0 +1,30 @@ +<dataset> + + <users id="1" login="user1" name="User1" email="user1@mail.com" active="[true]" + scm_accounts="[null]" + created_at="1500000000000" + updated_at="1500000000000" + /> + + <!-- re-entrant migration - ignore the issues that are already fed with scm accounts --> + <users id="2" login="user2" name="User2" email="user2@mail.com" active="[true]" + scm_accounts="user2;user1@mail.com" + created_at="1500000000000" + updated_at="1500000000000" + /> + + <!-- NULL email --> + <users id="3" login="user3" name="User3" email="[null]" active="[true]" + scm_accounts="[null]" + created_at="1500000000000" + updated_at="1500000000000" + /> + + <!-- login with comma --> + <users id="4" login="user4" name="User4" email="user4,@mail.com" active="[true]" + scm_accounts="[null]" + created_at="1500000000000" + updated_at="1500000000000" + /> + +</dataset> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/schema.sql b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/schema.sql new file mode 100644 index 00000000000..81d04105e31 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersScmAccountsTest/schema.sql @@ -0,0 +1,14 @@ +CREATE TABLE "USERS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "LOGIN" VARCHAR(255), + "NAME" VARCHAR(200), + "EMAIL" VARCHAR(100), + "CRYPTED_PASSWORD" VARCHAR(40), + "SALT" VARCHAR(40), + "REMEMBER_TOKEN" VARCHAR(500), + "REMEMBER_TOKEN_EXPIRES_AT" TIMESTAMP, + "ACTIVE" BOOLEAN DEFAULT TRUE, + "SCM_ACCOUNTS" VARCHAR(4000), + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT, +); diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/755_add_scm_accounts_to_users.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/755_add_scm_accounts_to_users.rb new file mode 100644 index 00000000000..566d814b443 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/755_add_scm_accounts_to_users.rb @@ -0,0 +1,32 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# SonarQube is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +# +# SonarQube 5.1 +# SONAR-5919 +# +class AddScmAccountsToUsers < ActiveRecord::Migration + + def self.up + add_column 'users', 'scm_accounts', :string, :limit => 4000, :null => true + end + +end + diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/756_feed_users_scm_accounts.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/756_feed_users_scm_accounts.rb new file mode 100644 index 00000000000..033075527ab --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/756_feed_users_scm_accounts.rb @@ -0,0 +1,32 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# SonarQube is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +# +# SonarQube 5.1 +# SONAR-5919 +# +class FeedUsersScmAccounts < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.server.db.migrations.v51.FeedUsersScmAccounts') + end + +end + diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index e4de173996f..5da2b41df2c 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -33,7 +33,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 754; + public static final int LAST_VERSION = 756; /** * List of all the tables.n @@ -89,7 +89,7 @@ public class DatabaseVersion implements BatchComponent, ServerComponent { "user_roles", "widgets", "widget_properties" - ); + ); private MyBatis mybatis; public DatabaseVersion(MyBatis mybatis) { diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 44722fe3358..04a1e72303d 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -283,6 +283,8 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('751'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('752'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('753'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('754'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('755'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('756'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 9b434e00a20..54392299488 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -323,11 +323,12 @@ CREATE TABLE "USERS" ( "EMAIL" VARCHAR(100), "CRYPTED_PASSWORD" VARCHAR(40), "SALT" VARCHAR(40), - "CREATED_AT" BIGINT, - "UPDATED_AT" BIGINT, "REMEMBER_TOKEN" VARCHAR(500), "REMEMBER_TOKEN_EXPIRES_AT" TIMESTAMP, - "ACTIVE" BOOLEAN DEFAULT TRUE + "ACTIVE" BOOLEAN DEFAULT TRUE, + "SCM_ACCOUNTS" VARCHAR(4000), + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT ); CREATE TABLE "DASHBOARDS" ( |