Welcome back.
In the previous video,
we added the code to the contracts class
to upgrade our database to version 4.
Version 3 contains a new view,
and we've seen the upgrade from version 2 working.
Of course, we don't know that it really works
because we haven't written any code to use the view yet.
But at least we know that the view gets created. Well, we think we know.
There wasn't an error at least.
To be sure we can examine the database on the phone,
I'll do it from the terminal this time.
But if you don't have access to your phone, you can copy the database
back to your computer.
With the database open, we can check the schema to make sure our view exists.
That's fine, there's our view created.
Some versions of SQLite add a comment to the end of the view when displaying the schema.
That's helpful, but you shouldn't include it in your code to create the view.
All right. Do we still have data in the database?
It's important to test that your upgrade does preserve the existing data.
Users won't be impressed if your app wipes out their data after updating itself.
Okay, to test the upgrade from version 1 to version 3,
we need to copy our version 1 database onto the device,
quit SQLite.
Also make sure the app isn't running on the emulator.
Replacing the database file while the app's running could cause problems.
So if it's running use the stop button in android studio to stop it.
We've already seen how to copy files from the device
using the device explorer.
We can also copy files to the device.
I'm going to replace the database with my version 1 database
that I copied in an earlier section.
We could be causing a problem here though.
If there's a journal file from version 4,
it could make our version 1 database unusable.
It's a good idea to clear out the existing files first
before copying the new database.
I'll navigate to /data/data/learnprogramming
.academy.tasktimer in the device file explorer,
then expand the databases directory.
I suggest you delete all the existing files
before copying new ones onto the device.
Select the task timer DB file
and any journal files that may be present,
then press the del button to delete them.
You can also right click and choose delete,
but I probably don't need to tell you that at this point in the course.
After deleting the existing files, right click databases
and choose upload.
I want to copy my version 1 database,
so I'll select all the files in my
tasktimerdb-version1 directory before clicking okay.
Device file explorer pops up a message
at the bottom to let you know if the file transfer was successful or not.
All right. Run the app again, and keep an eye on the logcat.
I'll close the device file explorer because we don't need it at the moment.
We should see the SQL commands to create the timings table
and its trigger followed by the command to create the two views.
Close the app down, and run SQLite 3 on the device again.
My terminal is still in the device shell
but you know how to get to the right place by now.
We can check the data in our database.
We've got the new table and the view
and there's still data in the tasks table.
The timings table should be empty,
which is fine. It's only just been created.
We can generate some timing data from the apps menu,
so switch back to the emulator, and do that.
Once a logcat stops updating,
switch back to the terminal window and check the database.
We've now got a version 4 database on the device
upgraded from version 1.
It won't do this, but we should also uninstall the app and run it again.
That will test the database creation process.
Remember that you'll have new users of the app
not everyone will be upgrading from an earlier version.
It's very important to test both processes;
the upgrade and the new database creation.
You should also test the upgrade from version 2 to version 4.
Whichever database version you start with,
you should end up with exactly the same database schema after the upgrade.
Okay. Quit SQLite and the terminal.
In the next video, we'll create the layouts to display the data
for a new view.
I'll see you in that one.