Database migrations in Grails - activerecord

Suppose I have a database containing 3 tables in a grails application:
User
Activity
Friend
User table table has one to many relation to Activity and Friend tables so in User table I have:
static hasMany = [activies: Activity, friends: Friend]
and in Friend and Activity I have:
static belongsTo User.
Application is deployed and lets says thousands of customers have registered. Now changes are made in the database: Table Activity is dropped. A table Journal is created which is on the many sides of the User table. The User table has a new column added and this column cannot be null. An old column in Friend table is deleted that was also defined as not null.
So given above scenario and assume using MySQL what needs to be done to make above changes without deleting existing customers data and safely add the new table to existing customers?
Ruby on Rails comes with ActiveRecord for database migrations. Does Grails comes with something like this out of the box?
Currently in development when I run my grails application after adding a new not null column to a table, I get column cannot be null exception thrown unless I delete that table in the database before running grails application which would recreate the table if not exists. Obviously once the application is deployed I will not have the luxury to delete the table.

Unfortunately, the current version of Grails doesn't come with database migration. However, there is a plugin for Liquibase which makes migrations possible.
The next version of Grails (1.4, planned for Q1 2011) will supposedly contain a built-in migration tool, which I am very much looking forward to.
Note: I haven't used the Liquibase plugin, so I don't have any firsthand experience with it. I have seen numerous blog posts describing its use, however, and I'm probably going to use it in my next Grails project if 1.4 isn't out by then.

Related

Add columns to a table managed by Sequel Model

I'm using Sequel::Model. My model has a set_schema block and a create_table call, so the database is completely managed by the model. The database is sqlite.
I'm trying to add columns to the database and I can't find a way to. Adding fields to the schema has no effect. Adding a migration or an alter_table call doesn't do anything. I've asked on IRC and read the docs. I can't find an example of anyone doing this, but it seems simple.
How do I add a column/field to a Sequel Model?
I did a bunch of research and talked to Jeremy Evans. I was going about this wrong. The right way to do this was to remove my schema plugin code and create_table block and move to using migrations. The steps I went through were:
Remove the schema code (create_table, set_schema) from my modesl
Dump the schema from my current sqlite data files into initial migrations into migration files via the sequel -d command
Create a new migration that adds the columns I need via add_column calls in an alter_table block in a change block
Apply the migrations via the sequel -m command
Create rake tasks to run the migrations and hook those tasks into my deploy tasks

Entity Framework 4.3.1 Migrations - enable automatic migration and creating a new database in the same time

I have played with Entity Framework 4.3 migrations for some time now, but I have trouble achieving the next behavior: In case my code runs on an existing database, I want the database to be migrated automatically to the latest version, but in case the database doesn't exist, the database should be created automatically from the migrations.
I think the problems is related to the first migration that you create. If you create the first migration using -IgnoreChanges parameter (or manually delete them as explained here: http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/), you will not be able to use migrations in order to create a new table using the DbMigrator class. because you don't have the initial migration. If you create the first migration without using -IgnoreChanges, than the migration of the existing database will not be possible. Does anybody has any solution for this problem?
So you have existing database and you want to use migrations on that database and in the same time you want to support database creation by migrations in case of new deployment?
It looks like little bit unsupported use case. The simplest in this case (not tested) would be either conditional compilation or conditional migration driven by some AppSettings key. It means creating initial migration as if you don't have the database and modify Up method to:
public override void Up() {
if (ConfigurationManager.AppSettings["NewDatabaseRequired"] == "true") {
// Here is generated content
}
}
or
public override void Up() {
#if NewDatabaseRequired
// Here is generated content
#endif
}
There is plenty of other more complicated options including scripting your current database, modify the script to end if tables already exist, add a script as a resource to your migration assembly and execute the script in the Up method generated with -IgnoreChanges.
As another option you can open additional database connection and check if tables from migration already exists (by querying sys.tables view in SQL Server). This will not need generated script.

MVC3 EDMmetadata table not found

I am new in MVC3 i am going to create a MVC3 test project where i am create model class name WhiteAccount with ID,Name,Email,Password property. and successfully create a DB but when i add another new property in that WhiteAccount model class and in my DB table too but it give me some error. Some people say just delete the EDMmetadata table from your DB, But Here is the problem i have no EDMmetadata table in my DB ! I create my DB by EntityFramework v4.3.1 system automatically (CodeFirst). What should i do now ?
Check for the __MigrationHistory table.
Open the nuget package manager console and run
update-database -script
It will likely give you a message about having to enable it first, follow those directions
Run: Enable-Migrations
some more info on migrations
http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
Im gathering this table is there in your db (under system tables) and it contains your model information. Since your project changed you need to tell the migrations about the new field or delete the table (__MigrationsHistory)

InnerException {"Invalid object name 'dbo.Users'"}

I am currently trying to setup an ASP.Net MVC 3 Application for the first time. I know what I am doing with Ruby on Rails but completely lost with ASP.Net. I would like the app to create the database but not sure what I am doing wrong. I have an empty Database build called ssDB. I have setup the connectionStrings and built out the Model classes. According to everything I have read thus for, when I go to the run the App it should create the database but it doesn't look that way.
The Abbreviated Error I get is:
InnerException: System.Data.SqlClient.SqlException
Message=Invalid object name 'dbo.Users'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=208
Procedure=""
Server=TESTSVR01
State=1
I have an empty Database build called ssDB
You have created an empty database with no tables. Since there is a database EF assumes tables are also created. Hence it will not proceed to create the tables.
You can drop the database and allow EF to create the database and the tables.
I also experienced the same issue while using Database first approach. Connection string in my service's/service's host web.config was incorrect. I corrected the connection string and it worked.

INSERT fails on EF 4.1 Code First Database

I am using Visual Studio 2010 SP1 with MVC3 Tools Update, and EF 4.1 Code First to scaffold my application's database.
Database CRUD operations work fine when testing locally and on a local database. It is not until I migrate the database to web host's SQL Server 2008 R2 and target that hosted database in my connection string that all INSERT operations fail.
Whenever I try to add a new record I get this error: [I]Cannot insert the value NULL into column 'Id', table 'DB_23378_bloomlmsdata.dbo.Courses'; column does not allow nulls. INSERT fails. The statement has been terminated.[/I]
My data models all specify a Primary Identity Key like so:
[Key]
public int Id {get; set;}
...
And in the local database that gets scaffolded, I see a non-nullable Primary Identity Key in each table.
I read somewhere that Entity Framework, when performing an INSERT operation to the database it will first try to insert a record with a null value as the Primary Key. I do not know how to override this behaviour. Also, this does not seem to be a common problem and people usually only run in to this when they use something different than an IDENTITY key. For the record, I had the same problem with EF CodeFirst CTP5 before EF 4.1 was released.
I have been troubleshooting this for 4 weeks.. My web host is up to date with the technology I am using in my application and database. When I try to get help, they are telling me it is a coding issue but my code checks out. Any ideas would be greatly appreciated.
Check the IsIdentity property of the Id column, it should be set to yes. EF doesn't pass a value for the identity column on an insert because it should be auto-generated by the database.

Resources