-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create model migrations and a proper model system
- Loading branch information
1 parent
61eb1e9
commit b4f0d2f
Showing
10 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/iantapply/wynncraft/database/MigrationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/iantapply/wynncraft/database/database/MigrationsDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/com/iantapply/wynncraft/database/model/MigrationModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/iantapply/wynncraft/database/table/Column.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.