Browse Source

Improved schema upgraded for the case when no new database schemas are available. Updated unit tests accordingly.

Dan Helfman 9 years ago
parent
commit
27f5f4e1a9
2 changed files with 30 additions and 6 deletions
  1. 5
    0
      controller/Schema_upgrader.py
  2. 25
    6
      controller/test/Test_schema_upgrader.py

+ 5
- 0
controller/Schema_upgrader.py View File

@@ -77,6 +77,10 @@ class Schema_upgrader:
77 77
 
78 78
       versions.append( ( version, filename ) )
79 79
 
80
+    if len( versions ) == 0:
81
+      print "no new database schemas available"
82
+      return
83
+
80 84
     # sort the schema delta files by version
81 85
     versions.sort( lambda a, b: cmp( a[ 0 ], b[ 0 ] ) )
82 86
 
@@ -91,6 +95,7 @@ class Schema_upgrader:
91 95
   def schema_version( database, default_version = None ):
92 96
     try:
93 97
       schema_version = database.select_one( tuple, "select * from schema_version;" );
98
+      schema_version = tuple( map( int, schema_version ) )
94 99
     # if there's no schema version table, then use the default version given. if there's no default
95 100
     # version, then assume the from_version is 1.5.4, which was the last version not to include a
96 101
     # schema_version table

+ 25
- 6
controller/test/Test_schema_upgrader.py View File

@@ -42,29 +42,38 @@ class Test_schema_upgrader( object ):
42 42
 
43 43
     return contents
44 44
 
45
-  def test_upgrade_schema( self ):
45
+  def test_upgrade_schema( self, to_version = None ):
46
+    if not to_version:
47
+      to_version = u"5.7.11"
48
+
46 49
     self.fake_files = {
47 50
       u"model/delta/5.6.7.sqlite": u"create table new_table ( foo text ); insert into new_table values ( 'hi' );",
48 51
       u"model/delta/5.6.8.sqlite": u"insert into new_table values ( 'bye' );",
49 52
       u"model/delta/5.6.10.sqlite": u"alter table new_table add column bar text;",
50 53
       u"model/delta/5.7.11.sqlite": u"insert into new_table values ( 'whee', 'stuff' );",
51
-      u"model/delta/5.7.18.sqlite": u"insert into new_table values ( 'should not be present', 'nope' );",
54
+      u"model/delta/5.7.18.sqlite": u"insert into new_table values ( 'more', 'things' );",
52 55
     }
53 56
 
54
-    self.upgrader.upgrade_schema( u"5.7.11" )
57
+    self.upgrader.upgrade_schema( to_version )
55 58
 
56 59
     result = self.database.select_many( tuple, u"select * from new_table;" )
57
-    assert result == [ ( u"hi", None ), ( u"bye", None ), ( "whee", "stuff" ) ]
60
+    if to_version == u"5.7.11":
61
+      assert result == [ ( u"hi", None ), ( u"bye", None ), ( "whee", "stuff" ) ]
62
+    else:
63
+      assert result == [ ( u"hi", None ), ( u"bye", None ), ( "whee", "stuff" ), ( "more", "things" ) ]
58 64
 
59 65
     result = self.database.select_many( tuple, u"select * from schema_version;" )
60
-    assert result == [ ( 5, 7, 11 ) ]
66
+    if to_version == u"5.7.11":
67
+      assert result == [ ( 5, 7, 11 ) ]
68
+    else:
69
+      assert result == [ ( 5, 7, 18 ) ]
61 70
 
62 71
   def test_upgrade_schema_with_schema_version_table( self ):
63 72
     self.database.execute( u"create table schema_version ( major numeric, minor numeric, \"release\" numeric );" )
64 73
     self.database.execute( u"insert into schema_version values ( 0, 0, 0 );" )
65 74
     self.test_upgrade_schema()
66 75
 
67
-  def test_upgrade_schema_with_schema_version_table_and_specific_starting_version( self ):
76
+  def test_upgrade_schema_with_schema_version_table_and_starting_version( self ):
68 77
     self.database.execute( u"create table schema_version ( major numeric, minor numeric, \"release\" numeric );" )
69 78
     self.database.execute( u"insert into schema_version values ( 5, 6, 6 );" )
70 79
 
@@ -73,6 +82,16 @@ class Test_schema_upgrader( object ):
73 82
 
74 83
     self.test_upgrade_schema()
75 84
 
85
+  def test_upgrade_schema_with_schema_version_table_and_target_version_without_schema( self ):
86
+    self.database.execute( u"create table schema_version ( major numeric, minor numeric, \"release\" numeric );" )
87
+    self.database.execute( u"insert into schema_version values ( 0, 0, 0 );" )
88
+    self.test_upgrade_schema( to_version = u"5.7.20" )
89
+
90
+  def test_upgrade_schema_with_schema_version_table_and_starting_version_and_target_version_without_schema( self ):
91
+    self.database.execute( u"create table schema_version ( major numeric, minor numeric, \"release\" numeric );" )
92
+    self.database.execute( u"insert into schema_version values ( 5, 6, 6 );" )
93
+    self.test_upgrade_schema( to_version = u"5.7.20" )
94
+
76 95
   def test_upgrade_schema_with_future_ending_version( self ):
77 96
     self.fake_files = {
78 97
       u"model/delta/5.6.7.sqlite": u"create table new_table ( foo text ); insert into new_table values ( 'hi' );",