@@ -69,7 +69,7 @@ protected MigrationManager(FastLoginCore<?, ?, ?> core, SQLStorage storage) {
6969 this .storage = storage ;
7070 }
7171
72- protected void createTables () throws SQLException {
72+ private void createMigrationTable () throws SQLException {
7373 try (Connection con = storage .getDataSource ().getConnection ();
7474 Statement createStmt = con .createStatement ()) {
7575
@@ -84,6 +84,29 @@ protected void createTables() throws SQLException {
8484 }
8585 }
8686
87+ /**
88+ * Initialize tables if they don't exist yet
89+ * @throws SQLException
90+ */
91+ protected void createTables () throws SQLException {
92+ createMigrationTable ();
93+ createTable (storage .getAuthStorage ());
94+ }
95+
96+ /**
97+ * Create a new table in the database, and add an entry about it to MigrationManager
98+ * @param table the table to be created
99+ * @throws SQLException
100+ */
101+ private void createTable (MigratableStorage table ) throws SQLException {
102+ if (!tableExists (table )) {
103+ table .createTable ();
104+ try (Connection con = storage .getDataSource ().getConnection ()) {
105+ insertMigration (con , table , table .getRequiredVersion ());
106+ }
107+ }
108+ }
109+
87110 /**
88111 * Check the current version of a table stored in the connected database
89112 * @param table the table to check
@@ -106,7 +129,7 @@ protected int getCurrentTableVersion(MigratableStorage table) {
106129 }
107130
108131 // special case: table premium was created before the migration manager
109- if (version == 0 && "premium" .equals (table .getTableName ()) && tableExists ( table ) ) {
132+ if (version == 0 && "premium" .equals (table .getTableName ())) {
110133 version = 1 ;
111134 }
112135
@@ -119,18 +142,13 @@ protected void migrateTable(MigratableStorage table) {
119142 for (int i = initialVersion ; i < table .getRequiredVersion (); i ++) {
120143 core .getPlugin ().getLog ().info ("Starting database migration of table {} to version {}" ,
121144 table .getTableName (), i + 1 );
122- try (Connection con = storage .getDataSource ().getConnection ();
123- PreparedStatement saveStmt = con .prepareStatement (INSERT_MIGRATION )) {
145+ try (Connection con = storage .getDataSource ().getConnection ()) {
124146 for (String statement : getMigrationStatement (table , i )) {
125147 try (Statement migrateStmt = con .createStatement ()) {
126148 migrateStmt .executeUpdate (statement );
127149 }
150+ insertMigration (con , table , i + 1 );
128151 }
129-
130- // add entry to migrations table
131- saveStmt .setString (1 , table .getTableName ());
132- saveStmt .setInt (2 , i + 1 );
133- saveStmt .executeUpdate ();
134152 } catch (SQLException sqlEx ) {
135153 core .getPlugin ().getLog ().error ("Failed to migrate table {} to version {}" ,
136154 table .getTableName (), i + 1 , sqlEx );
@@ -141,6 +159,22 @@ protected void migrateTable(MigratableStorage table) {
141159 }
142160 }
143161
162+ /**
163+ * Add an entry to migrations table
164+ * @param con an open connection that was used to execute the migration statements
165+ * @param table the table that was migrated
166+ * @param newVersion the version the table was migrated to
167+ */
168+ private void insertMigration (Connection con , MigratableStorage table , int newVersion ) {
169+ try (PreparedStatement saveStmt = con .prepareStatement (INSERT_MIGRATION )) {
170+ saveStmt .setString (1 , table .getTableName ());
171+ saveStmt .setInt (2 , newVersion );
172+ saveStmt .executeUpdate ();
173+ } catch (SQLException e ) {
174+ throw new RuntimeException (e );
175+ }
176+ }
177+
144178 /**
145179 * Get an SQL statement to migrate the table to the next version
146180 * @param table the database table to be migrated
0 commit comments