We have an Android-Application communicating with a MySQL database via SOAP.
Now we were forced to create a webpage with Grails and of course we want to use the same database.
But how can we tell Grails to use our database structure for the domains?
Is there a way to merge these systems?
(The connection to the MySQL-Database is already established, but the two structures do not work together)
e.g. (in the least complicated case) we have a table "locations" with just one column 'name' which is PK. Grails would create a structure for the domain "location" with three columns 'id' 'version' and 'name'.
Grails creators had already thought about you and your case which is a common scenario when someone tries to move to grails with an existing enterprise infrastructure.
You need to have a look at db reverse engineer plugin which creates domain classes based on the existing table structure. You can use the domain classes once created by the plugin.
You can access your MySQL db by providing the datasource as such. In general, company wide datasource would be maintained (or you can create one if required), and use the datasource in Datasource.groovy.
GORM allows you a lot of configurations, you can disable the version control, change your primary key mapping and so on. In your example:
class Locations {
String name
static mapping = {
id column: 'name' //change the id from "id" to name
version false //remove version control, so it will not be added to your table
}
}
Related
I have a scala application that manages multiple MySQL database schemas, which includes modifying (adding, renaming, etc.) tables. The commands are issued over a connection pool that connects to a generic management database in the database server.
Because the application is designed to be cross-database, I use JOOQ to render SQL queries (execution is done via a separate JDBC module).
I experience issues with JOOQs alterTable(...).renameTo(...) DSL - consider the following example:
We have a table "TestTable" in database "TestDatabase". Let's say I want to rename that table simply to "Foo", keeping it in "TestDatabase".
This code:
...
val context = DSL.using(SQLDialect.MYSQL_5_7)
val query = context
.alterTable(table(name("TestDatabase", "TestDatabase")))
.renameTo(name("TestDatabase", "Foo"))
...
Generates: ALTER TABLE `TestDatabase`.`TestTable` RENAME TO `Foo`
However, since the connection pool I'm using is connected to my management database, it just renames the table to "Foo" and moves it to my management database. I would have expected the SQL to be: ALTER TABLE `TestDatabase`.`TestTable` RENAME TO `TestDatabase`.`Foo`. I tried a variety of alternatives to invoke the .renameTo method and convice it to use the fully qualified name, to no avail:
.renameTo(table(name(...) -> same behaviour.
.renameTo("`TestDatabase`.`Foo`") -> Escapes the name with backticks, treats it as one name instead of a qualified name.
I'm wondering if I'm missing something, if this is intended behaviour, or maybe even a bug or design shortcoming of JOOQ.
Is there a way to rename the table using fully qualified names?
Thank you!
That's a bug in jOOQ: https://github.com/jOOQ/jOOQ/issues/8042
Your workaround is close. This doesn't work:
.renameTo("`TestDatabase`.`Foo`")
As you've noticed, behind the scenes, the DSL.name() API is used to wrap the target name, because the renameTo() method doesn't implement the plain SQL templating API. You can, however, explicitly use plain SQL templating by writing as a workaround:
.renameTo(table("`TestDatabase`.`Foo`"))
Propel ORM fails to query on the production environment because the database it connects to has case-sensitivity enabled (linux/ubuntu). Since the db is managed by another organization, it's harder to get it fixed than to make the changes to the code from our end.
Is there a way to force Propel ORM to use case-sensitive names?
Thanks!
Propel has a Map directory inside the generated files. The directory has all the mappings of table and column names which propel uses onto what MySQL uses.
We solved our problem by writing a simple python script that corrects the case-sensitivity as per our requirements.
Each file inside the Map directory corresponds to the mapping for one table. Our script opened each file one by one and replaced the each mapping with our specific requirement (which was to make table names upper case):
Here's the RegEx we used to match table name mapping:
"(\s*const TABLE_NAME = ')([a-zA-Z_]+)(';)"
RegEx for matching column mapping inside each file:
"(\s*\* the column name for the.*\s*\*\/.*\s*const [a-zA-Z_]+ = ')([a-zA-Z_]+)(\.[a-zA-Z_]+)(';)"
I have an existing application in grails using mysql database with much data. The previous programmer uses int as id and I need to change to long as I'm running out of ids. I know that the change in the domain class does not update the column of the existing table. Do I change the type in mysql manually?
There's this thing called database migrations... There's a plugin for it.
http://grails.org/plugin/database-migration
Yes, after changing the domain class change the column manually.
Also, I suppose it's a good idea first to set dbUpdate to e.g. "create-drop" and try it (on another DB instance) to let Grails generate new schema and to see whether it looks as you expect.
So, change the domain, generate test schema and check whether it is correct, then change the original DB manually.
Why are these important, what effect do they have on the database at hand?
Isn't the "schema" already decided at that point, as it exists in the database?
What exactly is a "catalog"? What kind of variable/input is the JPA catalog field expecting? Also, same question for the "schema" field.
Catalogs and schemas are "namespaces" that you define on the server side of the database. Some databases contains schemas, some contains catalogs, and some contains both. When logging in with a specific user, some databases defaults the schema/catalog to the user's namespace, causing the table to not be visible to other users, thus, causing the need to use a "common" namespace. So, depending on the database you are using to back your data, you might want to ignore those settings.
For MySQL, you might want to ignore those settings. This is because the "database" part of the JDBC URL (the one after the last slash) points to the database name, which is semantically identical to schema (for MySQL).
I know EF checks the EdmMetadata table to determine if the version of model classes is same as database tables.
I want to know exactly how EF can find if the version of a model has changed. In other words, I want to know what does EF compare to the modelhash in the database?
Have a look at this blog post about the EdmMetadata table.
For your question, this is the relevant parts:
The EdmMetadata table is a simple way for Code First to tell if the
model used to create a database is the same model that is now being
used to access the database. As of EF 4.1 the only thing stored in the
table is a single row containing a hash of the SSDL part of the model
used to create the database.
(Geek details: when you look in an EDMX file, the SSDL is the part of
that file that represents the database (store) schema. This means that
the EdmMetadata model hash only changes if the database schema that
would be generated changes; changes to the conceptual model (CSDL) or
the mapping between the conceptual model and the database (MSL) will
not affect the hash.)