Resolve "Flyway migrations"

This commit is contained in:
anthorob committed 2025-05-22 13:09:36 +00:00
1 parent fae59dd9bb
commit 41d6de7219
11 files changed
+132 -78

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
NETNEUTRALITY_TAG=latest
NETNEUTRALITY_BOT_ENV=dev
NETNEUTRALITY_BOT_TOKEN=
NETNEUTRALITY_BOT_PREFIX=$
+1 -1
View File
@@ -20,7 +20,7 @@ ARG API_PORT=9000
EXPOSE ${API_PORT}
COPY startup.sh ./
COPY --from=build /target/netneutrality-jar-with-dependencies.jar ./
COPY --from=build /target/netneutrality.jar ./
RUN ["chmod", "+x", "./startup.sh"]
CMD ["./startup.sh"]
+1 -2
View File
@@ -5,7 +5,6 @@ services:
restart: unless-stopped
volumes:
- netneutrality-db:/var/lib/mysql
- ./sql:/docker-entrypoint-initdb.d
environment:
MYSQL_USER: 'java'
MYSQL_PASSWORD: 'password'
@@ -18,8 +17,8 @@ services:
- internal
bot:
image: registry.bobert.me/anthorob/netneutrality:${NETNEUTRALITY_TAG:-latest}
container_name: bot
build: ./
restart: unless-stopped
depends_on:
- db
+43
View File
@@ -0,0 +1,43 @@
param (
[ValidateSet("migrate", "info", "validate", "clean", "baseline", "repair")]
[string]$Command = "info"
)
$envFile = ".env"
if (-not (Test-Path $envFile)) {
Write-Error ".env file not found in the current directory."
exit 1
}
Get-Content $envFile | ForEach-Object {
if ($_ -match "^\s*([A-Za-z_][A-Za-z0-9_]*)=(.*)$") {
$name = $matches[1]
$value = $matches[2]
[System.Environment]::SetEnvironmentVariable($name, $value)
}
}
$MYSQL_HOST = $env:MYSQL_HOST
$MYSQL_PORT = $env:MYSQL_PORT
$MYSQL_DATABASE = $env:MYSQL_DATABASE
if (-not $MYSQL_HOST -or -not $MYSQL_PORT -or -not $MYSQL_DATABASE) {
Write-Error "MYSQL_HOST, MYSQL_PORT, or MYSQL_DATABASE missing from .env"
exit 1
}
$env:LOCALDEV_FLYWAY_URL = "jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
$env:FLYWAY_URL = $env:LOCALDEV_FLYWAY_URL
$env:FLYWAY_USER = $env:MYSQL_USER
$env:FLYWAY_PASSWORD = $env:MYSQL_PASSWORD
Write-Host "Ensuring database '$MYSQL_DATABASE' exists..."
$mysqlCommand = "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
& mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$env:MYSQL_USER --password=$env:MYSQL_PASSWORD -e $mysqlCommand
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to create database or connect to MySQL. Check your credentials or server status."
exit 1
}
.\mvnw flyway:$Command
+34
View File
@@ -0,0 +1,34 @@
#!/bin/bash
set -e
COMMAND=${1:-info}
ENV_FILE=".env"
if [[ ! -f "$ENV_FILE" ]]; then
echo ".env file not found in the current directory."
exit 1
fi
# Export variables from .env
export $(grep -E '^[A-Za-z_][A-Za-z0-9_]*=' "$ENV_FILE" | xargs)
if [[ -z "$MYSQL_HOST" || -z "$MYSQL_PORT" || -z "$MYSQL_DATABASE" ]]; then
echo "MYSQL_HOST, MYSQL_PORT, or MYSQL_DATABASE missing from .env"
exit 1
fi
export LOCALDEV_FLYWAY_URL="jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
export FLYWAY_URL="$LOCALDEV_FLYWAY_URL"
export FLYWAY_USER="$MYSQL_USER"
export FLYWAY_PASSWORD="$MYSQL_PASSWORD"
echo "Ensuring database '$MYSQL_DATABASE' exists..."
mysql -h"$MYSQL_HOST" -P"$MYSQL_PORT" -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" \
-e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
if [[ $? -ne 0 ]]; then
echo "Failed to create database or connect to MySQL. Check your credentials or server status."
exit 1
fi
./mvnw flyway:"$COMMAND"
+29 -17
View File
@@ -33,9 +33,9 @@
<artifactId>flyway-maven-plugin</artifactId>
<version>11.3.4</version>
<configuration>
<url>jdbc:mysql://localhost:3306/netneutrality</url>
<user>root</user>
<password></password>
<url>${env.FLYWAY_URL}</url>
<user>${env.FLYWAY_USER}</user>
<password>${env.FLYWAY_PASSWORD}</password>
</configuration>
</plugin>
<plugin>
@@ -48,34 +48,45 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>netneutrality</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
<goal>shade</goal>
</goals>
<configuration>
<finalName>netneutrality</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${main.class}</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
@@ -179,6 +190,7 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>11.3.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
@@ -9,6 +9,7 @@ package me.bobert.netneutrality;
import io.sentry.Sentry;
import me.bobert.netneutrality.core.LogHandler;
import me.bobert.netneutrality.core.command.CommandManager;
import me.bobert.netneutrality.core.db.DatabaseMigration;
import me.bobert.netneutrality.core.db.SessionManager;
import me.bobert.netneutrality.core.tasks.TaskManager;
import me.bobert.netneutrality.core.thread.ThreadManager;
@@ -67,7 +68,7 @@ public class NetNeutrality {
throw new RuntimeException("No bot token provided, exiting.");
}
// DatabaseMigration.migrate();
DatabaseMigration.migrate();
SessionManager.getSessionFactory();
CommandManager.init();
MusicManager.initPlayerManager();
@@ -18,11 +18,11 @@ public class DatabaseMigration {
String dbUser = Settings.getInstance().get("MYSQL_USER", "root");
String dbPass = Settings.getInstance().get("MYSQL_PASSWORD", "");
String dbHost = Settings.getInstance().get("MYSQL_HOST", "localhost");
String dbUrl = String.format("jdbc:mysql://%s:3306/%s", dbHost, dbName);
try {
Flyway flyway = Flyway.configure()
.dataSource(String.format("jdbc:mysql://%s:3306/%s", dbHost, dbName), dbUser, dbPass)
.baselineOnMigrate(true)
.dataSource(dbUrl, dbUser, dbPass)
.load();
flyway.migrate();
@@ -33,7 +33,7 @@ public class SessionManager {
String dbUser = Settings.getInstance().get("MYSQL_USER", "root");
String dbPass = Settings.getInstance().get("MYSQL_PASSWORD", "");
String dbHost = Settings.getInstance().get("MYSQL_HOST", "localhost");
logger.info(String.format("Connecting to database '%s' at %s:3306 with user '%s'", dbName, dbHost, dbUser));
logger.info("Connecting to database '{}' at {}:3306 with user '{}'", dbName, dbHost, dbUser);
settings.put(Environment.JAKARTA_JDBC_URL, String.format("jdbc:mysql://%s:3306/%s", dbHost, dbName));
settings.put(Environment.JAKARTA_JDBC_USER, dbUser);
@@ -1,27 +1,14 @@
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `netneutrality`
--
CREATE DATABASE IF NOT EXISTS `netneutrality` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `netneutrality`;
-- --------------------------------------------------------
--
-- Table structure for table `blacklist`
--
DROP TABLE IF EXISTS `blacklist`;
CREATE TABLE IF NOT EXISTS `blacklist` (
CREATE TABLE `blacklist` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild_id` varchar(255) NOT NULL,
`word` varchar(255) NOT NULL,
@@ -36,9 +23,7 @@ CREATE TABLE IF NOT EXISTS `blacklist` (
--
-- Table structure for table `guilds`
--
DROP TABLE IF EXISTS `guilds`;
CREATE TABLE IF NOT EXISTS `guilds` (
CREATE TABLE `guilds` (
`guild_id` varchar(255) NOT NULL,
`logging` tinyint(1) NOT NULL DEFAULT 0,
`debug` tinyint(1) NOT NULL DEFAULT 1,
@@ -51,9 +36,7 @@ CREATE TABLE IF NOT EXISTS `guilds` (
--
-- Table structure for table `guild_settings`
--
DROP TABLE IF EXISTS `guild_settings`;
CREATE TABLE IF NOT EXISTS `guild_settings` (
CREATE TABLE `guild_settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild_id` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
@@ -67,9 +50,7 @@ CREATE TABLE IF NOT EXISTS `guild_settings` (
--
-- Table structure for table `logging`
--
DROP TABLE IF EXISTS `logging`;
CREATE TABLE IF NOT EXISTS `logging` (
CREATE TABLE `logging` (
`guild_id` varchar(255) NOT NULL,
`channel_id` varchar(255) NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT 0,
@@ -81,9 +62,7 @@ CREATE TABLE IF NOT EXISTS `logging` (
--
-- Table structure for table `log_message`
--
DROP TABLE IF EXISTS `log_message`;
CREATE TABLE IF NOT EXISTS `log_message` (
CREATE TABLE `log_message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild_id` varchar(255) NOT NULL,
`channel_id` varchar(255) NOT NULL,
@@ -102,9 +81,7 @@ CREATE TABLE IF NOT EXISTS `log_message` (
--
-- Table structure for table `tasks`
--
DROP TABLE IF EXISTS `tasks`;
CREATE TABLE IF NOT EXISTS `tasks` (
CREATE TABLE `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`task_id` varchar(255) NOT NULL,
`task_class` varchar(255) NOT NULL,
@@ -118,9 +95,7 @@ CREATE TABLE IF NOT EXISTS `tasks` (
--
-- Table structure for table `web_users`
--
DROP TABLE IF EXISTS `web_users`;
CREATE TABLE IF NOT EXISTS `web_users` (
CREATE TABLE `web_users` (
`id` varchar(255) NOT NULL,
`email` mediumtext NOT NULL,
`token` varchar(255) NOT NULL,
@@ -130,10 +105,6 @@ CREATE TABLE IF NOT EXISTS `web_users` (
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `blacklist`
--
@@ -157,8 +128,3 @@ ALTER TABLE `logging`
--
ALTER TABLE `log_message`
ADD CONSTRAINT `fk_log_guilds` FOREIGN KEY (`guild_id`) REFERENCES `guilds` (`guild_id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+11 -13
View File
@@ -15,30 +15,28 @@ _term() {
trap _term TERM
trap _term INT
API_PORT="${API_PORT:-9000}"
MYSQL_HOST="${MYSQL_HOST:-localhost}"
MYSQL_PORT="${MYSQL_PORT:-3306}"
MYSQL_DATABASE="${MYSQL_DATABASE:-netneutrality}"
if [ ! -f /usr/bin/nc ]; then
echo "nc not found. Exiting."
exit 1
fi
if [ -z $API_PORT ]; then
API_PORT=9000;
fi
if [ -z $MYSQL_HOST ]; then
MYSQL_HOST="localhost"
fi
if [ -z $MYSQL_PORT ]; then
MYSQL_PORT=3306
fi
echo "Waiting 10 seconds for database to start.."
sleep 10
echo "Checking if MySQL / MariaDB is started.."
if /usr/bin/nc -z $MYSQL_HOST $MYSQL_PORT; then
echo "Database OK. Starting bot..";
/opt/java/openjdk/bin/java -jar /app/netneutrality-jar-with-dependencies.jar &
export FLYWAY_URL="jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
export FLYWAY_USER="${MYSQL_USER}"
export FLYWAY_PASSWORD="${MYSQL_PASSWORD}"
/opt/java/openjdk/bin/java -jar /app/netneutrality.jar &
pid=$!
wait "$pid"