16
16
import uuid
17
17
18
18
19
- METADATA_VERSION = '2.1 '
19
+ METADATA_VERSION = '2.2 '
20
20
21
21
DELETE_CANDIDATE_MAYBE = 0
22
22
DELETE_CANDIDATE_SURE = 1
@@ -47,15 +47,16 @@ class Version(Base):
47
47
__tablename__ = 'versions'
48
48
uid = Column (String (36 ), primary_key = True )
49
49
date = Column ("date" , DateTime , default = func .now (), nullable = False )
50
- name = Column (String , nullable = False )
50
+ name = Column (String , nullable = False , default = '' )
51
+ snapshot_name = Column (String , nullable = False , server_default = '' , default = '' )
51
52
size = Column (BigInteger , nullable = False )
52
53
size_bytes = Column (BigInteger , nullable = False )
53
54
valid = Column (Integer , nullable = False )
54
55
protected = Column (Integer , nullable = False )
55
56
56
57
def __repr__ (self ):
57
- return "<Version(uid='%s', name='%s', date='%s')>" % (
58
- self .uid , self .name , self .date )
58
+ return "<Version(uid='%s', name='%s', snapshot_name='%s', date='%s')>" % (
59
+ self .uid , self .name , self .snapshot_name , self . date )
59
60
60
61
61
62
DereferencedBlock = namedtuple ('Block' , ['uid' , 'version_uid' , 'id' , 'date' , 'checksum' , 'size' , 'valid' ])
@@ -121,18 +122,20 @@ def __init__(self, config):
121
122
from alembic .config import Config
122
123
from alembic import command
123
124
alembic_cfg = Config (os .path .join (os .path .dirname (os .path .realpath (__file__ )), "sql_migrations" , "alembic.ini" ))
124
- try :
125
- Base .metadata .create_all (engine , checkfirst = False ) # checkfirst False will raise when it finds an existing table
126
- except sqlalchemy .exc .OperationalError :
127
- # tables already exist, see if there are any db schema upgrades
128
- with engine .begin () as connection :
129
- alembic_cfg .attributes ['connection' ] = connection
130
- #command.upgrade(alembic_cfg, "head", sql=True)
131
- command .upgrade (alembic_cfg , "head" )
132
- else :
133
- # new database.
134
- # mark the version table, "stamping" it with the most recent rev:
135
- command .stamp (alembic_cfg , "head" )
125
+ with engine .begin () as connection :
126
+ alembic_cfg .attributes ['connection' ] = connection
127
+ try :
128
+ Base .metadata .create_all (engine , checkfirst = False ) # checkfirst False will raise when it finds an existing table
129
+ except sqlalchemy .exc .OperationalError :
130
+ # tables already exist, see if there are any db schema upgrades
131
+ with engine .begin () as connection :
132
+ alembic_cfg .attributes ['connection' ] = connection
133
+ #command.upgrade(alembic_cfg, "head", sql=True)
134
+ command .upgrade (alembic_cfg , "head" )
135
+ else :
136
+ # new database.
137
+ # mark the version table, "stamping" it with the most recent rev:
138
+ command .stamp (alembic_cfg , "head" )
136
139
137
140
Session = sessionmaker (bind = engine )
138
141
self .session = Session ()
@@ -147,11 +150,12 @@ def _commit(self):
147
150
self .session .commit ()
148
151
149
152
150
- def set_version (self , version_name , size , size_bytes , valid , protected = 0 ):
153
+ def set_version (self , version_name , snapshot_name , size , size_bytes , valid , protected = 0 ):
151
154
uid = self ._uid ()
152
155
version = Version (
153
156
uid = uid ,
154
157
name = version_name ,
158
+ snapshot_name = snapshot_name ,
155
159
size = size ,
156
160
size_bytes = size_bytes ,
157
161
valid = valid ,
@@ -404,9 +408,11 @@ def export(self, version_uid, f):
404
408
version .uid ,
405
409
version .date .strftime ('%Y-%m-%d %H:%M:%S' ),
406
410
version .name ,
411
+ version .snapshot_name ,
407
412
version .size ,
408
413
version .size_bytes ,
409
414
version .valid ,
415
+ version .protected ,
410
416
])
411
417
for block in blocks :
412
418
_csv .writerow ([
@@ -426,7 +432,7 @@ def import_(self, f):
426
432
signature = next (_csv )
427
433
if signature [0 ] != 'backy2 Version {} metadata dump' .format (METADATA_VERSION ):
428
434
raise ValueError ('Wrong import format.' )
429
- version_uid , version_date , version_name , version_size , version_size_bytes , version_valid = next (_csv )
435
+ version_uid , version_date , version_name , version_snapshot_name , version_size , version_size_bytes , version_valid , version_protected = next (_csv )
430
436
try :
431
437
self .get_version (version_uid )
432
438
except KeyError :
@@ -437,9 +443,11 @@ def import_(self, f):
437
443
uid = version_uid ,
438
444
date = datetime .datetime .strptime (version_date , '%Y-%m-%d %H:%M:%S' ),
439
445
name = version_name ,
446
+ snapshot_name = version_snapshot_name ,
440
447
size = version_size ,
441
448
size_bytes = version_size_bytes ,
442
449
valid = version_valid ,
450
+ protected = version_protected ,
443
451
)
444
452
self .session .add (version )
445
453
for uid , version_uid , id , date , checksum , size , valid in _csv :
0 commit comments