Skip to content

Commit

Permalink
Create model migrations and a proper model system
Browse files Browse the repository at this point in the history
  • Loading branch information
IanTapply22 committed Nov 30, 2024
1 parent 61eb1e9 commit b4f0d2f
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/iantapply/wynncraft/Wynncraft.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.iantapply.wynncraft;

import com.iantapply.wynncraft.command.CommandCore;
import com.iantapply.wynncraft.database.database.MigrationsDatabase;
import com.iantapply.wynncraft.database.database.WynncraftDatabase;
import com.iantapply.wynncraft.logger.Logger;
import lombok.Getter;
Expand All @@ -14,6 +15,7 @@ public final class Wynncraft extends JavaPlugin {
public void onEnable() {
Logger.logInitialization();

new MigrationsDatabase().connect();
new WynncraftDatabase().connect();

CommandCore commandCore = new CommandCore(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* but not limited to building URLs, finishing queries, and interacting
* with tables
*/
// TODO: Remove thrown exceptions and handle
public class DatabaseHelpers {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.iantapply.wynncraft.database;

public class MigrationHandler {
// TODO: Create a way to mark models as automatically migrating upon startup
// TODO: Create method to notify console of outdated migrations
// TODO: Use database object to make connection for below query
// TODO: If model can be migrated, migrate it by altering the table and adding the columns in the "columns" value of the interface
// TODO: Upon failing, notify console
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.iantapply.wynncraft.database.database;

import com.iantapply.wynncraft.database.Database;
import com.iantapply.wynncraft.database.DatabaseInformation;
import com.iantapply.wynncraft.logger.Logger;
import com.iantapply.wynncraft.logger.LoggingLevel;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class MigrationsDatabase extends Database {

/**
* The information associated with the database
*
* @return The information as a DatabaseInformation object
*/
@Override
public DatabaseInformation databaseInformation() {
return new DatabaseInformation("localhost", "5432", "migrations", "Migrations", "root", "wynncraft");
}

/**
* Initializes the database
*/
@Override
public void initialize() {
Logger.log(LoggingLevel.INFO, "Initializing the" + databaseInformation().getDisplayName() +"database...");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.iantapply.wynncraft.database.model;

import com.iantapply.wynncraft.database.Database;
import com.iantapply.wynncraft.database.database.ExampleDatabase;
import com.iantapply.wynncraft.database.table.Column;
import com.iantapply.wynncraft.database.table.DataType;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

/**
* Represents an example model that can be used to demonstrate
* how to create a model for the database.
Expand All @@ -17,6 +23,37 @@ public ExampleModel(Integer someNumber, String someString) {
this.someString = someString;
}

/**
* The database, or table that the model is the child of. This is also referred to as
* the parent of the model.
* @return The Database object of the table
*/
@Override
public Database database() {
return new ExampleDatabase();
}

/**
* The table the migration will be run on inside the parent
* @return The name of the table in the parent database
*/
@Override
public String table() {
return "example";
}

/**
* The columns that will appear in order and in the specified parent
* @return An arraylist of column objects the contain the information of the column
*/
@Override
public ArrayList<Column> columns() {
ArrayList<Column> columns = new ArrayList<>();
columns.add(new Column("Example", DataType.TEXT));

return columns;
}

/**
* The name of the model, used in the initialization of the database and debugging
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.iantapply.wynncraft.database.model;

import com.iantapply.wynncraft.database.Database;
import com.iantapply.wynncraft.database.database.MigrationsDatabase;
import com.iantapply.wynncraft.database.table.Column;
import com.iantapply.wynncraft.database.table.DataType;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

/**
* A model that is used to track the versioning of models as they are migrated.
* <p>
* This is used to notify administrators of migrations that are needed to be performed
* on the database.
*/
@Getter @Setter
public class MigrationModel implements Model {
public String uuid;
public String version;

public MigrationModel(String uuid, String version) {
this.uuid = uuid;
this.version = version;
}

/**
* The database, or table that the model is the child of. This is also referred to as
* the parent of the model.
* @return The Database object of the table
*/
@Override
public Database database() {
return new MigrationsDatabase();
}

/**
* The table the migration will be run on inside the parent
* @return The name of the table in the parent database
*/
@Override
public String table() {
return "migrations";
}

/**
* The columns that will appear in order and in the specified parent
* @return An arraylist of column objects the contain the information of the column
*/
@Override
public ArrayList<Column> columns() {
ArrayList<Column> columns = new ArrayList<>();
columns.add(new Column("uuid", DataType.UUID));
columns.add(new Column("version", DataType.TEXT));

return columns;
}

/**
* The name of the model, used in the initialization of the database and debugging
*
* @return The name of the model as a string
*/
@Override
public String name() {
return "Example";
}

/**
* The version of the model, should be updated when changes have been
* made to the model that require a database migration.
* <p>
* The version should be in the format of "major.minor.patch" (e.g. "7.7.2")
*
* @return The version of the model as a string
*/
@Override
public String version() {
return "1.0.0";
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/iantapply/wynncraft/database/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
package com.iantapply.wynncraft.database.model;

import com.iantapply.wynncraft.database.Database;
import com.iantapply.wynncraft.database.DatabaseHelpers;
import com.iantapply.wynncraft.database.table.Column;
import com.iantapply.wynncraft.logger.Logger;
import com.iantapply.wynncraft.logger.LoggingLevel;

import java.sql.SQLException;
import java.util.ArrayList;

/**
* Represents a model that contains various data on an object
*/
public interface Model {

/**
* The database, or table that the model is the child of. This is also referred to as
* the parent of the model.
* @return The Database object of the table
*/
Database database();

/**
* The table the migration will be run on inside the parent
* @return The name of the table in the parent database
*/
String table();

/**
* The columns that will appear in order and in the specified parent
* @return An arraylist of column objects the contain the information of the column
*/
ArrayList<Column> columns();

/**
* The name of the model, used in the initialization of the database and debugging
* @return The name of the model as a string
Expand All @@ -19,4 +47,43 @@ public interface Model {
* @return The version of the model as a string
*/
String version();

/**
* Whether the model should be migrated automatically upon creation or a modification.
* @return A boolean representing if it should be able to automatically migrate. This
* is by default false.
*/
default boolean automaticallyMigrate() {
return false;
}

/**
* Migrates the model on a table and creates the columns by default
* @throws SQLException If the statement cannot be executed or close. This will be removed
* as there will be handles for this error.
*/
default void migrate() throws SQLException {
// TODO: Remove SQLException in parallel with createColumn method
// Loops over each column and adds the column to the specified table in the model
// This is restricted to only run on the specified table in the specified database.
for (Column column : columns()) {
// Catch clause to catch if there's no default value
if (column.getDefaultValue() == null) {
DatabaseHelpers.createColumn(database().connection(), table(), column.getName(), column.getType().getType());
continue;
}

// Create the column with a default value if there's one present
DatabaseHelpers.createColumn(database().connection(), table(), column.getName(), column.getType().getType(), column.getDefaultValue());
}
}

/**
* Reverts the migration to the specified state
* <p>
* This has no action by default as it can be intrusive to assume actions.
*/
default void revert() {
Logger.log(LoggingLevel.INFO, "No reversion is available. Please set one.");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.iantapply.wynncraft.database.model;

import com.iantapply.wynncraft.database.Database;
import com.iantapply.wynncraft.database.table.Column;
import com.iantapply.wynncraft.rank.NonPurchasableRank;
import com.iantapply.wynncraft.rank.PurchasableRank;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

@Getter @Setter
public class PlayerModel implements Model {
private String id;
private String uuid;
/* These are stored separately to give players permissions based on things that other ranks doesn't have **/
private PurchasableRank purchasedRank;
private NonPurchasableRank nonPurchasedRank;
Expand All @@ -17,6 +21,34 @@ public class PlayerModel implements Model {
public PlayerModel(Integer someNumber, String someString) {
}

/**
* The database, or table that the model is the child of. This is also referred to as
* the parent of the model.
* @return The Database object of the table
*/
@Override
public Database database() {
return null;
}

/**
* The table the migration will be run on inside the parent
* @return The name of the table in the parent database
*/
@Override
public String table() {
return null;
}

/**
* The columns that will appear in order and in the specified parent
* @return An arraylist of column objects the contain the information of the column
*/
@Override
public ArrayList<Column> columns() {
return null;
}

/**
* The name of the model, used in the initialization of the database and debugging
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/iantapply/wynncraft/database/table/Column.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.iantapply.wynncraft.database.table;

import lombok.Getter;
import lombok.Setter;

/**
* Represents a column in a table with the name and type
* <p>
* This is used to create a column for an existing or new database table.
*/
@Getter @Setter
public class Column {
private String name;
private DataType type;
private String defaultValue;

/**
* A representation of a column in a database table
* @param name The name of the column
* @param type The PostgreSQL type that the column cells will always have
*/
public Column(String name, DataType type) {
this.name = name;
this.type = type;
}

/**
* A representation of a column in a database table
* @param name The name of the column
* @param type The PostgreSQL type that the column cells will always have
* @param defaultValue The default value that appears in the column when creating a row
*/
public Column(String name, DataType type, String defaultValue) {
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
}
}
Loading

0 comments on commit b4f0d2f

Please sign in to comment.