versioning-solana
This repo demonstrates ONE rudimentary way to upgrade/migrate account data changes with solana program changes.
What is data versioning?
Fundamentally to version data means to create a unique reference for a collection of data. This reference can take the form of a query, an ID, or also commonly a datetime identifier.
Simple scenario:
- You create a program that stores a
u64
value in your program's accounts data, assume you initialized the account and setsomevalue
to50u64
- Later on you decide that you want to also have some
String
value to reside in the account data that the rest of your program requires to be present
What do you do? There are a few options:
-
If the initial allocation of your program account has room to spare, and you had the foresite to include a 'data version' indicator in the account data: This repo will demonstrate an approach
-
If the initial allocation was sized specifically to the
u64
:- If you are running with a Solana version that incorporates the 'account re-allocation feature' (v 1.10.?) then leverage that
- Otherwise, leveraging PDAs may help but someone else can demonstrate that
On with the show
In your initial planning, make sure to include a 'data version' field in your data. It can be a simple incrementing ordinal (e.g. u8) or something more sophisticated, up to you.
When creating the Program account, allocate enough space for data growth
Demonstration
data_version
= 0)
First Version (- Clone this repo
- Change to repo folder
git checkout v0.0.1
- Change to
program
folder cargo build-bpf
- Change back to repo folder
code .
- Open the
prechange.rs
file in the maintest
folder - Click
Run Test
onfn test_load_pass()
- Open the
postchange.rs
file 11 ClickRun Test
onfn test_pre_data_change_load_pass()
- Exit code
mkdir stored
- Open another terminal and change to this repo folder then
solana-test-validator -l ./.ledger
- From the first folder terminal:
solana account -o stored/user1.json --output json-compact keys/accounts/user1_account.json
solana account -o stored/user2.json --output json-compact keys/accounts/user2_account.json
- In the terminal running
solana-test-validator
hit Ctrl+C to kill it
This will create two accounts with somevalue
to 1 and then modify each of their 1's value to 50u64
. Steps 13 - 16 are to export the accounts from the ledger for use in the next steps.
data_version
= 1)
First Version (- From repo main folder run
cargo clean
git checkout main
- Change to
program folder
cargo build-bpf
- Change back to repo folder
code .
- Open the
postchange.rs
file in the maintest
folder - Click
Run Test
onfn test_post_data_change_u2_pass()
The results will be as below