|
| 1 | +package com.javadiscord.javabot.data.commands; |
| 2 | + |
| 3 | +import com.javadiscord.javabot.Bot; |
| 4 | +import com.javadiscord.javabot.commands.Responses; |
| 5 | +import com.javadiscord.javabot.commands.SlashCommandHandler; |
| 6 | +import com.javadiscord.javabot.data.MigrationUtils; |
| 7 | +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
| 8 | +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.net.URISyntaxException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.sql.SQLException; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +/** |
| 18 | + * This subcommand is responsible for executing SQL migrations on the bot's |
| 19 | + * schema. |
| 20 | + * <p> |
| 21 | + * It uses the given name (adding .sql if it's not already there) to look |
| 22 | + * for a matching file in the /migrations/ resource directory. Once it's |
| 23 | + * found the file, it will split it up into a list of statements by the ';' |
| 24 | + * character, and then proceed to execute each statement. |
| 25 | + * </p> |
| 26 | + */ |
| 27 | +public class MigrateSubcommand implements SlashCommandHandler { |
| 28 | + @Override |
| 29 | + public ReplyAction handle(SlashCommandEvent event) { |
| 30 | + String migrationName = Objects.requireNonNull(event.getOption("name")).getAsString(); |
| 31 | + if (!migrationName.endsWith(".sql")) { |
| 32 | + migrationName = migrationName + ".sql"; |
| 33 | + } |
| 34 | + try { |
| 35 | + Path migrationsDir = MigrationUtils.getMigrationsDirectory(); |
| 36 | + Path migrationFile = migrationsDir.resolve(migrationName); |
| 37 | + if (Files.notExists(migrationFile)) { |
| 38 | + return Responses.warning(event, "The specified migration `" + migrationName + "` does not exist."); |
| 39 | + } |
| 40 | + String sql = Files.readString(migrationFile); |
| 41 | + String[] statements = sql.split("\\s*;\\s*"); |
| 42 | + if (statements.length == 0) { |
| 43 | + return Responses.warning(event, "The migration `" + migrationName + "` does not contain any statements. Please remove or edit it before running again."); |
| 44 | + } |
| 45 | + Bot.asyncPool.submit(() -> { |
| 46 | + try (var con = Bot.dataSource.getConnection()) { |
| 47 | + for (int i = 0; i < statements.length; i++) { |
| 48 | + if (statements[i].isBlank()) { |
| 49 | + event.getChannel().sendMessage("Skipping statement " + i + "; it is blank.").complete(); |
| 50 | + continue; |
| 51 | + } |
| 52 | + try (var stmt = con.createStatement()){ |
| 53 | + int rowsUpdated = stmt.executeUpdate(statements[i]); |
| 54 | + event.getChannel().sendMessageFormat( |
| 55 | + "Executed statement %d of %d:\n```\n%s\n```\nRows Updated: `%d`", i, statements.length, statements[i], rowsUpdated |
| 56 | + ).complete(); |
| 57 | + } catch (SQLException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + event.getChannel().sendMessage("Error while executing statement " + i + ": " + e.getMessage()).queue(); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (SQLException e) { |
| 64 | + event.getChannel().sendMessage("Could not obtain a connection to the database.").queue(); |
| 65 | + } |
| 66 | + }); |
| 67 | + return Responses.info(event, "Migration Started", "Execution of the migration `" + migrationName + "` has been started. " + statements.length + " statements will be executed."); |
| 68 | + } catch (IOException | URISyntaxException e) { |
| 69 | + return Responses.error(event, e.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments