1
00:00:04,700 --> 00:00:05,700
Welcome back.

2
00:00:06,500 --> 00:00:07,860
In the previous video,

3
00:00:07,860 --> 00:00:10,160
we added the code to the contracts class

4
00:00:10,460 --> 00:00:12,760
to upgrade our database to version 4.

5
00:00:13,560 --> 00:00:15,560
Version 3 contains a new view,

6
00:00:15,960 --> 00:00:18,660
and we've seen the upgrade from version 2 working.

7
00:00:18,660 --> 00:00:21,460
Of course, we don't know that it really works

8
00:00:21,460 --> 00:00:24,660
because we haven't written any code to use the view yet.

9
00:00:25,660 --> 00:00:29,660
But at least we know that the view gets created. Well, we think we know.

10
00:00:29,660 --> 00:00:31,160
There wasn't an error at least.

11
00:00:32,960 --> 00:00:35,560
To be sure we can examine the database on the phone,

12
00:00:36,060 --> 00:00:37,960
I'll do it from the terminal this time.

13
00:00:38,160 --> 00:00:41,760
But if you don't have access to your phone, you can copy the database

14
00:00:41,760 --> 00:00:43,120
back to your computer.

15
00:01:01,720 --> 00:01:06,490
With the database open, we can check the schema to make sure our view exists.

16
00:01:10,990 --> 00:01:13,590
That's fine, there's our view created.

17
00:01:14,250 --> 00:01:18,850
Some versions of SQLite add a comment to the end of the view when displaying the schema.

18
00:01:19,650 --> 00:01:23,310
That's helpful, but you shouldn't include it in your code to create the view.

19
00:01:23,860 --> 00:01:26,860
All right. Do we still have data in the database?

20
00:01:40,060 --> 00:01:44,420
It's important to test that your upgrade does preserve the existing data.

21
00:01:44,970 --> 00:01:49,170
Users won't be impressed if your app wipes out their data after updating itself.

22
00:01:49,830 --> 00:01:54,190
Okay, to test the upgrade from version 1 to version 3,

23
00:01:54,590 --> 00:01:58,390
we need to copy our version 1 database onto the device,

24
00:01:59,390 --> 00:02:00,690
quit SQLite.

25
00:02:04,690 --> 00:02:08,490
Also make sure the app isn't running on the emulator.

26
00:02:12,290 --> 00:02:16,290
Replacing the database file while the app's running could cause problems.

27
00:02:16,840 --> 00:02:20,740
So if it's running use the stop button in android studio to stop it.

28
00:02:21,540 --> 00:02:24,640
We've already seen how to copy files from the device

29
00:02:24,890 --> 00:02:26,490
using the device explorer.

30
00:02:27,290 --> 00:02:29,890
We can also copy files to the device.

31
00:02:30,770 --> 00:02:34,670
I'm going to replace the database with my version 1 database

32
00:02:34,670 --> 00:02:36,670
that I copied in an earlier section.

33
00:02:37,170 --> 00:02:39,370
We could be causing a problem here though.

34
00:02:40,030 --> 00:02:42,530
If there's a journal file from version 4,

35
00:02:42,930 --> 00:02:46,130
it could make our version 1 database unusable.

36
00:02:47,130 --> 00:02:50,330
It's a good idea to clear out the existing files first

37
00:02:50,730 --> 00:02:52,530
before copying the new database.

38
00:02:53,030 --> 00:02:57,690
I'll navigate to /data/data/learnprogramming

39
00:02:57,890 --> 00:03:01,990
.academy.tasktimer in the device file explorer,

40
00:03:02,350 --> 00:03:04,650
then expand the databases directory.

41
00:03:17,450 --> 00:03:20,650
I suggest you delete all the existing files

42
00:03:20,650 --> 00:03:23,010
before copying new ones onto the device.

43
00:03:24,010 --> 00:03:26,370
Select the task timer DB file

44
00:03:26,920 --> 00:03:29,280
and any journal files that may be present,

45
00:03:29,880 --> 00:03:32,280
then press the del button to delete them.

46
00:03:35,640 --> 00:03:38,000
You can also right click and choose delete,

47
00:03:38,550 --> 00:03:41,450
but I probably don't need to tell you that at this point in the course.

48
00:03:42,350 --> 00:03:46,850
After deleting the existing files, right click databases

49
00:03:46,850 --> 00:03:48,450
and choose upload.

50
00:03:54,050 --> 00:03:57,050
I want to copy my version 1 database,

51
00:03:57,410 --> 00:03:59,810
so I'll select all the files in my

52
00:03:59,810 --> 00:04:04,210
tasktimerdb-version1 directory before clicking okay.

53
00:04:17,910 --> 00:04:20,269
Device file explorer pops up a message

54
00:04:20,630 --> 00:04:24,630
at the bottom to let you know if the file transfer was successful or not.

55
00:04:25,290 --> 00:04:29,290
All right. Run the app again, and keep an eye on the logcat.

56
00:04:42,890 --> 00:04:46,250
I'll close the device file explorer because we don't need it at the moment.

57
00:04:49,610 --> 00:04:53,270
We should see the SQL commands to create the timings table

58
00:04:53,600 --> 00:04:57,700
and its trigger followed by the command to create the two views.

59
00:05:09,060 --> 00:05:13,610
Close the app down, and run SQLite 3 on the device again.

60
00:05:16,110 --> 00:05:18,110
My terminal is still in the device shell

61
00:05:18,610 --> 00:05:20,810
but you know how to get to the right place by now.

62
00:05:26,410 --> 00:05:28,770
We can check the data in our database.

63
00:05:36,770 --> 00:05:39,770
We've got the new table and the view

64
00:05:39,770 --> 00:05:41,970
and there's still data in the tasks table.

65
00:05:42,470 --> 00:05:44,970
The timings table should be empty,

66
00:05:49,330 --> 00:05:51,830
which is fine. It's only just been created.

67
00:05:52,490 --> 00:05:55,480
We can generate some timing data from the apps menu,

68
00:05:55,480 --> 00:05:58,080
so switch back to the emulator, and do that.

69
00:06:06,680 --> 00:06:08,680
Once a logcat stops updating,

70
00:06:10,180 --> 00:06:13,480
switch back to the terminal window and check the database.

71
00:06:21,780 --> 00:06:24,780
We've now got a version 4 database on the device

72
00:06:24,780 --> 00:06:26,980
upgraded from version 1.

73
00:06:27,970 --> 00:06:32,270
It won't do this, but we should also uninstall the app and run it again.

74
00:06:32,930 --> 00:06:35,530
That will test the database creation process.

75
00:06:36,030 --> 00:06:38,830
Remember that you'll have new users of the app

76
00:06:39,190 --> 00:06:42,190
not everyone will be upgrading from an earlier version.

77
00:06:42,490 --> 00:06:45,090
It's very important to test both processes;

78
00:06:45,450 --> 00:06:48,150
the upgrade and the new database creation.

79
00:06:48,650 --> 00:06:52,150
You should also test the upgrade from version 2 to version 4.

80
00:06:52,810 --> 00:06:55,210
Whichever database version you start with,

81
00:06:55,210 --> 00:06:59,310
you should end up with exactly the same database schema after the upgrade.

82
00:07:00,110 --> 00:07:03,310
Okay. Quit SQLite and the terminal.

83
00:07:07,310 --> 00:07:11,410
In the next video, we'll create the layouts to display the data

84
00:07:11,610 --> 00:07:12,610
for a new view.

85
00:07:13,270 --> 00:07:14,270
I'll see you in that one.

