-
Notifications
You must be signed in to change notification settings - Fork 3
/
scripts.py
1125 lines (965 loc) · 38.9 KB
/
scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import datetime
import imp
import logging
import os
import re
import requests
import time
from requests.exceptions import (
ConnectionError,
HTTPError,
)
import sys
import traceback
from collections import defaultdict
from nose.tools import set_trace
from sqlalchemy import create_engine
from sqlalchemy.sql.functions import func
from sqlalchemy.orm.session import Session
from config import Configuration, CannotLoadConfiguration
from metadata_layer import ReplacementPolicy
from model import (
get_one,
get_one_or_create,
production_session,
CustomList,
DataSource,
Edition,
Identifier,
LicensePool,
PresentationCalculationPolicy,
Subject,
Timestamp,
Work,
WorkCoverageRecord,
WorkGenre,
)
from external_search import (
ExternalSearchIndex,
)
from nyt import NYTBestSellerAPI
from opds_import import OPDSImportMonitor
from util.opds_writer import OPDSFeed
from monitor import SubjectAssignmentMonitor
from overdrive import (
OverdriveBibliographicCoverageProvider,
)
from threem import (
ThreeMBibliographicCoverageProvider,
)
from axis import Axis360BibliographicCoverageProvider
class Script(object):
@property
def _db(self):
if not hasattr(self, "_session"):
self._session = production_session()
return self._session
@property
def log(self):
if not hasattr(self, '_log'):
logger_name = getattr(self, 'name', None)
self._log = logging.getLogger(logger_name)
return self._log
@property
def data_directory(self):
return Configuration.data_directory()
@classmethod
def parse_command_line(cls, _db=None, cmd_args=None):
parser = cls.arg_parser()
return parser.parse_known_args(cmd_args)[0]
@classmethod
def arg_parser(cls):
raise NotImplementedError()
@classmethod
def parse_time(cls, time_string):
"""Try to pass the given string as a time."""
if not time_string:
return None
for format in ('%Y-%m-%d', '%m/%d/%Y', '%Y%m%d'):
for hours in ('', ' %H:%M:%S'):
full_format = format + hours
try:
parsed = datetime.datetime.strptime(
time_string, full_format
)
return parsed
except ValueError, e:
continue
raise ValueError("Could not parse time: %s" % time_string)
def __init__(self, _db=None):
"""Basic constructor.
:_db: A database session to be used instead of
creating a new one. Useful in tests.
"""
if _db:
self._session = _db
def run(self):
self.load_configuration()
DataSource.well_known_sources(self._db)
try:
self.do_run()
except Exception, e:
logging.error(
"Fatal exception while running script: %s", e,
exc_info=e
)
raise e
def load_configuration(self):
if not Configuration.instance:
Configuration.load()
class RunMonitorScript(Script):
def __init__(self, monitor, **kwargs):
if callable(monitor):
monitor = monitor(self._db, **kwargs)
self.monitor = monitor
self.name = self.monitor.service_name
def do_run(self):
self.monitor.run()
class RunCoverageProvidersScript(Script):
"""Alternate between multiple coverage providers."""
def __init__(self, providers):
self.providers = []
for i in providers:
if callable(i):
i = i(self._db)
self.providers.append(i)
def do_run(self):
offsets = dict()
providers = list(self.providers)
while providers:
random.shuffle(providers)
for provider in providers:
offset = offsets.get(provider, 0)
self.log.debug(
"Running %s with offset %s", provider.service_name, offset
)
offset = provider.run_once_and_update_timestamp(offset)
self.log.debug(
"Completed %s, new offset is %s", provider.service_name, offset
)
if offset is None:
# We're done with this provider for now.
if provider in offsets:
del offsets[provider]
if provider in providers:
providers.remove(provider)
else:
offsets[provider] = offset
class IdentifierInputScript(Script):
"""A script that takes identifiers as command line inputs."""
@classmethod
def read_stdin_lines(self, stdin):
"""Read lines from a (possibly mocked, possibly empty) standard input."""
if stdin is not sys.stdin or not os.isatty(0):
# A file has been redirected into standard input. Grab its
# lines.
lines = [x.strip() for x in stdin.readlines()]
else:
lines = []
return lines
@classmethod
def parse_command_line(cls, _db=None, cmd_args=None, stdin=sys.stdin,
*args, **kwargs):
parser = cls.arg_parser()
parsed = parser.parse_args(cmd_args)
stdin = cls.read_stdin_lines(stdin)
return cls.look_up_identifiers(_db, parsed, stdin, *args, **kwargs)
@classmethod
def look_up_identifiers(cls, _db, parsed, stdin_identifier_strings, *args, **kwargs):
"""Turn identifiers as specified on the command line into
real database Identifier objects.
"""
if _db and parsed.identifier_type:
# We can also call parse_identifier_list.
identifier_strings = parsed.identifier_strings
if stdin_identifier_strings:
identifier_strings = (
identifier_strings + stdin_identifier_strings
)
parsed.identifiers = cls.parse_identifier_list(
_db, parsed.identifier_type, identifier_strings,
*args, **kwargs
)
else:
# The script can call parse_identifier_list later if it
# wants to.
parsed.identifiers = None
return parsed
@classmethod
def arg_parser(cls):
parser = argparse.ArgumentParser()
parser.add_argument(
'--identifier-type',
help='Process identifiers of this type. If IDENTIFIER is not specified, all identifiers of this type will be processed. If IDENTIFIER is specified, this argument is required.'
)
parser.add_argument(
'identifier_strings',
help='A specific identifier to process.',
metavar='IDENTIFIER', nargs='*'
)
return parser
@classmethod
def parse_identifier_list(
cls, _db, identifier_type, arguments, autocreate=False
):
"""Turn a list of identifiers into a list of Identifier objects.
The list of arguments is probably derived from a command-line
parser such as the one defined in
IdentifierInputScript.arg_parser().
This makes it easy to identify specific identifiers on the
command line. Examples:
1 2
a b c
"""
current_identifier_type = None
if len(arguments) == 0:
return []
if not identifier_type:
raise ValueError("No identifier type specified!")
identifiers = []
for arg in arguments:
identifier, ignore = Identifier.for_foreign_id(
_db, identifier_type, arg, autocreate=autocreate
)
if not identifier:
logging.warn(
"Could not load identifier %s/%s", identifier_type, arg
)
if identifier:
identifiers.append(identifier)
return identifiers
class SubjectInputScript(Script):
"""A script whose command line filters the set of Subjects.
:return: a 2-tuple (subject type, subject filter) that can be
passed into the SubjectSweepMonitor constructor.
"""
@classmethod
def arg_parser(cls):
parser = argparse.ArgumentParser()
parser.add_argument(
'--subject-type',
help='Process subjects of this type'
)
parser.add_argument(
'--subject-filter',
help='Process subjects whose names or identifiers match this substring'
)
return parser
class RunCoverageProviderScript(IdentifierInputScript):
"""Run a single coverage provider."""
@classmethod
def arg_parser(cls):
parser = IdentifierInputScript.arg_parser()
parser.add_argument(
'--cutoff-time',
help='Update existing coverage records if they were originally created after this time.'
)
return parser
@classmethod
def parse_command_line(cls, _db, cmd_args=None, stdin=sys.stdin,
*args, **kwargs):
parser = cls.arg_parser()
parsed = parser.parse_args(cmd_args)
stdin = cls.read_stdin_lines(stdin)
parsed = cls.look_up_identifiers(_db, parsed, stdin, *args, **kwargs)
if parsed.cutoff_time:
parsed.cutoff_time = cls.parse_time(parsed.cutoff_time)
return parsed
def __init__(self, provider, _db=None, cmd_args=None, **provider_arguments):
super(RunCoverageProviderScript, self).__init__(_db)
args = self.parse_command_line(self._db, cmd_args)
if callable(provider):
if args.identifier_type:
self.identifier_type = args.identifier_type
self.identifier_types = [self.identifier_type]
else:
self.identifier_type = None
self.identifier_types = []
kwargs = self.extract_additional_command_line_arguments(args)
kwargs.update(provider_arguments)
provider = provider(
self._db,
cutoff_time=args.cutoff_time,
**kwargs
)
self.provider = provider
self.name = self.provider.service_name
self.identifiers = args.identifiers
def extract_additional_command_line_arguments(self, args):
"""A hook method for subclasses.
Turns command-line arguments into additional keyword arguments
to the CoverageProvider constructor.
By default, pass in a value used only by CoverageProvider
(as opposed to WorkCoverageProvider).
"""
return {
"input_identifier_types" : self.identifier_types,
}
def do_run(self):
if self.identifiers:
self.provider.run_on_specific_identifiers(self.identifiers)
else:
self.provider.run()
class BibliographicRefreshScript(RunCoverageProviderScript):
"""Refresh the core bibliographic data for Editions direct from the
license source.
This covers all known sources of licensed content.
"""
def __init__(self, **metadata_replacement_args):
self.metadata_replacement_policy = ReplacementPolicy.from_metadata_source(
**metadata_replacement_args
)
def do_run(self):
args = self.parse_command_line(self._db)
if args.identifiers:
# This script is being invoked to fix a problem.
# Make sure to always recalculate OPDS feeds and reindex the
# work.
self.metadata_replacement_policy.presentation_calculation_policy = (
PresentationCalculationPolicy.recalculate_everything()
)
for identifier in args.identifiers:
self.refresh_metadata(identifier)
else:
# This script is being invoked to provide general coverage,
# so we'll only recalculate OPDS feeds and reindex the work
# if something actually changes.
for provider_class in (
ThreeMBibliographicCoverageProvider,
OverdriveBibliographicCoverageProvider,
Axis360BibliographicCoverageProvider
):
try:
provider = provider_class(
self._db,
cutoff_time=args.cutoff_time
)
except CannotLoadConfiguration, e:
self.log.info(
'Cannot create provider: "%s" Assuming this is intentional and proceeding.',
str(e)
)
provider = None
try:
if provider:
provider.run()
except Exception, e:
self.log.error(
"Error in %r, moving on to next source.",
provider, exc_info=e
)
self._db.commit()
def refresh_metadata(self, identifier):
provider = None
if identifier.type==Identifier.THREEM_ID:
provider = ThreeMBibliographicCoverageProvider
elif identifier.type==Identifier.OVERDRIVE_ID:
provider = OverdriveBibliographicCoverageProvider
elif identifier.type==Identifier.AXIS_360_ID:
provider = Axis360BibliographicCoverageProvider
else:
self.log.warn("Cannot update coverage for %r" % identifier)
if provider:
provider = provider(
self._db,
metadata_replacement_policy=self.metadata_replacement_policy,
)
provider.ensure_coverage(identifier, force=True)
class WorkProcessingScript(IdentifierInputScript):
name = "Work processing script"
def __init__(self, force=False, batch_size=10):
args = self.parse_command_line(self._db)
self.identifier_type = args.identifier_type
self.identifiers = args.identifiers
self.batch_size = batch_size
self.query = self.make_query(
self._db, self.identifier_type, self.identifiers, self.log
)
self.force = force
@classmethod
def make_query(self, _db, identifier_type, identifiers, log=None):
query = _db.query(Work)
if identifiers or identifier_type:
query = query.join(Work.license_pools).join(
LicensePool.identifier
)
if identifiers:
if log:
log.info(
'Restricted to %d specific identifiers.' % len(identifiers)
)
query = query.filter(
LicensePool.identifier_id.in_([x.id for x in identifiers])
)
if identifier_type:
if log:
log.info(
'Restricted to identifier type "%s".' % identifier_type
)
query = query.filter(Identifier.type==identifier_type)
if log:
log.info(
"Processing %d works.", query.count()
)
return query.order_by(Work.id)
def do_run(self):
works = True
offset = 0
while works:
works = self.query.offset(offset).limit(self.batch_size).all()
for work in works:
self.process_work(work)
offset += self.batch_size
self._db.commit()
self._db.commit()
def process_work(self, work):
raise NotImplementedError()
class WorkConsolidationScript(WorkProcessingScript):
"""Given an Identifier, make sure all the LicensePools for that
Identifier are in Works that follow these rules:
a) For a given permanent work ID, there may be at most one Work
containing open-access LicensePools.
b) Each non-open-access LicensePool has its own individual Work.
"""
name = "Work consolidation script"
def make_query(self, _db, identifier_type, identifiers, log=None):
# We actually process LicensePools, not Works.
qu = _db.query(LicensePool).join(LicensePool.identifier)
if identifier_type:
qu = qu.filter(Identifier.type==identifier_type)
if identifiers:
qu = qu.filter(
Identifier.identifier.in_([x.identifier for x in identifiers])
)
return qu
def process_work(self, work):
# We call it 'work' for signature compatibility with the superclass,
# but it's actually a LicensePool.
licensepool = work
licensepool.calculate_work()
def do_run(self):
super(WorkConsolidationScript, self).do_run()
qu = self._db.query(Work).outerjoin(Work.license_pools).filter(
LicensePool.id==None
)
self.log.info(
"Deleting %d Works that have no LicensePools." % qu.count()
)
for i in qu:
self._db.delete(i)
self._db.commit()
class WorkPresentationScript(WorkProcessingScript):
"""Calculate the presentation for Work objects."""
# Do a complete recalculation of the presentation.
policy = PresentationCalculationPolicy()
def process_work(self, work):
work.calculate_presentation(policy=self.policy)
class WorkClassificationScript(WorkPresentationScript):
"""Recalculate the classification--and nothing else--for Work objects.
"""
policy = PresentationCalculationPolicy(
choose_edition=False,
set_edition_metadata=False,
classify=True,
choose_summary=False,
calculate_quality=False,
choose_cover=False,
regenerate_opds_entries=False,
update_search_index=False,
)
class WorkOPDSScript(WorkPresentationScript):
"""Recalculate the OPDS entries and search index entries for Work objects.
This is intended to verify that a problem has already been resolved and just
needs to be propagated to these two 'caches'.
"""
policy = PresentationCalculationPolicy(
choose_edition=False,
set_edition_metadata=False,
classify=True,
choose_summary=False,
calculate_quality=False,
choose_cover=False,
regenerate_opds_entries=True,
update_search_index=True,
)
class CustomListManagementScript(Script):
"""Maintain a CustomList whose membership is determined by a
MembershipManager.
"""
def __init__(self, manager_class,
data_source_name, list_identifier, list_name,
primary_language, description,
**manager_kwargs
):
data_source = DataSource.lookup(self._db, data_source_name)
self.custom_list, is_new = get_one_or_create(
self._db, CustomList,
data_source_id=data_source.id,
foreign_identifier=list_identifier,
)
self.custom_list.primary_language = primary_language
self.custom_list.description = description
self.membership_manager = manager_class(
self.custom_list, **manager_kwargs
)
def run(self):
self.membership_manager.update()
self._db.commit()
class OPDSImportScript(Script):
"""Import all books from an OPDS feed."""
@classmethod
def arg_parser(cls):
parser = argparse.ArgumentParser()
parser.add_argument(
'--url',
help='URL of the OPDS feed to be imported'
)
parser.add_argument(
'--data-source',
help='The name of the data source providing the OPDS feed.'
)
parser.add_argument(
'--force',
help='Import the feed from scratch, even if it seems like it was already imported.',
dest='force', action='store_true'
)
return parser
def __init__(self, feed_url, opds_data_source, importer_class,
immediately_presentation_ready=False, cmd_args=None):
args = self.parse_command_line(cmd_args)
self.force_reimport = args.force
self.feed_url = args.url or feed_url
self.opds_data_source = args.data_source or opds_data_source
self.importer_class = importer_class
self.immediately_presentation_ready = immediately_presentation_ready
def do_run(self):
monitor = OPDSImportMonitor(
self._db, self.feed_url, self.opds_data_source,
self.importer_class,
immediately_presentation_ready = self.immediately_presentation_ready,
force_reimport=self.force_reimport
)
monitor.run()
class NYTBestSellerListsScript(Script):
def __init__(self, include_history=False):
super(NYTBestSellerListsScript, self).__init__()
self.include_history = include_history
def do_run(self):
self.api = NYTBestSellerAPI(self._db)
self.data_source = DataSource.lookup(self._db, DataSource.NYT)
# For every best-seller list...
names = self.api.list_of_lists()
for l in sorted(names['results'], key=lambda x: x['list_name_encoded']):
name = l['list_name_encoded']
logging.info("Handling list %s" % name)
best = self.api.best_seller_list(l)
if self.include_history:
self.api.fill_in_history(best)
else:
self.api.update(best)
# Mirror the list to the database.
customlist = best.to_customlist(self._db)
logging.info(
"Now %s entries in the list.", len(customlist.entries))
self._db.commit()
class RefreshMaterializedViewsScript(Script):
"""Refresh all materialized views."""
@classmethod
def arg_parser(cls):
parser = argparse.ArgumentParser()
parser.add_argument(
'--blocking-refresh',
help="Provide this argument if you're on an older version of Postgres and can't refresh materialized views concurrently.",
action='store_true',
)
return parser
def do_run(self):
args = self.parse_command_line()
if args.blocking_refresh:
concurrently = ''
else:
concurrently = 'CONCURRENTLY'
# Initialize database
from model import (
MaterializedWork,
MaterializedWorkWithGenre,
)
db = self._db
for i in (MaterializedWork, MaterializedWorkWithGenre):
view_name = i.__table__.name
a = time.time()
db.execute("REFRESH MATERIALIZED VIEW %s %s" % (concurrently, view_name))
b = time.time()
print "%s refreshed in %.2f sec." % (view_name, b-a)
# Close out this session because we're about to create another one.
db.commit()
db.close()
# The normal database connection (which we want almost all the
# time) wraps everything in a big transaction, but VACUUM
# can't be executed within a transaction block. So create a
# separate connection that uses autocommit.
url = Configuration.database_url()
engine = create_engine(url, isolation_level="AUTOCOMMIT")
engine.autocommit = True
a = time.time()
engine.execute("VACUUM (VERBOSE, ANALYZE)")
b = time.time()
print "Vacuumed in %.2f sec." % (b-a)
class DatabaseMigrationScript(Script):
"""Runs new migrations"""
name = "Database Migration"
MIGRATION_WITH_COUNTER = re.compile("\d{8}-(\d+)-(.)+\.(py|sql)")
@classmethod
def arg_parser(cls):
parser = argparse.ArgumentParser()
parser.add_argument(
'-d', '--last-run-date',
help=('A date string representing the last migration file '
'run against your database, formatted as YYYY-MM-DD')
)
parser.add_argument(
'-c', '--last-run-counter', type=int,
help=('An optional digit representing the counter of the last '
'migration run against your database. Only necessary if '
'multiple migrations were created on the same date.')
)
return parser
@classmethod
def migratable_files(cls, filelist):
"""Filter a list of files for migratable file extensions"""
migratable = [f for f in filelist
if (f.endswith('.py') or f.endswith('.sql'))]
return cls.sort_migrations(migratable)
@classmethod
def sort_migrations(self, migrations):
"""Ensures that migrations with a counter digit are sorted after
migrations without one.
"""
def compare_migrations(first, second):
"""Compares migrations according to ideal sorting order.
- Migrations are first ordered by timestamp (asc).
- If two migrations have the same timestamp, any migrations
without counters come before migrations with counters.
- If two migrations with the same timestamp, have counters,
migrations are sorted by counter (asc).
"""
first_datestamp = int(first[:8])
second_datestamp = int(second[:8])
datestamp_difference = first_datestamp - second_datestamp
if datestamp_difference != 0:
return datestamp_difference
# Both migrations have the same timestamp, so compare using
# their counters (default to 0 if no counter is included)
first_count = self.MIGRATION_WITH_COUNTER.search(first) or 0
second_count = self.MIGRATION_WITH_COUNTER.search(second) or 0
if not isinstance(first_count, int):
first_count = int(first_count.groups()[0])
if not isinstance(second_count, int):
second_count = int(second_count.groups()[0])
return first_count - second_count
return sorted(migrations, cmp=compare_migrations)
@property
def directories_by_priority(self):
"""Returns a list containing the migration directory path for core
and its container server, organized in priority order (core first)
"""
current_dir = os.path.split(os.path.abspath(__file__))[0]
core = os.path.join(current_dir, 'migration')
server = os.path.join(os.path.split(current_dir)[0], 'migration')
# Core is listed first, since core makes changes to the core database
# schema. Server migrations generally fix bugs or otherwise update
# the data itself.
return [core, server]
def do_run(self):
args = self.parse_command_line()
last_run_date = args.last_run_date
last_run_counter = args.last_run_counter
existing_timestamp = get_one(self._db, Timestamp, service=self.name)
if last_run_date:
last_run_datetime = self.parse_time(last_run_date)
if existing_timestamp:
existing_timestamp.timestamp = last_run_datetime
if last_run_counter:
existing_timestamp.counter = last_run_counter
else:
existing_timestamp, ignore = get_one_or_create(
self._db, Timestamp,
service=self.name,
timestamp=last_run_datetime
)
if existing_timestamp:
migrations, migrations_by_dir = self.fetch_migration_files()
new_migrations = self.get_new_migrations(
existing_timestamp, migrations
)
if new_migrations:
# Log the new migrations.
print "%d new migrations found." % len(new_migrations)
for migration in new_migrations:
print " - %s" % migration
self.run_migrations(
new_migrations, migrations_by_dir, existing_timestamp
)
else:
print "No new migrations found. Your database is up-to-date."
else:
print ""
print (
"NO TIMESTAMP FOUND. Run script with timestamp that indicates"
" the last migration run against this database."
)
self.arg_parser().print_help()
def fetch_migration_files(self):
"""Pulls migration files from the expected locations
Return a list of migration filenames and a dictionary of those
files separated by their absolute directory location.
"""
migrations = list()
migrations_by_dir = defaultdict(list)
for directory in self.directories_by_priority:
# In the case of tests, the container server migration directory
# may not exist.
if os.path.isdir(directory):
dir_migrations = self.migratable_files(os.listdir(directory))
migrations += dir_migrations
migrations_by_dir[directory] = dir_migrations
return migrations, migrations_by_dir
def get_new_migrations(self, timestamp, migrations):
"""Return a list of migration filenames, representing migrations
created since the timestamp
"""
last_run = timestamp.timestamp.strftime('%Y%m%d')
migrations = self.sort_migrations(migrations)
new_migrations = [migration for migration in migrations
if int(migration[:8]) >= int(last_run)]
# Multiple migrations run on the same day have an additional digit
# after the date and a dash, eg:
#
# 20150826-1-change_target_age_from_int_to_range.sql
#
# When that migration is run, the number will be saved to the
# 'counter' column of Timestamp, so we have to account for that.
start_found = False
later_found = False
index = 0
while not start_found and not later_found and index < len(new_migrations):
start_found, later_found = self._is_matching_migration(
new_migrations[index], timestamp
)
index += 1
if later_found:
index -= 1
new_migrations = new_migrations[index:]
return new_migrations
def _is_matching_migration(self, migration_file, timestamp):
"""Determine whether a given migration filename matches a given
timestamp or is after it.
"""
is_match = False
is_after_timestamp = False
timestamp_str = timestamp.timestamp.strftime('%Y%m%d')
counter = timestamp.counter
if migration_file[:8]>=timestamp_str:
if migration_file[:8]>timestamp_str:
is_after_timestamp = True
elif counter:
count = self.MIGRATION_WITH_COUNTER.search(migration_file)
if count:
migration_num = int(count.groups()[0])
if migration_num==counter:
is_match = True
if migration_num > counter:
is_after_timestamp = True
else:
is_match = True
return is_match, is_after_timestamp
def run_migrations(self, migrations, migrations_by_dir, timestamp):
"""Run each migration, first by timestamp and then by directory
priority.
"""
previous = None
migrations = self.sort_migrations(migrations)
for migration_file in migrations:
for d in self.directories_by_priority:
if migration_file in migrations_by_dir[d]:
full_migration_path = os.path.join(d, migration_file)
try:
self._run_migration(full_migration_path, timestamp)
self._db.commit()
previous = migration_file
except Exception:
print
print "ERROR: Migration has been halted."
print "%s must be migrated manually." % full_migration_path
print "=" * 50
print traceback.print_exc(file=sys.stdout)
sys.exit(1)
else:
print "All new migrations have been run."
def _run_migration(self, migration_path, timestamp):
"""Runs a single SQL or Python migration file"""
migration_filename = os.path.split(migration_path)[1]
if migration_path.endswith('.sql'):
with open(migration_path) as clause:
sql = clause.read()
self._db.execute(sql)
if migration_path.endswith('.py'):
module_name = migration_filename[:-3]
imp.load_source(module_name, migration_path)
# Update timestamp for the migration.
self.update_timestamp(timestamp, migration_filename)
def update_timestamp(self, timestamp, migration_file):
"""Updates this service's timestamp to match a given migration"""
last_run_date = self.parse_time(migration_file[0:8])
timestamp.timestamp = last_run_date
# When multiple migration files are created on the same date, an
# additional number is added. This number is held in the 'counter'
# column of Timestamp.
# (It's not ideal, but it avoids creating a new database table.)
match = self.MIGRATION_WITH_COUNTER.search(migration_file)
if match:
timestamp.counter = int(match.groups()[0])
self._db.commit()
print "New timestamp created at %s for %s" % (
last_run_date.strftime('%Y-%m-%d'), migration_file
)
class DatabaseMigrationInitializationScript(DatabaseMigrationScript):
"""Creates a timestamp to kickoff the regular use of
DatabaseMigrationScript to manage migrations.
"""
def do_run(self):
existing_timestamp = get_one(self._db, Timestamp, service=self.name)
if existing_timestamp:
raise Exception(
"Timestamp for Database Migration script already exists"
)
migrations = self.fetch_migration_files()[0]
most_recent_migration = self.sort_migrations(migrations)[-1]
initial_timestamp = Timestamp.stamp(self._db, self.name)
self.update_timestamp(initial_timestamp, most_recent_migration)
class Explain(IdentifierInputScript):
"""Explain everything known about a given work."""
def run(self):
args = self.parse_command_line(self._db)
identifier_ids = [x.id for x in args.identifiers]
editions = self._db.query(Edition).filter(
Edition.primary_identifier_id.in_(identifier_ids)
)
#policy = PresentationCalculationPolicy.recalculate_everything()
policy = None
for edition in editions:
self.explain(self._db, edition, policy)