how to handle schema change if using sorm - database

I am new to OR-mapping techniques. I wonder if I'm using SORM to persist data to DB, how do I handle future database schema changes?
For example,
here is the User class :
case class User(name:String, age:Int)
what if in the future I want to add one more field like gender
do I need to manually change existing tables, or does the SORM helps me do this
what's the default value for the new field
what if I want to add a non-null field?
Or it is not easy to change data schema using SORM? That seems very restricted. What's the best method to handle database schema change?

Implement a migration. Have your original model and the updated one connect to associated DBs and use them to migrate all the data from the original model to the new one. You can drop the original database afterwards.
You can implement it either as a script or some form of a migration strategy-resolving function. E.g., you could have a table bearing the current schema version information, which you could use to determine how to migrate the database to the current version.
It, of course, won't be quite as fast as manual SQL alteration, but then it's the compromise of all ORMs.

Related

asp.net code first automatic database updates

I am creating an application in C# Asp.net using Code First Entity Framework that will be using a different databases for different customers (in other words every customer has its own database, that will be generated on first time use).
I am trying to figure out a way to update all these databases automatically whenever I apply changes to my objects. In other words, how would I approach a cleanstep system in Code First EF?
Currently I am using InitializerIfModelChange to define a simple database that allows me to test my application whenever a schema change occurs. However, this method drops the database, which obviously is unacceptable in case of customer databases.
I must assume hundreds of customers so updating all databases by hand is not an option
I do not mind writing code that copies the data into a new database.
I think the best solution would be a way to backup a database somehow and then reinsert all data into the newly created database. Even better would be a way that automatically updates the schema without dropping the database. However I have no idea how to approach this. Can anyone point me in the right direction?
The link posted by Joakim was helpful. It requires you to update to EF 4.3.1 (dont forget your references in other projects if you have them) after which you can run the command that enables the migration. To automatically update the schema from code you can use
Configuration configuration = new Configuration();
DbMigrator migrator = new DbMigrator(configuration);
migrator.Update();
Database.SetInitializer<DbContext>(null);

What is the best way to record the edit history in a database

I am developing a system with Java EE and JPA where users can make changes to entities. It is needed to trace back to the changes when needed. So the all the changes and the user have to be recorded for each occasion when en update is made. What is the best way to record the changes.
For example, there is an Entity called Investigation. It has attributes like Name, Category, Price, Volume, etc. A user can search a single investigation and change the name in one instance and in another instance, another user can change the price. All these occasions with the change done and the user who did it is needed to be traced back when needed.
One method described in this link is that to label objects as old edited and create a new object with updated values, but the problem there are several other objects from different entities referring to the old one.
Another method as described in this link is to use a versioning field in a new table. Than can be achieved in JPA by creating a new entity that extends the main entity.
Out of these methods what is the best practice? Is there any other optimized way to keep the record editing history in Java Persistence?
EclipseLink supports history.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/History
If you don't mind using Hibernate, Envers might be interesting for you. It performs auditing automatically, optionally appending metadata like current user.
For each audited entity it creates a history table that holds previous versions.

Table for History of changes

I am working in asp.net MVC 3 Website and I need to keep track of any changes made to a table/entity. Whenever on Edit view something is modified, a list of changes will display with date, changes made columns below that Edit view. Do I need to create another table with entityHistory Name or I need to insert another record in same table for that ?
Please suggest
Depends what you want to do with the history data. If you want to show the record or object graph snapshots I have found creating a History table, with the same columns as the current table, easier to work with in building up how the complete record looked after or before a certain change. This also means that you'll have duplicated tables and data.
If your needs is a pure audit requirement it is easier to have one/two tables that holds data for entity, property, old value and new value columns.
Besides Audit options, SQL Server has now CDC (Change Data Capture in SQL2008) feature which enables developers to trace data changes on a sql table
You can build a similar logging mechanism by using triggers (refer to http://www.kodyaz.com/articles/sql-trigger-sql-server-trigger-example-to-log-changes-history.aspx for a sample)
You can also check the following article for an enhanced solution for logging data changes similar to CDC in SQL2005 http://www.kodyaz.com/articles/log-data-changes-using-change-data-capture-for-sql-server-2005.aspx

django manual database migration

I am preferring to manually migrate my tables in Django. Because using automated tools puts me in a place where I cannot see the impact. With impact, I mean the the time it takes the db get in synch with my models. Below is a simple example:
class User(models.Model):
first_name = CharField(..)
Let's say I want to add this:
class User(models.Model):
first_name = CharField(..)
last_name = CharField(..)
I will follow the these steps in my production server:
Disable site traffic.
Manually connect to the your DB server, let's say MySQL and add a field to the User table named last_name (make sure it is sync with the SQL generated for the new Model, of course.)
Update your model.
Upload new files, restart traffic.
I have two questions for this scenario:
Is this a preferred/acceptable way for manual db migration in Django?
If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?
Thanks in advance,
With all of the schema migration tools, such as south, there are ways of explicitly defining how your models get migrated. The benefits of using a tool such as this are:
Your migrations are stored in your version control system
There's a documented procedure to roll back schema migrations
If another developer joins your project, you can refer that person to the south documentation rather than explaining your own hacky solution to documenting schema migrations.
I think I should just emphasize a point here: Though south has automigration tools, you don't have to use automigration if you're using South.
Is this a preferred/acceptable way for manual db migration in Django?
I would answer no. As #Mike said Django has a reliable and fairly versatile ecosystem of migration tools, the most prominent of which is South. #Mike's answer has the details right.
To answer your second question:
If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?
No. Your models will continue to function normally. Of course if you want to do something with the new fields using Django's ORM you'll be better off adding them to the model class.
A side effect of this is that you can migrate legacy database tables by selectively choosing the fields to use in your models.

Should I clone or denormalize my database for portable use?

I have a database that has lots of data and is all "neat", normalized (within reason - using EAV), and I have stored procedures to access and modify the data.
I also have a WinForms application that users download to search and view this data (no inserts). To make things handy for use and updates, I've been using SQLite to store this data and it works really well.
I'm working on updating the entire process and I was wondering if I should use a denormalized view of the data to ship out to the users, ala the 1 table with all the properties as columns, or continue to use the same schema as the master database?
My initial thoughts are along the lines of :
Denormalized View:
Benefits...
Provides a simple method of querying the data (since I'm not doing a lot of joins, just a bunch of column searching.
Cons...
I'd have to manage a second data access layer. Granted I don't think it will be difficult, but it is still a bit more work.
If a new property is added, I'd have to modify the schema again and accomodate for the changes. Wheras I can simply query the property bag and work form there.
Same Schema:
Pros...
Same layout as master database, so updates are minimal, and I can even use the same queries when building my Data Access Layer since SQLite doesn't support stored procedures.
Cons...
There is a lot of small tables for lookup codes and the like, so I could start running into issues when building the queries and managing it in the DAL.
How should I proceed?
If you develop your application to query views of the data rather than the underlying data itself, you will be able to keep the same database for both scenarios without concern or the need to alter your DAL.

Resources