diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReader.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReader.java index f80d9fc57..c40fc6e17 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReader.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReader.java @@ -81,6 +81,18 @@ public final class SchemaReader { return queries; } + public static String tableFromStatement(String statement) { + Matcher table = CREATE_TABLE_PATTERN.matcher(statement); + if (table.matches()) { + return table.group(1).toLowerCase(Locale.ROOT); + } + Matcher index = CREATE_INDEX_PATTERN.matcher(statement); + if (index.matches()) { + return index.group(1).toLowerCase(Locale.ROOT); + } + throw new IllegalArgumentException("Unknown statement type: " + statement); + } + /** * Filters which statements should be executed based on the current list of tables in the database * @@ -89,16 +101,8 @@ public final class SchemaReader { * @return the filtered list of statements */ public static List filterStatements(List statements, List currentTables) { - return statements.stream().filter(statement -> { - Matcher table = CREATE_TABLE_PATTERN.matcher(statement); - if (table.matches()) { - return !currentTables.contains(table.group(1).toLowerCase(Locale.ROOT)); - } - Matcher index = CREATE_INDEX_PATTERN.matcher(statement); - if (index.matches()) { - return !currentTables.contains(index.group(1).toLowerCase(Locale.ROOT)); - } - throw new IllegalArgumentException("Unknown statement type: " + statement); - }).collect(Collectors.toList()); + return statements.stream() + .filter(statement -> !currentTables.contains(tableFromStatement(statement))) + .collect(Collectors.toList()); } } diff --git a/common/src/test/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReaderTest.java b/common/src/test/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReaderTest.java index 45d20c8a1..8b926dd9e 100644 --- a/common/src/test/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReaderTest.java +++ b/common/src/test/java/me/lucko/luckperms/common/storage/implementation/sql/SchemaReaderTest.java @@ -25,15 +25,18 @@ package me.lucko.luckperms.common.storage.implementation.sql; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.junit.jupiter.api.Test; -import org.testcontainers.shaded.com.google.common.collect.ImmutableList; import java.io.IOException; import java.io.InputStream; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SchemaReaderTest { @@ -111,6 +114,27 @@ public class SchemaReaderTest { ), readStatements("postgresql")); } + @Test + public void testTableFromStatement() throws IOException { + Set allowedTables = ImmutableSet.of( + "luckperms_user_permissions", + "luckperms_group_permissions", + "luckperms_players", + "luckperms_groups", + "luckperms_actions", + "luckperms_tracks" + ); + + for (String type : new String[]{"h2", "mariadb", "mysql", "postgresql", "sqlite"}) { + List tables = readStatements(type).stream() + .map(s -> s.replace("{prefix}", "luckperms_")) + .map(SchemaReader::tableFromStatement) + .collect(Collectors.toList()); + + assertTrue(allowedTables.containsAll(tables)); + } + } + @Test public void testFilter() throws IOException { StatementProcessor processor = s -> s.replace("{prefix}", "luckperms_"); diff --git a/standalone/src/test/java/me/lucko/luckperms/standalone/CommandsIntegrationTest.java b/standalone/src/test/java/me/lucko/luckperms/standalone/CommandsIntegrationTest.java index 362825e84..4e5780141 100644 --- a/standalone/src/test/java/me/lucko/luckperms/standalone/CommandsIntegrationTest.java +++ b/standalone/src/test/java/me/lucko/luckperms/standalone/CommandsIntegrationTest.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.standalone; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import me.lucko.luckperms.common.model.Group; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.node.types.Inheritance; @@ -36,8 +38,6 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.luckperms.api.event.log.LogNotifyEvent; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; -import org.testcontainers.shaded.com.google.common.collect.ImmutableSet; import java.nio.file.Path; import java.util.HashMap; diff --git a/standalone/src/test/java/me/lucko/luckperms/standalone/ImportExportIntegrationTest.java b/standalone/src/test/java/me/lucko/luckperms/standalone/ImportExportIntegrationTest.java index 2600033d5..cea228030 100644 --- a/standalone/src/test/java/me/lucko/luckperms/standalone/ImportExportIntegrationTest.java +++ b/standalone/src/test/java/me/lucko/luckperms/standalone/ImportExportIntegrationTest.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.standalone; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.gson.Gson; import com.google.gson.JsonObject; import me.lucko.luckperms.common.commands.misc.ExportCommand; @@ -38,8 +40,6 @@ import me.lucko.luckperms.standalone.app.integration.CommandExecutor; import me.lucko.luckperms.standalone.utils.TestPluginProvider; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.testcontainers.shaded.com.google.common.collect.ImmutableList; -import org.testcontainers.shaded.com.google.common.collect.ImmutableSet; import java.io.BufferedReader; import java.io.IOException; diff --git a/standalone/src/test/java/me/lucko/luckperms/standalone/utils/TestPluginProvider.java b/standalone/src/test/java/me/lucko/luckperms/standalone/utils/TestPluginProvider.java index 89f6ba076..de3f394a1 100644 --- a/standalone/src/test/java/me/lucko/luckperms/standalone/utils/TestPluginProvider.java +++ b/standalone/src/test/java/me/lucko/luckperms/standalone/utils/TestPluginProvider.java @@ -25,9 +25,9 @@ package me.lucko.luckperms.standalone.utils; +import com.google.common.collect.ImmutableMap; import me.lucko.luckperms.standalone.app.LuckPermsApplication; import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin; -import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; import java.nio.file.Path; import java.util.HashMap;