(final String migrationResource)
| 253 | } |
| 254 | |
| 255 | public void createDB(final String migrationResource) { |
| 256 | if (migrationResource == null) |
| 257 | return; |
| 258 | try { |
| 259 | myVersion = 0; |
| 260 | try { |
| 261 | Integer v = queryOne("SELECT ivalue FROM vars WHERE name = 'version'"); |
| 262 | if (v != null) |
| 263 | myVersion = v; |
| 264 | } catch (SQLException e) { |
| 265 | if (e.getMessage().indexOf("vars") < 0) |
| 266 | e.printStackTrace(); |
| 267 | } |
| 268 | currentDbVersion = detectMaxMigrationVersion(migrationResource); |
| 269 | log.d("My db version is %d current is %d", myVersion, currentDbVersion); |
| 270 | while (myVersion < currentDbVersion) transaction(() -> { |
| 271 | log.d("Migrating to %d", myVersion + 1); |
| 272 | preMigrate(myVersion); |
| 273 | executeFile(migrationResource + myVersion + ".sql"); |
| 274 | postMigrate(myVersion); |
| 275 | myVersion++; |
| 276 | update("update vars set ivalue=? where name='version'", myVersion); |
| 277 | return null; |
| 278 | }); |
| 279 | } catch (RuntimeException e) { |
| 280 | throw e; |
| 281 | } catch (Exception e) { |
| 282 | e.printStackTrace(); |
| 283 | throw new RuntimeException("migrations failed", e); |
| 284 | } |
| 285 | log.d("Db migrated successfully"); |
| 286 | } |
| 287 | |
| 288 | private int detectMaxMigrationVersion(String migrationResource) { |
| 289 | if (migrationResource == null || migrationResource.length() == 0) |
no test coverage detected