Updating the database
A initialized database is ready to be updated. Before updating, a version source
must be created.
Version source
For this example we'll use a very simple version source. It's a JSON
file.
{
"name": "example-db",
"version": [{
"_id": "1.0.0",
"createtable": {
"customers": {
"primary_key": "customer_id",
"customer_id": {
"type": "INT",
"a_i": true
},
"name": {
"type": "VARCHAR",
"length": 100,
"unique": true
},
"created": {
"type": "DATETIME"
}
}
},
"default_data": {
"customers": [
{
"customer_id": 1,
"name": "John",
"created": "2023-10-31 02:12:52.000000"
},
{
"customer_id": 2,
"name": "Brian",
"created": "2022-02-21 11:42:19.000000"
},
{
"customer_id": 3,
"name": "Anouk",
"created": "2021-06-12 08:13:48.000000"
}
]
}
}]
}
This will create the following table in your database:
customer_id 🔑 | name | created |
---|---|---|
1 | John | 2023-10-31 02:12:52.000000 |
2 | Brian | 2022-02-21 11:42:19.000000 |
3 | Anouk | 2021-06-12 08:13:48.000000 |
Read from file
AlphaDB expects a String
for the version source, so we can simple read the contents of the JSON file and supply it to the update
method.
use std::fs;
use alphadb::utils::types::ToleratedVerificationIssueLevel;
let version_source = match fs::read_to_string("path/to/version-source.json") {
Ok(f) => f,
Err(_) => panic!("An error occured while opening the version source file!")
};
The update
method takes a couple more arguments. Let's take a look at the function definition:
fn update(
&mut self,
version_source: String,
update_to_version: Option<&str>, // Optionally set the max version to update to
no_data: bool, // Whether the default data should be inserted or not
verify: bool, // Wheter the version source should be checked for errors before updating
allowed_error_priority: ToleratedVerificationIssueLevel,
) -> Result<(), UpdateError>
Tolerated verification issue level
When verify
is set to true
, the version source will be verified before running the update. The verification process seperates errors in different priorities
. With allowed_error_priority
you specify the level at which the verification will fail.
Run updates
db.update(version_source, None, false, true, ToleratedVerificationIssueLevel::Low);
The update will now include default data, first be verified and fail whenever there is an issue with a higher priority than ToleratedVerificationIssueLevel::Low
.