Changeset 5853
- Timestamp:
- Nov 7, 2011, 2:50:41 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/HibernateUtil.java
r5689 r5853 592 592 @return The schemaVersion that is currently installed 593 593 */ 594 static int createStaticTables( boolean update, ProgressReporter progress)594 static int createStaticTables(SchemaGenerator.Mode mode, ProgressReporter progress) 595 595 throws BaseException 596 596 { … … 598 598 599 599 int currentSchemaVersion = Install.NEW_SCHEMA_VERSION; 600 if ( update)600 if (mode == SchemaGenerator.Mode.UPDATE) 601 601 { 602 602 currentSchemaVersion = Application.getSchemaVersion(); … … 627 627 try 628 628 { 629 SchemaGenerator schemaGenerator = new SchemaGenerator(cfg, dialect, dbEngine, update, progress);629 SchemaGenerator schemaGenerator = new SchemaGenerator(cfg, dialect, dbEngine, mode, progress); 630 630 session = HibernateUtil.newSession(); 631 631 tx = HibernateUtil.newTransaction(session); -
trunk/src/core/net/sf/basedb/core/Install.java
r5833 r5853 63 63 import net.sf.basedb.core.data.MappingCoordinate; 64 64 import net.sf.basedb.core.data.PluginTypeData; 65 import net.sf.basedb.core.hibernate.SchemaGenerator; 65 66 66 67 import net.sf.basedb.core.SessionControl; … … 119 120 public static final int NEW_SCHEMA_VERSION = Integer.valueOf(100).intValue(); 120 121 121 public static synchronized int createTables( boolean update, ProgressReporter progress,122 public static synchronized int createTables(SchemaGenerator.Mode mode, ProgressReporter progress, 122 123 String rootLogin, String rootPassword) 123 124 throws BaseException … … 129 130 Application.start(true, false, false); 130 131 131 if ( update)132 if (mode == SchemaGenerator.Mode.UPDATE) 132 133 { 133 134 Update.verifyRootLoginAndPassword(rootLogin, rootPassword); … … 135 136 136 137 if (progress != null) progress.display(0, "Building database..."); 137 schemaVersion = HibernateUtil.createStaticTables( update, progress);138 schemaVersion = HibernateUtil.createStaticTables(mode, progress); 138 139 message = "Database built successfully."; 139 140 } … … 159 160 will not be modified. 160 161 161 @param update TRUE if the run is an update, FALSE otherwise.162 @param mode The installation mode 162 163 @param progress An object implementing the {@link ProgressReporter} 163 164 interface … … 166 167 @throws BaseException This exception is thrown if there is an error 167 168 */ 168 public static synchronized void initDatabase( boolean update, ProgressReporter progress, String rootLogin, String rootPassword)169 public static synchronized void initDatabase(SchemaGenerator.Mode mode, ProgressReporter progress, String rootLogin, String rootPassword) 169 170 throws BaseException 170 171 { … … 178 179 Application.start(false, false, false); 179 180 session = HibernateUtil.newSession(); 181 boolean update = mode == SchemaGenerator.Mode.UPDATE; 180 182 181 183 // SchemaVersion -
trunk/src/core/net/sf/basedb/core/dbengine/AbstractDbEngine.java
r5852 r5853 23 23 24 24 import java.util.regex.Pattern; 25 26 import net.sf.basedb.core.hibernate.SchemaGenerator; 25 27 26 28 import org.hibernate.dialect.Dialect; … … 193 195 statements that doesn't supply a default value for the new column 194 196 when updating. 195 @since 2.16197 @since 3.1 196 198 */ 197 199 @Override 198 200 public String inspectSchemaGenerationSQL(String sql, Dialect dialect, 199 boolean update)201 SchemaGenerator.Mode mode) 200 202 { 201 203 String addColumn = dialect.getAddColumnString(); 202 if (update && sql.contains("alter table") && sql.contains(addColumn) && 204 if (mode == SchemaGenerator.Mode.UPDATE && 205 sql.contains("alter table") && sql.contains(addColumn) && 203 206 sql.contains("not null") && !sql.contains("default")) 204 207 { -
trunk/src/core/net/sf/basedb/core/dbengine/DbEngine.java
r5852 r5853 363 363 @param sql The original SQL statment as generated by Hibernate 364 364 @param dialect The Hibernate dialect currently in use 365 @param update TRUE if we are updating an existing database, 366 FALSE if we are creating a new one 365 @param mode The installation mode 367 366 @return The SQL statement which may have been modified or null 368 367 to skip executing the statment 369 @since 2.16370 */ 371 public String inspectSchemaGenerationSQL(String sql, Dialect dialect, boolean update);368 @since 3.1 369 */ 370 public String inspectSchemaGenerationSQL(String sql, Dialect dialect, SchemaGenerator.Mode mode); 372 371 373 372 } -
trunk/src/core/net/sf/basedb/core/dbengine/PostgresDbEngine.java
r5852 r5853 23 23 24 24 import java.util.Set; 25 26 import net.sf.basedb.core.hibernate.SchemaGenerator; 25 27 26 28 import org.hibernate.dialect.Dialect; … … 243 245 with unique names. PostgreSQL users should always run the ./updateindexes.sh 244 246 tool which will create the missing indexes. 245 @since 2.16247 @since 3.1 246 248 */ 247 249 @Override 248 250 public String inspectSchemaGenerationSQL(String sql, Dialect dialect, 249 boolean update)250 { 251 sql = super.inspectSchemaGenerationSQL(sql, dialect, update);251 SchemaGenerator.Mode mode) 252 { 253 sql = super.inspectSchemaGenerationSQL(sql, dialect, mode); 252 254 if (sql != null && sql.startsWith("create index")) 253 255 { 254 256 sql = null; 255 257 } 258 259 if (sql != null && mode == SchemaGenerator.Mode.MIGRATE) 260 { 261 if (sql.startsWith("alter table") && sql.contains("add constraint") && sql.contains("foreign key")) 262 { 263 // Skip foreign key creation 264 sql = null; 265 } 266 else if (sql.contains("unique")) 267 { 268 // Do not add "unique" contstraints 269 // Variant 1: ... , unique (column, column) ... 270 sql = sql.replaceAll(", unique \\([^)]*\\)", ""); 271 // Variant 2: ... column ... unique, .... 272 sql = sql.replaceAll("unique,", ","); 273 } 274 275 } 276 256 277 return sql; 257 278 } -
trunk/src/core/net/sf/basedb/core/dbengine/TableInfo.java
r5852 r5853 79 79 { 80 80 List<Table> tables = new LinkedList<Table>(); 81 ResultSet result = metaData.getTables(catalog, schema, null, n ull);81 ResultSet result = metaData.getTables(catalog, schema, null, new String[] { "TABLE" }); 82 82 while (result.next()) 83 83 { … … 85 85 String tableCatalog = result.getString("TABLE_CAT"); 86 86 String tableSchema = result.getString("TABLE_SCHEM"); 87 88 //System.out.println(tableName + ":" + result.getString("TABLE_TYPE")); 89 87 90 Table table = new Table(); 88 91 table.setName(tableName); -
trunk/src/core/net/sf/basedb/core/hibernate/SchemaGenerator.java
r5427 r5853 56 56 private final Dialect dialect; 57 57 private final DbEngine dbEngine; 58 private final boolean update;58 private final Mode mode; 59 59 private final ProgressReporter progress; 60 60 … … 65 65 @param dialect The dialect of the database we are connecting to 66 66 @param dbEngine The DbEngine for the database we are connecting to 67 @param update TRUE if we are updating an existing database, FALSE if 68 we are creating a new one 67 @param mode The installation mode 69 68 @param progress An optional progress reporter 70 69 */ 71 public SchemaGenerator(Configuration cfg, Dialect dialect, DbEngine dbEngine, boolean update, ProgressReporter progress)70 public SchemaGenerator(Configuration cfg, Dialect dialect, DbEngine dbEngine, Mode mode, ProgressReporter progress) 72 71 { 73 72 this.cfg = cfg; 74 73 this.dialect = dialect; 75 74 this.dbEngine = dbEngine; 76 this. update = update;75 this.mode = mode; 77 76 this.progress = progress; 78 77 } … … 94 93 { 95 94 String[] allSql = null; 96 if ( update)95 if (mode == Mode.UPDATE) 97 96 { 98 97 log.info("Fetching database metadata"); … … 109 108 for (String sql : allSql) 110 109 { 111 String actualSql = dbEngine.inspectSchemaGenerationSQL(sql, dialect, update);110 String actualSql = dbEngine.inspectSchemaGenerationSQL(sql, dialect, mode); 112 111 if (actualSql != null) 113 112 { … … 118 117 log.debug("Executing: " + actualSql); 119 118 stmt.executeUpdate(actualSql); 119 //System.out.println(actualSql); 120 if (progress != null) 121 { 122 if (numDone % interval == 0) 123 { 124 progress.display((100 * numDone) / numStatements, "--" + StringUtil.trimString(actualSql, 45)); 125 } 126 else 127 { 128 progress.append("."); 129 } 130 } 120 131 } 121 132 else 122 133 { 123 134 log.debug("Ignoring: " + sql); 124 }125 if (progress != null)126 {127 if ((numDone % interval == 0) && (actualSql != null))128 {129 progress.display((100 * numDone) / numStatements, "--" + StringUtil.trimString(actualSql, 45));130 }131 else132 {133 progress.append(".");134 }135 135 } 136 136 numDone++; … … 158 158 } 159 159 160 /** 161 The installation mode. 162 @since 3.1 163 */ 164 public static enum Mode 165 { 166 /** 167 Creating a fresh installation. 168 */ 169 INSTALL, 170 /** 171 Updating an existing installation. 172 */ 173 UPDATE, 174 /** 175 Creating a minimal database schema for migration. The 176 schema should be created without foreign key and 177 other constraints. 178 */ 179 MIGRATE 180 } 181 160 182 } -
trunk/src/install/net/sf/basedb/install/InitDB.java
r5852 r5853 36 36 import net.sf.basedb.core.Update; 37 37 import net.sf.basedb.core.Version; 38 import net.sf.basedb.core.hibernate.SchemaGenerator; 38 39 import net.sf.basedb.util.ConsoleProgressReporter; 39 40 import net.sf.basedb.util.ChainedProgressReporter; … … 67 68 String rootLogin = args.length == 3 ? args[1] : "root"; 68 69 69 boolean update = "update".equals(cmd); 70 SchemaGenerator.Mode mode = "update".equals(cmd) ? 71 SchemaGenerator.Mode.UPDATE : SchemaGenerator.Mode.INSTALL; 70 72 ChainedProgressReporter progress = new ChainedProgressReporter(new ConsoleProgressReporter()); 71 73 progress.setRange(0, 30); 72 74 showDbInfo(progress); 73 int schemaVersion = Install.createTables( update, progress, rootLogin, rootPassword);75 int schemaVersion = Install.createTables(mode, progress, rootLogin, rootPassword); 74 76 progress.setRange(30, 50); 75 if ( update)77 if (mode == SchemaGenerator.Mode.UPDATE) 76 78 { 77 79 if (schemaVersion < 99) … … 85 87 } 86 88 progress.setRange(50, 75); 87 Install.initDatabase( update, progress, rootLogin, rootPassword);89 Install.initDatabase(mode, progress, rootLogin, rootPassword); 88 90 progress.setRange(75, 90); 89 if (update) Update.updateDatabase(progress, rootLogin, rootPassword); 91 if (mode == SchemaGenerator.Mode.UPDATE) 92 { 93 Update.updateDatabase(progress, rootLogin, rootPassword); 94 } 90 95 progress.setRange(95, 100); 91 96 progress.display(0, "Installing web application..."); … … 141 146 if (export) 142 147 { 143 Application.start(false);144 148 Migration.exportAll(path, new ConsoleProgressReporter(false)); 145 149 } 146 150 else 147 151 { 152 ChainedProgressReporter progress = new ChainedProgressReporter(new ConsoleProgressReporter()); 148 153 // Create empty database without indexes and foreign keys 154 progress.setRange(0, 5); 155 Install.createTables(SchemaGenerator.Mode.MIGRATE, progress, null, null); 156 progress.append("\n"); 157 149 158 // Import all data 159 progress = new ChainedProgressReporter(new ConsoleProgressReporter(false)); 160 progress.setRange(5, 90); 161 Migration.importAll(path, progress); 162 150 163 // Add indexes, foreign keys, etc. to database 151 164 }
Note: See TracChangeset
for help on using the changeset viewer.