@@ -77,11 +77,6 @@ def __init__(self, specification, database_definition_object):
7777
7878 self .__tables = {}
7979
80- # Deriving class has not established its latest revision?
81- if not hasattr (self , "LATEST_REVISION" ) or self .LATEST_REVISION is None :
82- # Presume it is 1.
83- self .LATEST_REVISION = 1
84-
8580 self .__backup_restore_lock = asyncio .Lock ()
8681
8782 # Last undo position.
@@ -147,12 +142,13 @@ async def connect(self, should_drop_database=False):
147142 if should_create_schemas :
148143 await self .create_schemas ()
149144 await self .insert (
150- Tablenames .REVISION , [{"number" : self .LATEST_REVISION }]
145+ Tablenames .REVISION ,
146+ [{"number" : self .__database_definition_object .LATEST_REVISION }],
151147 )
152148
153149 # Emit the name of the database file for positive confirmation on console.
154150 logger .info (
155- f"{ callsign (self )} database name is { self .__database_name } code revision { self .LATEST_REVISION } "
151+ f"{ callsign (self )} database name is { self .__database_name } database definition revision { self . __database_definition_object .LATEST_REVISION } "
156152 )
157153
158154 # ----------------------------------------------------------------------------------------
@@ -170,36 +166,38 @@ async def apply_revisions(self):
170166 why = "get database revision" ,
171167 )
172168 if len (records ) == 0 :
173- old_revision = 0
169+ database_revision = 0
174170 else :
175- old_revision = records [0 ]["number" ]
171+ database_revision = records [0 ]["number" ]
176172 except Exception as exception :
177173 logger .warning (
178174 f"could not get revision, presuming legacy database with no table: { exception } "
179175 )
180- old_revision = 0
176+ database_revision = 0
181177
182- if old_revision < self .LATEST_REVISION :
178+ if database_revision < self . __database_definition_object .LATEST_REVISION :
183179 # Backup before applying revisions.
184180 logger .debug (
185- f"[BKREVL] backing up before updating from revision { old_revision } to revision { self .LATEST_REVISION } "
181+ f"[BKREVL] backing up before updating from database revision { database_revision } "
182+ f" to definition revision { self .__database_definition_object .LATEST_REVISION } "
186183 )
187184
188185 await self .backup ()
189186
190- for revision in range (old_revision , self .LATEST_REVISION ):
191- logger .debug (f"updating to revision { revision + 1 } " )
187+ for revision in range (
188+ database_revision , self .__database_definition_object .LATEST_REVISION
189+ ):
192190 await self .apply_revision (revision + 1 )
193191 await self .update (
194192 Tablenames .REVISION ,
195- {"number" : self .LATEST_REVISION },
193+ {"number" : self .__database_definition_object . LATEST_REVISION },
196194 "1 = 1" ,
197195 why = "update database revision" ,
198196 )
199197 else :
200198 logger .debug (
201- f"[BKREVL] no need to update persistent revision { old_revision } "
202- f" which matches code revision { self .LATEST_REVISION } "
199+ f"[BKREVL] no need to update database revision { database_revision } "
200+ f" which matches definition revision { self . __database_definition_object .LATEST_REVISION } "
203201 )
204202
205203 # ----------------------------------------------------------------------------------------
@@ -212,6 +210,9 @@ async def apply_revision(self, revision):
212210 await self .create_table (Tablenames .REVISION )
213211 await self .insert (Tablenames .REVISION , [{"revision" : revision }])
214212
213+ # Let the database definition object do its thing.
214+ await self .__database_definition_object .apply_revision (self , revision )
215+
215216 # ----------------------------------------------------------------------------------------
216217 async def disconnect (self ):
217218
@@ -551,39 +552,7 @@ async def backup(self):
551552 """
552553
553554 async with self .__backup_restore_lock :
554- # Prune all the restores which were orphaned.
555- directory = self .__backup_directory
556- if directory is None :
557- raise RuntimeError ("no backup directory supplied in confirmation" )
558-
559- basename , suffix = os .path .splitext (os .path .basename (self .__database_name ))
560-
561- filenames = glob .glob (f"{ directory } /{ basename } .*{ suffix } " )
562-
563- filenames .sort (reverse = True )
564-
565- for restore in range (self .__last_restore ):
566- logger .debug (
567- f"[BACKPRU] removing { restore } -th restore { filenames [restore ]} "
568- )
569- os .remove (filenames [restore ])
570-
571- self .__last_restore = 0
572-
573- timestamp = isodatetime_filename ()
574- to_filename = f"{ directory } /{ basename } .{ timestamp } { suffix } "
575-
576- await self .disconnect ()
577- try :
578- await self .__create_directory (to_filename )
579- shutil .copy2 (self .__database_name , to_filename )
580- logger .debug (f"backed up to { to_filename } " )
581- except Exception :
582- raise RuntimeError (
583- f"copy { self .__database_name } to { to_filename } failed"
584- )
585- finally :
586- await self .connect ()
555+ pass
587556
588557 # ----------------------------------------------------------------------------------------
589558 async def restore (self , nth ):
@@ -592,37 +561,7 @@ async def restore(self, nth):
592561 """
593562
594563 async with self .__backup_restore_lock :
595- directory = self .__backup_directory
596- if directory is None :
597- raise RuntimeError ("no backup directory supplied in confirmation" )
598-
599- basename , suffix = os .path .splitext (os .path .basename (self .__database_name ))
600-
601- filenames = glob .glob (f"{ directory } /{ basename } .*{ suffix } " )
602-
603- filenames .sort (reverse = True )
604-
605- if nth >= len (filenames ):
606- raise RuntimeError (
607- f"restoration index { nth } is more than available { len (filenames )} "
608- )
609-
610- from_filename = filenames [nth ]
611-
612- await self .disconnect ()
613- try :
614- shutil .copy2 (from_filename , self .__database_name )
615- logger .debug (
616- f"restored nth { nth } out of { len (filenames )} from { from_filename } "
617- )
618- except Exception :
619- raise RuntimeError (
620- f"copy { from_filename } to { self .__database_name } failed"
621- )
622- finally :
623- await self .connect ()
624-
625- self .__last_restore = nth
564+ pass
626565
627566
628567# ----------------------------------------------------------------------------------------
0 commit comments