Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added db-admin command with single export-schema subcommand.
  • Loading branch information
andrewlalis committed Aug 11, 2021
commit 62d2a6de3b7968575452a6fe655d2175c9872eac
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.javadiscord.javabot.data.commands;

import com.javadiscord.javabot.commands.DelegatingCommandHandler;

public class DbAdminCommandHandler extends DelegatingCommandHandler {
public DbAdminCommandHandler() {
this.addSubcommand("export-schema", new ExportSchemaSubcommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.javadiscord.javabot.data.commands;

import com.javadiscord.javabot.Bot;
import com.javadiscord.javabot.commands.SlashCommandHandler;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;

/**
* This subcommand exports the database schema to a file, and uploads that file
* to the channel in which the command was received.
*/
public class ExportSchemaSubcommand implements SlashCommandHandler {
private static final Path SCHEMA_FILE = Path.of("___schema.sql");

@Override
public ReplyAction handle(SlashCommandEvent event) {
var includeDataOption = event.getOption("include-data");
boolean includeData = includeDataOption != null && includeDataOption.getAsBoolean();
Bot.asyncPool.submit(() -> {
try {
var con = Bot.dataSource.getConnection();
var stmt = con.createStatement();
boolean success = stmt.execute(String.format("SCRIPT %s TO '%s';", includeData ? "" : "NODATA", SCHEMA_FILE));
if (!success) {
event.getHook().sendMessage("Exporting the schema was not successful.").queue();
} else {
event.getHook().sendMessage("The export was successful.").queue();
event.getChannel().sendFile(Files.newInputStream(SCHEMA_FILE), "schema.sql").complete();
Files.delete(SCHEMA_FILE);
}
} catch (SQLException | IOException e) {
e.printStackTrace();
event.getHook().sendMessage("An error occurred, and the export could not be made: " + e.getMessage()).queue();
}
});
return event.deferReply(true);
}
}
17 changes: 17 additions & 0 deletions src/main/resources/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,20 @@
description: The amount of funds to add. Make this negative to subtract.
required: true
type: INTEGER

# Database Management Commands
- name: db-admin
description: Administrative tools for managing the server's database.
handler: com.javadiscord.javabot.data.commands.DbAdminCommandHandler
enabledByDefault: false
privileges:
- type: ROLE
id: staff_rid
subCommands:
- name: export-schema
description: Exports the current database schema to an SQL file.
options:
- name: include-data
description: Should data be included in the export?
required: true
type: BOOLEAN