#30 Fix database upgrades from halfly initialized schema 1 databases
Merged 8 years ago by puiterwijk. Opened 8 years ago by puiterwijk.

@@ -1,6 +1,6 @@ 

  # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING

  

- from ipsilon.util.data import Store, UNIQUE_DATA_TABLE

+ from ipsilon.util.data import Store, UNIQUE_DATA_TABLE, OPTIONS_TABLE

  

  from openid import oidutil

  from openid.association import Association
@@ -82,6 +82,11 @@ 

          q = self._query(self._db, 'association', UNIQUE_DATA_TABLE,

                          trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

+         q = self._query(self._db, 'openid_extensions', OPTIONS_TABLE,

+                         trans=False)

+         q.create()

+         q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:
@@ -92,6 +97,11 @@ 

              self._db.add_constraint(table.primary_key)

              for index in table.indexes:

                  self._db.add_index(index)

+             table = self._query(self._db, 'openid_extensions', OPTIONS_TABLE,

+                                 trans=False)._table

+             self._db.add_constraint(table.primary_key)

+             for index in table.indexes:

+                 self._db.add_index(index)

              return 2

          else:

              raise NotImplementedError()

file modified
+21 -5
@@ -331,6 +331,7 @@ 

          #  the main codebase, and even in the same database.

          q = self._query(self._db, 'dbinfo', OPTIONS_TABLE, trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

Can you add to the commit message why you are closing the connection after each create so someone coming along later will understand? After that ACK.

          cls_name = self.__class__.__name__

          current_version = self.load_options('dbinfo').get('%s_schema'

                                                            % cls_name, {})
@@ -342,7 +343,8 @@ 

              fallback_version = self.load_options('dbinfo').get('scheme',

                                                                 {})

              if 'version' in fallback_version:

-                 return int(fallback_version['version'])

+                 # Explanation for this is in def upgrade_database(self)

+                 return -1

              else:

                  return None

  
@@ -388,6 +390,14 @@ 

              # Just initialize a new schema

              self._initialize_schema()

              self._store_new_schema_version(self._code_schema_version())

+         elif old_schema_version == -1:

+             # This is a special-case from 1.0: we only created tables at the

+             # first time they were actually used, but the upgrade code assumes

+             # that the tables exist. So let's fix this.

+             self._initialize_schema()

+             # The old version was schema version 1

+             self._store_new_schema_version(1)

+             self.upgrade_database()

          elif old_schema_version != self._code_schema_version():

              # Upgrade from old_schema_version to code_schema_version

              self.debug('Upgrading from schema version %i' % old_schema_version)
@@ -599,6 +609,7 @@ 

                        'provider_config']:

              q = self._query(self._db, table, OPTIONS_TABLE, trans=False)

              q.create()

+             q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:
@@ -618,10 +629,12 @@ 

              raise NotImplementedError()

  

      def create_plugin_data_table(self, plugin_name):

-         table = plugin_name+'_data'

-         q = self._query(self._db, table, UNIQUE_DATA_TABLE,

-                         trans=False)

-         q.create()

+         if not self.is_readonly:

+             table = plugin_name+'_data'

+             q = self._query(self._db, table, UNIQUE_DATA_TABLE,

+                             trans=False)

+             q.create()

+             q._con.close()  # pylint: disable=protected-access

  

  

  class UserStore(Store):
@@ -644,6 +657,7 @@ 

      def _initialize_schema(self):

          q = self._query(self._db, 'users', OPTIONS_TABLE, trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:
@@ -668,6 +682,7 @@ 

          q = self._query(self._db, 'transactions', UNIQUE_DATA_TABLE,

                          trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:
@@ -769,6 +784,7 @@ 

          q = self._query(self._db, self.table, UNIQUE_DATA_TABLE,

                          trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:

@@ -21,6 +21,7 @@ 

          q = self._query(self._db, 'sessions', SESSION_TABLE,

                          trans=False)

          q.create()

+         q._con.close()  # pylint: disable=protected-access

  

      def _upgrade_schema(self, old_version):

          if old_version == 1:

@@ -11,4 +11,9 @@ 

  	name TEXT NOT NULL, 

  	value TEXT

  );

+ CREATE TABLE openid_extensions (

+     name TEXT NOT NULL,

+     option TEXT NOT NULL,

+     value TEXT

+ );

  COMMIT;

no initial comment

Can you add to the commit message why you are closing the connection after each create so someone coming along later will understand? After that ACK.