From 5cea701a724733f34e70d91d5b65336c808b65fe Mon Sep 17 00:00:00 2001 From: Sébastien Lesaint Date: Thu, 8 Dec 2016 10:55:57 +0100 Subject: SONAR-8445 move DatabaseMigration to sonar-db-migration also, split state (which must be stored at platform level 2 because we need to keep state until SQ is shutdown) from migration code, which doesn't need to be kept in memory once Platform is out of safe mode --- .../platform/db/migration/DatabaseMigration.java | 36 +++++++++++ .../db/migration/DatabaseMigrationState.java | 54 +++++++++++++++++ .../db/migration/DatabaseMigrationStateImpl.java | 67 +++++++++++++++++++++ .../migration/MutableDatabaseMigrationState.java | 31 ++++++++++ .../db/migration/NoopDatabaseMigrationImpl.java | 34 +++++++++++ .../migration/DatabaseMigrationStateImplTest.java | 70 ++++++++++++++++++++++ .../migration/NoopDatabaseMigrationImplTest.java | 29 +++++++++ 7 files changed, 321 insertions(+) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationState.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MutableDatabaseMigrationState.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImpl.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImplTest.java (limited to 'server/sonar-db-migration') diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java new file mode 100644 index 00000000000..bceeb9aeb3d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigration.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +public interface DatabaseMigration { + + /** + * Starts the migration status and returns immediately. + *

+ * Migration can not be started twice but calling this method wont raise an error. + *

+ *

+ * This method should be named {@code start} but it can not be because it will be called by the pico container + * and this will cause unwanted behavior + *

+ */ + void startIt(); + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationState.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationState.java new file mode 100644 index 00000000000..f733b25fdb0 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationState.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +import java.util.Date; +import javax.annotation.CheckForNull; + +public interface DatabaseMigrationState { + + enum Status { + NONE, RUNNING, FAILED, SUCCEEDED + } + + /** + * Current status of the migration. + */ + Status getStatus(); + + /** + * The time and day the last migration was started. + *

+ * If no migration was ever started, the returned date is {@code null}. + *

+ * + * @return a {@link Date} or {@code null} + */ + @CheckForNull + Date getStartedAt(); + + /** + * The error of the last migration if it failed. + * + * @return a {@link Throwable} or {@code null} + */ + @CheckForNull + Throwable getError(); +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java new file mode 100644 index 00000000000..434934daae2 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImpl.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +import java.util.Date; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +/** + * This implementation of {@link MutableDatabaseMigrationState} does not provide any thread safety. + */ +public class DatabaseMigrationStateImpl implements MutableDatabaseMigrationState { + private Status status = Status.NONE; + @Nullable + private Date startedAt; + @Nullable + private Throwable error; + + @Override + public Status getStatus() { + return status; + } + + @Override + public void setStatus(Status status) { + this.status = status; + } + + @Override + @CheckForNull + public Date getStartedAt() { + return startedAt; + } + + @Override + public void setStartedAt(@Nullable Date startedAt) { + this.startedAt = startedAt; + } + + @Override + @CheckForNull + public Throwable getError() { + return error; + } + + @Override + public void setError(@Nullable Throwable error) { + this.error = error; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MutableDatabaseMigrationState.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MutableDatabaseMigrationState.java new file mode 100644 index 00000000000..bb500c4e280 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MutableDatabaseMigrationState.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +import java.util.Date; +import javax.annotation.Nullable; + +public interface MutableDatabaseMigrationState extends DatabaseMigrationState { + void setStatus(Status status); + + void setStartedAt(@Nullable Date startedAt); + + void setError(@Nullable Throwable error); +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImpl.java new file mode 100644 index 00000000000..7ad045dcead --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImpl.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +/** + * Implementation of {@link DatabaseMigration} which performs no operation at all. + * It is meant to be used when the platform in NOT in safe mode, which means that the database is up to date + * and migration is neither required nor should be performed. + */ +public class NoopDatabaseMigrationImpl implements DatabaseMigration { + + @Override + public void startIt() { + // do nothing + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java new file mode 100644 index 00000000000..81322534f74 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/DatabaseMigrationStateImplTest.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +import java.util.Date; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DatabaseMigrationStateImplTest { + private DatabaseMigrationStateImpl underTest = new DatabaseMigrationStateImpl(); + + @Test + public void getStatus_returns_NONE_when_component_is_created() { + assertThat(underTest.getStatus()).isEqualTo(DatabaseMigrationState.Status.NONE); + } + + @Test + public void getStatus_returns_argument_of_setStatus() { + for (DatabaseMigrationState.Status status : DatabaseMigrationState.Status.values()) { + underTest.setStatus(status); + + assertThat(underTest.getStatus()).isEqualTo(status); + } + + } + + @Test + public void getStartedAt_returns_null_when_component_is_created() { + assertThat(underTest.getStartedAt()).isNull(); + } + + @Test + public void getStartedAt_returns_argument_of_setStartedAt() { + Date expected = new Date(); + underTest.setStartedAt(expected); + + assertThat(underTest.getStartedAt()).isSameAs(expected); + } + + @Test + public void getError_returns_null_when_component_is_created() { + assertThat(underTest.getError()).isNull(); + } + + @Test + public void getError_returns_argument_of_setError() { + RuntimeException expected = new RuntimeException(); + underTest.setError(expected); + + assertThat(underTest.getError()).isSameAs(expected); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImplTest.java new file mode 100644 index 00000000000..122a6c9c086 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/NoopDatabaseMigrationImplTest.java @@ -0,0 +1,29 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.platform.db.migration; + +import org.junit.Test; + +public class NoopDatabaseMigrationImplTest { + @Test + public void startIt_does_nothing() { + new NoopDatabaseMigrationImpl().startIt(); + } +} -- cgit v1.2.3