- Detect if a migration is necessary - Create a backup - Perform the migration
This commit is contained in:
@@ -78,7 +78,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
Statement st = con.createStatement();
|
||||
// table is absent
|
||||
st.execute("DROP TABLE authme");
|
||||
SQLite sqLite = new SQLite(settings, con);
|
||||
SQLite sqLite = new SQLite(settings, null, con);
|
||||
|
||||
// when
|
||||
sqLite.setup();
|
||||
@@ -100,7 +100,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
+ "username varchar(255) unique, "
|
||||
+ "password varchar(255) not null, "
|
||||
+ "primary key (id));");
|
||||
SQLite sqLite = new SQLite(settings, con);
|
||||
SQLite sqLite = new SQLite(settings, null, con);
|
||||
|
||||
// when
|
||||
sqLite.setup();
|
||||
@@ -114,7 +114,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
@Override
|
||||
protected DataSource getDataSource(String saltColumn) {
|
||||
when(settings.getProperty(DatabaseSettings.MYSQL_COL_SALT)).thenReturn(saltColumn);
|
||||
return new SQLite(settings, con);
|
||||
return new SQLite(settings, null, con);
|
||||
}
|
||||
|
||||
private static <T> void set(Property<T> property, T value) {
|
||||
|
||||
@@ -16,7 +16,7 @@ public class SQLiteResourceClosingTest extends AbstractSqlDataSourceResourceClos
|
||||
|
||||
@Override
|
||||
protected DataSource createDataSource(Settings settings, Connection connection) throws Exception {
|
||||
return new SQLite(settings, connection);
|
||||
return new SQLite(settings, null, connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package fr.xephi.authme.datasource;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
|
||||
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceTestUtil.createSqlite;
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Integration test for {@link SqLiteMigrater}. Uses a real SQLite database.
|
||||
*/
|
||||
public class SqLiteMigraterIntegrationTest {
|
||||
|
||||
private File dataFolder;
|
||||
private SQLite sqLite;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Before
|
||||
public void setup() throws SQLException, IOException, NoSuchMethodException {
|
||||
TestHelper.setupLogger();
|
||||
|
||||
Settings settings = mock(Settings.class);
|
||||
TestHelper.returnDefaultsForAllProperties(settings);
|
||||
|
||||
File sqliteDbFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "datasource/sqlite.april2016.db");
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
File tempFile = new File(dataFolder, "authme.db");
|
||||
Files.copy(sqliteDbFile, tempFile);
|
||||
|
||||
Connection con = DriverManager.getConnection("jdbc:sqlite:" + tempFile.getPath());
|
||||
sqLite = createSqlite(settings, dataFolder, con);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRun() throws ClassNotFoundException, SQLException {
|
||||
// given / when
|
||||
sqLite.setup();
|
||||
sqLite.migrateIfNeeded();
|
||||
|
||||
// then
|
||||
List<PlayerAuth> auths = sqLite.getAllAuths();
|
||||
assertThat(Lists.transform(auths, PlayerAuth::getNickname),
|
||||
containsInAnyOrder("mysql1", "mysql2", "mysql3", "mysql4", "mysql5", "mysql6"));
|
||||
PlayerAuth auth1 = getByNameOrFail("mysql1", auths);
|
||||
assertThat(auth1, hasAuthBasicData("mysql1", "mysql1", "user1@example.com", "192.168.4.41"));
|
||||
assertThat(auth1, hasAuthLocation(0, 0, 0, "world1", 0, 0));
|
||||
assertThat(auth1.getLastLogin(), equalTo(1472992664137L));
|
||||
PlayerAuth auth2 = getByNameOrFail("mysql2", auths);
|
||||
assertThat(auth2, hasAuthBasicData("mysql2", "Player", "user2@example.com", null));
|
||||
assertThat(auth2, hasAuthLocation(0, 0, 0, "world2", 0, 0));
|
||||
assertThat(auth2.getLastLogin(), equalTo(1472992668391L));
|
||||
PlayerAuth auth3 = getByNameOrFail("mysql3", auths);
|
||||
assertThat(auth3, hasAuthBasicData("mysql3", "mysql3", null, "132.54.76.98"));
|
||||
assertThat(auth3, hasAuthLocation(0, 0, 0, "world3", 0, 0));
|
||||
assertThat(auth3.getLastLogin(), equalTo(1472992672790L));
|
||||
PlayerAuth auth4 = getByNameOrFail("mysql4", auths);
|
||||
assertThat(auth4, hasAuthBasicData("mysql4", "MySQL4", null, null));
|
||||
assertThat(auth4, hasAuthLocation(25, 4, 17, "world4", 0, 0));
|
||||
assertThat(auth4.getLastLogin(), equalTo(1472992676790L));
|
||||
PlayerAuth auth5 = getByNameOrFail("mysql5", auths);
|
||||
assertThat(auth5, hasAuthBasicData("mysql5", "mysql5", null, null));
|
||||
assertThat(auth5, hasAuthLocation(0, 0, 0, "world5", 0, 0));
|
||||
assertThat(auth5.getLastLogin(), equalTo(1472992680922L));
|
||||
PlayerAuth auth6 = getByNameOrFail("mysql6", auths);
|
||||
assertThat(auth6, hasAuthBasicData("mysql6", "MySql6", "user6@example.com", "44.45.67.188"));
|
||||
assertThat(auth6, hasAuthLocation(28.5, 53.43, -147.23, "world6", 0, 0));
|
||||
assertThat(auth6.getLastLogin(), equalTo(1472992686300L));
|
||||
|
||||
// Check that backup was made
|
||||
File backupsFolder = new File(dataFolder, "backups");
|
||||
assertThat(backupsFolder.exists(), equalTo(true));
|
||||
assertThat(backupsFolder.isDirectory(), equalTo(true));
|
||||
assertThat(backupsFolder.list(), arrayContaining(containsString("authme")));
|
||||
}
|
||||
|
||||
private static PlayerAuth getByNameOrFail(String name, List<PlayerAuth> auths) {
|
||||
return auths.stream()
|
||||
.filter(auth -> name.equals(auth.getNickname()))
|
||||
.findFirst().orElseThrow(() -> new IllegalStateException("No PlayerAuth with name '" + name + "'"));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -26,27 +27,23 @@ public final class SqlDataSourceTestUtil {
|
||||
return new MySQL(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
public static SQLite createSqlite(Settings settings, Connection connection) {
|
||||
return new SQLite(settings, connection) {
|
||||
public static SQLite createSqlite(Settings settings, File dataFolder, Connection connection) {
|
||||
return new SQLite(settings, dataFolder, connection) {
|
||||
// Override reload() so it doesn't run SQLite#connect, since we're given a specific Connection to use
|
||||
@Override
|
||||
public void reload() {
|
||||
try {
|
||||
this.setup();
|
||||
this.migrateIfNeeded();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void connect() {
|
||||
// noop
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static SQLite createSqliteAndInitialize(Settings settings, Connection connection) {
|
||||
SQLite sqLite = createSqlite(settings, connection);
|
||||
try {
|
||||
sqLite.setup();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return sqLite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user