Skip to content
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

Some Migration Questions #778

Closed
wilberolive opened this issue Oct 26, 2017 · 5 comments
Closed

Some Migration Questions #778

wilberolive opened this issue Oct 26, 2017 · 5 comments

Comments

@wilberolive
Copy link

wilberolive commented Oct 26, 2017

Hi I'm just wondering how I handle some simple migration scenarios when the data structure changes.

  1. I found this, which talks about changing a data type of a field. However the code seems to be different now and requires some sort of query. Is this still a valid solution for changing a field's data type?

  2. How do you handle changing the name of a field? So for example, I have a collection of items and the items class has a property called Price. However I would like to rename that property to Cost for example.

  3. How do you remove a field? Say if I remove the Price property entirely from the items class. I notice that the column seems to stay in the database. Can I remove the column somehow?

  4. I'm also wondering what happens if you change the order of properties within a collection's class. Does that matter at all?

I think you should add a page to the wiki that covers all of the common migration scenarios as it would be a common problem for any real world application.

@mbdavid
Copy link
Owner

mbdavid commented Oct 27, 2017

Hi @wilberolive,

  1. Yes, this tips are still valid for v4... You can use LiteEngine class to access your data using BsonDocument (an schemaless data structure). Using UserVersion you can control your database version.

  2. There is no rename key, so you need add a new field (using old value) and then remove old field.

using(var db = new LiteDatabase("..."))
{
if(db.Engine.UserVersion == 0)
{
foreach(var doc in db.Engine.Find("MyCol"))
{
doc["NewCol"] = Convert.ToInt32(doc["OldCol"].AsString);
doc.Remove("OldCol");
db.Engine.Update("MyCol", doc);
}
db.Engine.UserVersion = 1;
}
....
}

  1. To remove a field, just use Remove method in BsonDocument and execute Update.

  2. There is no problem with field order. All you class data will be serialized WITH fields, as JSON do. So, order doesn't matter.

You can also run shell commands to update your data without write any code. It's ideal when you have a single database to update. You can run

db.MyCol.update NewCol = OldCol
db.MyCol.update Fullname = UPPER(Last) + ', ' + UPPER(First)

Shell command also supported using Run method. Shell does not have all functions implementations and suggestion are welcome.

I will add more options in shell and then will write a wiki page

@wilberolive
Copy link
Author

Thanks for all the info. Will try all this out. Unfortunately I cannot use the Shell and have to do it through code as the database will reside on individual client machines that I don't have direct access to once the app is deployed. So anytime the data changes, I need the app itself to manage those changes and keep its own database updated.

@mbdavid
Copy link
Owner

mbdavid commented Oct 30, 2017

@wilberolive, you can use shell commands inside your code using Run method in LiteEngine. Sometimes are easy to modify you already exist data.

@lbnascimento
Copy link
Collaborator

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!

@gbthakkar
Copy link

Hi @wilberolive,

  1. Yes, this tips are still valid for v4... You can use LiteEngine class to access your data using BsonDocument (an schemaless data structure). Using UserVersion you can control your database version.
  2. There is no rename key, so you need add a new field (using old value) and then remove old field.

using(var db = new LiteDatabase("...")) { if(db.Engine.UserVersion == 0) { foreach(var doc in db.Engine.Find("MyCol")) { doc["NewCol"] = Convert.ToInt32(doc["OldCol"].AsString); doc.Remove("OldCol"); db.Engine.Update("MyCol", doc); } db.Engine.UserVersion = 1; } .... }

  1. To remove a field, just use Remove method in BsonDocument and execute Update.
  2. There is no problem with field order. All you class data will be serialized WITH fields, as JSON do. So, order doesn't matter.

You can also run shell commands to update your data without write any code. It's ideal when you have a single database to update. You can run

db.MyCol.update NewCol = OldCol db.MyCol.update Fullname = UPPER(Last) + ', ' + UPPER(First)

Shell command also supported using Run method. Shell does not have all functions implementations and suggestion are welcome.

I will add more options in shell and then will write a wiki page

This answer does not fit into current version of database (nuget package v 5.0.17).
Kindly provide new code sample. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants