-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto add columns. #266
Comments
What do you mean? PS: Have you considered designing your database in a Yaml file and then use code generation to create the actual code? |
Yeah right now we don't have a SQL update mechanism. It would need both the previous and new versions of the fields. Actually @noordawod, we could do this if we added some sort of revision number. So each field would have a int revNumber and the user would be able to ask for a list of field adds and drops when going from rev X to rev Y. That would be cool. I guess changing a field would be difficult. So if you changed a field from and int to a long and increased the revNumber then it would get confused. Hrmmmmm. Yeah add and drops are easy but any sort of alter isn't immediately apparent how it would be done. |
no |
I am currently using this method to add new columns. It is relatively safe because it does not remove anything, it only adds columns when it is needed. Yes this code is kinda shitty, but it is only temporary solution. public <T> void updateColumns(Class<T> tClass) {
this.action(tClass, (dao, connectionSource) -> {
TableInfo<T, Object> tableInfo = dao.getTableInfo();
DatabaseType databaseType = connectionSource.getDatabaseType();
Map<String, String> columns = new HashMap<>();
for (FieldType fieldType : tableInfo.getFieldTypes()) {
if (fieldType.isForeignCollection()) {
continue;
}
String columnDefinition = fieldType.getColumnDefinition();
StringBuilder sb = new StringBuilder();
if (columnDefinition == null) {
databaseType.appendColumnArg(tableInfo.getTableName(), sb, fieldType, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
} else {
databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
sb.append(' ').append(columnDefinition).append(' ');
}
Object defaultValue = fieldType.getDefaultValue();
if (defaultValue != null) {
sb.append("DEFAULT").append('(').append(defaultValue).append(')');
}
columns.put(fieldType.getColumnName(), sb.toString());
}
try (DatabaseConnection dbConnection = dao.startThreadConnection(); Connection connection = dbConnection.getUnderlyingConnection()) {
for (Map.Entry<String, String> entry : columns.entrySet()) {
String column = entry.getKey();
String columnAndType = entry.getValue();
try (ResultSet tables = connection.getMetaData().getColumns(null, null, dao.getTableName(), column)) {
if (tables.next()) {
continue;
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("ALTER TABLE `" + dao.getTableName() + "` ADD " + columnAndType);
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
});
} |
Yeah that's a good idea as well @Rollczi . Get all of the columns in the database and add any that aren't there. I'm not sure if it would be possible to change the types of the columns however. |
Changing column type is dangerous idea. I propose to add only feature to automatically add columns if it doesn't exists. |
basically you want a automigration feature. that is not available in ormlite. infact hardly do any light weight orm can provide this feature. but there is an work around i use. you need to create your own db version control.
you can add a table using these methods
to execute these queries use ConnectionSource executeStatement or execute method. i am not use the exact method name there is something like that to execute raw queries on connection source. |
[00:02:19 WARN]: java.sql.SQLException: Column 'name2' not found.
is there an option to automatically add new columns?
The text was updated successfully, but these errors were encountered: