Working migrations script from resources
This commit is contained in:
		
							parent
							
								
									aa1fdbeb2a
								
							
						
					
					
						commit
						9665dec8ae
					
				
					 3 changed files with 73 additions and 0 deletions
				
			
		|  | @ -49,6 +49,28 @@ public class Database { | |||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     // Reads a file from the resources folder and returns the content as a string | ||||
|     private String readResourceFile(String fileName) { | ||||
|         String content = ""; | ||||
|         try { | ||||
|             content = new String(getClass().getResourceAsStream(fileName).readAllBytes()); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         return content; | ||||
|     } | ||||
| 
 | ||||
|     public void runMigration() { | ||||
|         // This path needs to start with a / | ||||
|         String migration = readResourceFile("/migration.sql"); | ||||
|         try { | ||||
|             Statement stmt = conn.createStatement(); | ||||
|             stmt.execute(migration); | ||||
|         } catch (SQLException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
|          | ||||
|     /** | ||||
|      * Close the connection to the database. | ||||
|  |  | |||
|  | @ -48,6 +48,7 @@ public class MainApplication extends Application { | |||
| 	            alert.setContentText("Could not connect to the database! Check console for details."); | ||||
| 	            alert.showAndWait(); | ||||
| 	        } | ||||
| 			db.runMigration(); | ||||
| 		} catch(Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus