From 02cade230aec6e9f45f69185a2feec663e24da8a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 10 Dec 2014 13:44:18 +0100 Subject: [PATCH] SONAR-5918 Convert USERS.UPDATED_AT and USERS.CREATED_AT in long --- .../db/migrations/DatabaseMigrations.java | 4 +- .../db/migrations/v51/FeedUsersLongDates.java | 72 +++++++++++++++++++ .../v51/FeedUsersLongDatesTest.java | 51 +++++++++++++ .../QProfileProjectOperationsMediumTest.java | 4 +- .../QProfileServiceMediumTest.java | 3 +- .../v51/FeedUsersLongDatesTest/before.xml | 26 +++++++ .../v51/FeedUsersLongDatesTest/schema.sql | 15 ++++ .../db/GroupDaoTest/find_by_user_login.xml | 22 +----- .../app/controllers/users_controller.rb | 1 + .../main/webapp/WEB-INF/app/models/user.rb | 16 +++++ .../db/migrate/752_add_users_long_dates.rb | 32 +++++++++ .../db/migrate/753_feed_users_long_dates.rb | 32 +++++++++ .../db/migrate/754_rename_users_long_dates.rb | 36 ++++++++++ .../webapp/WEB-INF/lib/need_authentication.rb | 10 ++- .../core/persistence/DatabaseVersion.java | 4 +- .../java/org/sonar/core/user/UserDto.java | 13 ++-- .../org/sonar/core/persistence/rows-h2.sql | 5 +- .../org/sonar/core/persistence/schema-h2.ddl | 6 +- .../java/org/sonar/core/user/UserDaoTest.java | 21 ++++-- .../UserDaoTest/deactivate_user-result.xml | 27 ++----- .../core/user/UserDaoTest/deactivate_user.xml | 31 ++------ .../core/user/UserDaoTest/insert-result.xml | 2 +- .../UserDaoTest/selectActiveUserByLogin.xml | 6 +- .../user/UserDaoTest/selectGroupByName.xml | 2 +- .../user/UserDaoTest/selectUsersByLogins.xml | 6 +- .../user/UserDaoTest/selectUsersByQuery.xml | 23 +----- .../user/UserDaoTest/selectUsersByText.xml | 23 +----- 27 files changed, 349 insertions(+), 144 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersLongDates.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest.java create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/before.xml create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/schema.sql create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/752_add_users_long_dates.rb create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/753_feed_users_long_dates.rb create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/754_rename_users_long_dates.rb 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 10044861b2b..9bf8e7ce189 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 @@ -31,6 +31,7 @@ import org.sonar.server.db.migrations.v451.AddMissingCustomRuleParametersMigrati 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 java.util.List; @@ -76,7 +77,8 @@ public interface DatabaseMigrations { FeedIssueLongDates.class, // 5.1 - FeedIssueTags.class + FeedIssueTags.class, + FeedUsersLongDates.class ); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersLongDates.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersLongDates.java new file mode 100644 index 00000000000..5311c4285e6 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedUsersLongDates.java @@ -0,0 +1,72 @@ +/* + * 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.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.sql.SQLException; +import java.util.Date; + +public class FeedUsersLongDates extends BaseDataChange { + + private final System2 system; + + public FeedUsersLongDates(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.created_at, u.updated_at FROM users u WHERE created_at_ms IS NULL"); + massUpdate.update("UPDATE users SET created_at_ms=?, updated_at_ms=? 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); + Date createdAt = row.getDate(2); + Date updatedAt = row.getDate(3); + + if (createdAt == null) { + update.setLong(1, now); + } else { + update.setLong(1, Math.min(now, createdAt.getTime())); + } + if (updatedAt == null) { + update.setLong(2, now); + } else { + update.setLong(2, Math.min(now, updatedAt.getTime())); + } + update.setLong(3, id); + return true; + } + }); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest.java new file mode 100644 index 00000000000..77fc84f144a --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest.java @@ -0,0 +1,51 @@ +/* + * 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.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 static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedUsersLongDatesTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(FeedUsersLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(1500000000000L); + DatabaseMigration migration = new FeedUsersLongDates(db.database(), system); + migration.execute(); + + int count = db.count("select count(*) from users where created_at_ms is not null and updated_at_ms is not null"); + assertThat(count).isEqualTo(3); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java index 567245d08af..1e3104f3069 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java @@ -36,8 +36,6 @@ import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; import org.sonar.server.user.UserSession; -import java.util.Date; - import static org.fest.assertions.Assertions.assertThat; public class QProfileProjectOperationsMediumTest { @@ -136,7 +134,7 @@ public class QProfileProjectOperationsMediumTest { db.componentDao().insert(dbSession, project2); // Create a user having user permission on the two projects and the global quality profile admin permission - UserDto user = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(new Date()).setUpdatedAt(new Date()); + UserDto user = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis()); db.userDao().insert(dbSession, user); tester.get(PermissionFacade.class).insertUserPermission(project1.getId(), user.getId(), UserRole.USER, dbSession); tester.get(PermissionFacade.class).insertUserPermission(project2.getId(), user.getId(), UserRole.USER, dbSession); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index 73387e8481a..ba027b787d2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -55,7 +55,6 @@ import org.sonar.server.user.MockUserSession; import java.io.IOException; import java.io.Reader; import java.io.Writer; -import java.util.Date; import java.util.List; import java.util.Map; @@ -181,7 +180,7 @@ public class QProfileServiceMediumTest { public void search_qprofile_activity() throws InterruptedException { MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david"); - UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(new Date()).setUpdatedAt(new Date()); + UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis()); db.userDao().insert(dbSession, user); // We need an actual rule in DB to test RuleName in Activity diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/before.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/before.xml new file mode 100644 index 00000000000..27a64351c01 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/before.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/schema.sql b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/schema.sql new file mode 100644 index 00000000000..ba24adf3a29 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v51/FeedUsersLongDatesTest/schema.sql @@ -0,0 +1,15 @@ +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), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, + "CREATED_AT_MS" BIGINT, + "UPDATED_AT_MS" BIGINT, + "REMEMBER_TOKEN" VARCHAR(500), + "REMEMBER_TOKEN_EXPIRES_AT" TIMESTAMP, + "ACTIVE" BOOLEAN DEFAULT TRUE +); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/db/GroupDaoTest/find_by_user_login.xml b/server/sonar-server/src/test/resources/org/sonar/server/user/db/GroupDaoTest/find_by_user_login.xml index ea360dd7af1..da498e192bd 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/db/GroupDaoTest/find_by_user_login.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/db/GroupDaoTest/find_by_user_login.xml @@ -1,23 +1,3 @@ - - @@ -27,6 +7,6 @@ - + diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/users_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/users_controller.rb index ba549b36f7b..4f3495d7a3e 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/users_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/users_controller.rb @@ -30,6 +30,7 @@ class UsersController < ApplicationController @errors = [] user = User.find_by_login(params[:user][:login]) if user && !user.active + user.updated_at = Java::JavaLang::System.java_class.currentTimeMillis() if user.update_attributes(params[:user]) # case user: exist,inactive,no errors when update BUT TO REACTIVATE @user = user diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/models/user.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/models/user.rb index 39325bd9b13..001521734bd 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/models/user.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/models/user.rb @@ -63,6 +63,22 @@ class User < ActiveRecord::Base # anything else you want your user to change should be added here. attr_accessible :login, :email, :name, :password, :password_confirmation + #### + # As now dates are saved in long they should be no more automatically managed by Rails + ### + + def record_timestamps + false + end + + def before_create + self.created_at = java.lang.System.currentTimeMillis + self.updated_at = java.lang.System.currentTimeMillis + end + + def before_save + self.updated_at = java.lang.System.currentTimeMillis + end def email=(value) write_attribute :email, (value && value.downcase) diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/752_add_users_long_dates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/752_add_users_long_dates.rb new file mode 100644 index 00000000000..5c7dce4b952 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/752_add_users_long_dates.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-5918 +# +class AddUsersLongDates < ActiveRecord::Migration + + def self.up + add_column 'users', :created_at_ms, :big_integer, :null => true + add_column 'users', :updated_at_ms, :big_integer, :null => true + end +end + diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/753_feed_users_long_dates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/753_feed_users_long_dates.rb new file mode 100644 index 00000000000..1a83d496f41 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/753_feed_users_long_dates.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-5918 +# +class FeedUsersLongDates < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.server.db.migrations.v51.FeedUsersLongDates') + end + +end + diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/754_rename_users_long_dates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/754_rename_users_long_dates.rb new file mode 100644 index 00000000000..57ebabf6094 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/754_rename_users_long_dates.rb @@ -0,0 +1,36 @@ +# +# 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-5918 +# +class RenameUsersLongDates < ActiveRecord::Migration + + def self.up + remove_column 'users', 'created_at' + remove_column 'users', 'updated_at' + rename_column 'users', 'created_at_ms', 'created_at' + rename_column 'users', 'updated_at_ms', 'updated_at' + add_index 'users', 'updated_at', :name => 'users_updated_at' + end + +end + diff --git a/server/sonar-web/src/main/webapp/WEB-INF/lib/need_authentication.rb b/server/sonar-web/src/main/webapp/WEB-INF/lib/need_authentication.rb index 3e49975583a..22da5fdba07 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/lib/need_authentication.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/lib/need_authentication.rb @@ -66,6 +66,7 @@ class PluginRealm user = User.find_active_by_login(username) # SONAR-4950 Use a transaction to prevent multiple insertion of same groups User.transaction do + user.updated_at = Java::JavaLang::System.java_class.currentTimeMillis() user.save(false) end result = user if user && user.authenticated?(password) @@ -122,13 +123,16 @@ class PluginRealm username=details.getName() if username.blank? && details user = User.find_by_login(username) + now = java.lang.System.currentTimeMillis + # SONAR-4950 Use a transaction to prevent multiple insertion of same groups User.transaction do if !user # No such user in Sonar database return nil if !Api::Utils.java_facade.getSettings().getBoolean('sonar.authenticator.createUsers') # Automatically create a user in the sonar db if authentication has been successfully done - user = User.new(:login => username, :name => username, :email => '') + user = User.new(:login => username, :name => username, :email => '', :created_at => now, :updated_at => now) + if details user.name = details.getName() user.email = details.getEmail() @@ -147,8 +151,8 @@ class PluginRealm user.email = details.getEmail() end - # Force the update of updated_ad in order to execute an SQL update to block other session - user.updated_at = Time.now + # Force the update of updated_at in order to execute an SQL update to block other session + user.updated_at = now end if @save_password user.password = password 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 4a103c02590..e4de173996f 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,10 +33,10 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 751; + public static final int LAST_VERSION = 754; /** - * List of all the tables. + * List of all the tables.n * This list is hardcoded because we didn't succeed in using java.sql.DatabaseMetaData#getTables() in the same way * for all the supported databases, particularly due to Oracle results. */ diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserDto.java b/sonar-core/src/main/java/org/sonar/core/user/UserDto.java index 585299a6713..4fc269fc0fa 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserDto.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserDto.java @@ -20,7 +20,6 @@ package org.sonar.core.user; import javax.annotation.Nullable; -import java.util.Date; /** * @since 3.2 @@ -30,8 +29,8 @@ public class UserDto { private String login; private String name; private String email; - private Date createdAt; - private Date updatedAt; + private Long createdAt; + private Long updatedAt; private boolean active = true; public Long getId() { @@ -70,20 +69,20 @@ public class UserDto { return this; } - public Date getCreatedAt() { + public Long getCreatedAt() { return createdAt; } - public UserDto setCreatedAt(Date createdAt) { + public UserDto setCreatedAt(Long createdAt) { this.createdAt = createdAt; return this; } - public Date getUpdatedAt() { + public Long getUpdatedAt() { return updatedAt; } - public UserDto setUpdatedAt(Date updatedAt) { + public UserDto setUpdatedAt(Long updatedAt) { this.updatedAt = updatedAt; return this; } 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 6e9e8f5c12a..44722fe3358 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 @@ -280,6 +280,9 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('719'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('720'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('750'); 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 USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); +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 7e26868aaab..9b434e00a20 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,8 +323,8 @@ CREATE TABLE "USERS" ( "EMAIL" VARCHAR(100), "CRYPTED_PASSWORD" VARCHAR(40), "SALT" VARCHAR(40), - "CREATED_AT" TIMESTAMP, - "UPDATED_AT" TIMESTAMP, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT, "REMEMBER_TOKEN" VARCHAR(500), "REMEMBER_TOKEN_EXPIRES_AT" TIMESTAMP, "ACTIVE" BOOLEAN DEFAULT TRUE @@ -694,6 +694,8 @@ CREATE INDEX "ISSUE_FILTER_FAVS_USER" ON "ISSUE_FILTER_FAVOURITES" ("USER_LOGIN" CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN"); +CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT"); + CREATE INDEX "SNAPSHOTS_ROOT_PROJECT_ID" ON "SNAPSHOTS" ("ROOT_PROJECT_ID"); CREATE INDEX "GROUP_ROLES_ROLE" ON "GROUP_ROLES" ("ROLE"); diff --git a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java index 981695b23c9..0781d960195 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java @@ -27,7 +27,10 @@ import org.sonar.api.utils.DateUtils; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.persistence.DbSession; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -166,12 +169,22 @@ public class UserDaoTest extends AbstractDaoTestCase { @Test public void insert_user() { - Date date = DateUtils.parseDate("2014-06-20"); + Long date = DateUtils.parseDate("2014-06-20").getTime(); UserDto userDto = new UserDto().setId(1L).setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(date).setUpdatedAt(date); dao.insert(session, userDto); - - checkTables("insert"); + session.commit(); + + UserDto user = dao.selectActiveUserByLogin("john"); + assertThat(user).isNotNull(); + assertThat(user.getId()).isNotNull(); + assertThat(user.getLogin()).isEqualTo("john"); + assertThat(user.getName()).isEqualTo("John"); + assertThat(user.getEmail()).isEqualTo("jo@hn.com"); + assertThat(user.getCreatedAt()).isEqualTo(date); + assertThat(user.getUpdatedAt()).isEqualTo(date); + + // checkTables("insert", new String[] {"id"}, "users"); } @Test diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml index ff45e5df1ab..0de00c6c004 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml @@ -1,25 +1,6 @@ - - + - + @@ -43,13 +24,13 @@ - + - + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml index 2cd2c16822d..285b4f3c7ed 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml @@ -1,41 +1,22 @@ - - + - - + + - + - + @@ -45,7 +26,7 @@ - + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml index 374d956745d..f5b642f4fed 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml @@ -1,3 +1,3 @@ - + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml index 0052e0c54da..1927eda7d63 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml @@ -1,11 +1,11 @@ - + - + - + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectGroupByName.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectGroupByName.xml index 9434d78f050..01755d808c0 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectGroupByName.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectGroupByName.xml @@ -1,4 +1,4 @@ - \ No newline at end of file + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByLogins.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByLogins.xml index c192749b43a..3e671162c58 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByLogins.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByLogins.xml @@ -1,6 +1,6 @@ - - - + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByQuery.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByQuery.xml index 9614d25eb15..52a2421ea31 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByQuery.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByQuery.xml @@ -1,24 +1,5 @@ - - - + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByText.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByText.xml index 09a42cde6f8..6f5237422f9 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByText.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUsersByText.xml @@ -1,24 +1,5 @@ - - - + + -- 2.39.5