Updating models in a Doctrine Migration - migration

I'm trying to update a model in the context of a Doctrine_Migration. Calling save() on the object doesn't seem to update the database. I also tried calling execute() a Doctrine_Query in the context of a Doctrine_Migration. I tried running getSqlQuery() on the query object and I get a valid query that works if executed in a mysql console, however if I just run the migration normally I get no errors and the execute() doesn't seem to do anything.
How can successfully execute() a query in the context of a migration?

$res = Doctrine_Query::create()->update('FooBar')->set('colFoo', '?', 'valBar')->execute();
This should do the trick

Related

Creating a Sequelize Dialect for new Database

I'm pretty new to sequelize, though I've worked on node previously I did not use any ORM framework. At present I'm using new SQL DB(which is not supported by sequelize ) and want to connect it using node.js and sequelize( popular ORM for node.js ) by prototyping the existing dialects
The configuration is correct as I've tried it wihtout ORM.
The problem is after configuring the connection with properties the
sequelize.authenticate() doesn't throw any error but doesn't return a promise back
/**
* Test the connection by trying to authenticate
*
* #error 'Invalid credentials' if the authentication failed (even if the database did not respond at all...)
* #return {Promise}
*/
authenticate(options) {
return this.query('SELECT 1+1 AS result', _.assign({ raw: true, plain: true }, options)).return();
}
The return statement doesn't return anything. I've read this post how to create a new dialect. Though it says it is not encouraged to create a new dialect and throws an error if we try to, I think there must be a way to create because if it can be created for other SQL databases then may be there should be a way to do it. This is an open source project on github. Did anyone previously work on this any help is appreciated. Thanks in Advance
Only the 5 dialects are supported and an error will be thrown if you try and use NewSQL.
There is a lot of code in Sequelize to construct queries based on the dialect, so even if you could get past the error (such as forking the repo and changing it) the likelihood of everything working as you expect (or as is documented) is low.
I suggest posting an issue on GitHub to bring that dialect to the project.

Asp.net database migration, what's the Down method for?

When I add-migration, Up and Down method are generated.
and I know when I update database (update-database), it runs the Up method.
How about Down method?
when it will run, is it for rollback? and, how can I run it?
Its for when you want to "downgrade" the database to a previous migration state. You use it with the -TargetMigration flag of the Update-Database command. For example, if you have added the following migrations:
Initial
FirstMigration
SecondMigration (current state)
You can revert the database to the Initial migration state by:
Update-Database -TargetMigration:Initial
In which case the code in the Down() methods of the SecondMigration and FirstMigration classes will run.

Grails transactions (not GORM based but using Groovy Sql)

My Grails application is not using GORM but instead uses my own SQL and DML code to read and write the database (The database is a huge normalized legacy one and this was the only viable option).
So, I use the Groovy Sql Class to do the job. The database calls are done in Services that are called in my Controllers.
Furthermore, my datasource is declared via DBCP in Tomcat - so it is not declared in Datasource.groovy.
My problem is that I need to write some transaction code, that means to open a transaction and commit after a series of successful DML calls or rollback the whole thing back in case of an error.
I thought that it would be enough to use groovy.sql.Sql#commit() and groovy.sql.Sql#rollback() respectively.
But in these methods Javadocs, the Groovy Sql documentation clearly states
If this SQL object was created from a DataSource then this method does nothing.
So, I wonder: What is the suggested way to perform transactions in my context?
Even disabling autocommit in Datasource declaration seems to be irrelevant since those two methods "...do nothing"
The Groovy Sql class has withTransaction
http://docs.groovy-lang.org/latest/html/api/groovy/sql/Sql.html#withTransaction(groovy.lang.Closure)
public void withTransaction(Closure closure)
throws java.sql.SQLException
Performs the closure within a transaction using a cached connection. If the closure takes a single argument, it will be called with the connection, otherwise it will be called with no arguments.
Give it a try.
Thanks James. I also found the following solution, reading http://grails.org/doc/latest/guide/services.html:
I declared my service as transactional
static transactional = true
This way, if an Error occurs, the previously performed DMLs will be rolled back.
For each DML statement I throw an Error describing the message. For example:
try{
sql.executeInsert("""
insert into mytable1 (col1, col2) values (${val1}, ${val2})
""")
catch(e){
throw new Error("you cant enter empty val1 or val2")
}
try{
sql.executeInsert("""
insert into mytable2 (col1, col2) values (${val1}, ${val2})
""")
catch(e){
throw new Error("you cant enter empty val1 or val2. The previous insert is rolledback!")
}
Final gotcha! The service when called from the controller, must be in a try catch, as follows:
try{
myService.myMethod(params)
}catch(e){
//http://jts-blog.com/?p=9491
Throwable t = e instanceof UndeclaredThrowableException ? e.undeclaredThrowable : e
// use t.toString() to send info to user (use in view)
// redirect / forward / render etc
}

See the SQL commands generated by EntityFramework: Cast exception

Based on this and this, I'm doing the following to get the SQL enerated by Entity Framework 5.0
var query = from s in db.ClassesDetails
where s.ClassSet == "SetOne"
orderby s.ClassNum
select s.ClassNum;
var objectQuery = (System.Data.Objects.ObjectQuery)query; // <= problem!
var sql = objectQuery.ToTraceString();
However on the second line I get the following exception:
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Int16]' to type 'System.Data.Objects.ObjectQuery'.
Did something change since those SO answers were posted? What do I need to do to get the queries as strings? We're running against Azure SQL so can't run the usual SQL profiler tools :(
ObjectQuery is created when you are using ObjectContext. When you are using DbContext it uses and creates DbQuery. Also, note that this is actually not a DbQuery but DbQuery<T>. I believe that to display SQL when having DbQueries you can just do .ToString() on the DbQuery instance so no cast should be required. Note that parameter values will not be displayed though. Parameter values were added to the output very recently in EF6 - if you need this you can try the latest nightly build from http://entityframework.codeplex.com

NHibernate: No persister for NHibernate.Criterion.SqlFunctionProjection

I am using NHibernate version 2.0.0.4000.
In one of my queries I wanted to make use of the sql function dateadd to add a number of days. This wasn't registered so I created my own dialect and registered the function as follows:
RegisterFunction("adddays",
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"dateadd(dd, ?1, ?2)"));
The registration gets hit and seems to work fine. I use the function in a DetachedCriteria query as follows:
...
Restrictions.LtProperty("DateColumn1"
Projections.SqlFunction("adddays", NHibernateUtil.DateTime,
Projections.Constant(days),
Projections.Property("DateColumn2"))
...
The criteria is returned from a method and passed of to another query. Upon the execution of the final query I get the following exception:
NHibernate.MappingException was caught
Message="No persister for: NHibernate.Criterion.SqlFunctionProjection"
Source="NHibernate"
StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName,
Boolean throwIfNotFound)
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
...
None of the blog posts that I have seen mention this problem. Can anybody help?
Cheers in advance.
Nige.
Solved it.
The problem was caused by my usage elsewhere of Restrictions.Eq rather than Restrictions.EqProperty. The former does not have an overload for (IProjection,IProjection) and so was treating the second projection as an object and passing it to a persister.
Thanks to anyone who investigated this.
Nigel.
For No Persister exception in nhibernet solution is
just give a right click on the respective hbm file ->Properties->change the type Build Action-> to Embedded Resource
with the above step application will working fine