migrate.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use FindBin;
  6. use FindBin::libs;
  7. use Getopt::Long qw{GetOptions};
  8. use Time::HiRes ();
  9. use Trog::Config;
  10. use Trog::Data::FlatFile;
  11. use Trog::Data::SQLite;
  12. =head1 SYNOPSIS
  13. Move a site from the FlatFile data model to the SQLite one.
  14. =head2 USAGE
  15. bin/migrate.pl [--dry-run] [--batch N] [--quiet]
  16. Must be run from the tCMS root, as the data paths are relative to it.
  17. --dry-run Say what would be carried over, and write nothing.
  18. --batch N Posts per transaction. Default 500.
  19. --quiet Only complain; say nothing on success.
  20. =head2 WHAT IT DOES
  21. Reads every version of every post out of data/files and writes them into
  22. data/posts.sqlite, oldest version first, so that the version bookkeeping ends up
  23. the way it would have if the posts had been written there all along.
  24. Nothing is deleted. data/files is left exactly as it found it, and so is the
  25. flat file model's tag index in data/posts.db, so the way back is to put
  26. data_model back to FlatFile.
  27. Safe to re-run. Post versions already carried over are skipped rather than
  28. duplicated, which also makes it safe to run once against a live site and again
  29. after a final quiet period to pick up whatever was written in between.
  30. When it has finished, switch the site over in config/main.cfg:
  31. [general]
  32. data_model=SQLite
  33. ...and restart, so the workers build the new model rather than the old one.
  34. =head2 TERMINATION CONDITIONS
  35. Exits non-zero if any post could not be carried over, having written everything
  36. it could. Each batch is a transaction, so a batch that fails is not half
  37. applied, and re-running picks up from what actually landed.
  38. =cut
  39. # So that progress and complaints interleave in the order they happened when
  40. # this is piped to a log rather than watched.
  41. STDOUT->autoflush(1);
  42. STDERR->autoflush(1);
  43. my $usage = "Usage: bin/migrate.pl [--dry-run] [--batch N] [--quiet]\n";
  44. # Printed and exited rather than died: Carp::Always is in the dependency chain,
  45. # and a usage message wearing a stack trace reads like a crash.
  46. sub usage_error (@complaint) {
  47. print STDERR @complaint, $usage;
  48. exit 2;
  49. }
  50. my ( $dry_run, $quiet, $batch_size ) = ( 0, 0, 500 );
  51. GetOptions(
  52. 'dry-run' => \$dry_run,
  53. 'quiet' => \$quiet,
  54. 'batch=i' => \$batch_size,
  55. ) or usage_error();
  56. usage_error("--batch must be a positive number of posts.\n") if $batch_size < 1;
  57. sub say_unless_quiet (@args) {
  58. return if $quiet;
  59. say @args;
  60. return;
  61. }
  62. my $config = Trog::Config::get();
  63. my $from = Trog::Data::FlatFile->new($config);
  64. my $to = Trog::Data::SQLite->new($config);
  65. # raw, so we get the history rather than only the current version of each post,
  66. # and so that no acl filtering stands between a migration and the data it is
  67. # supposed to be moving.
  68. my $started = [ Time::HiRes::gettimeofday() ];
  69. my @posts = $from->get( raw => 1, limit => 0 );
  70. if ( !@posts ) {
  71. print STDERR "No posts found in data/files -- is this a tCMS root, and is it on the FlatFile model?\n";
  72. exit 2;
  73. }
  74. # Oldest first: write() appends, and the version bookkeeping is maintained as it
  75. # goes, so feeding it in order is what makes first_id and latest_id right.
  76. @posts = sort { ( $a->{id} // '' ) cmp ( $b->{id} // '' ) || ( $a->{version} // 0 ) <=> ( $b->{version} // 0 ) } @posts;
  77. # One query rather than one per post.
  78. my $present = $to->versions_present();
  79. # Separately: an array in a list assignment swallows everything after it.
  80. my @batch;
  81. my ( $written, $skipped, $failed ) = ( 0, 0, 0 );
  82. sub flush_batch () {
  83. return unless @batch;
  84. if ($dry_run) {
  85. $written += scalar(@batch);
  86. @batch = ();
  87. return;
  88. }
  89. local $@;
  90. eval {
  91. $to->write( \@batch );
  92. $written += scalar(@batch);
  93. 1;
  94. } or do {
  95. my $error = $@;
  96. $error =~ s/\n.*//s;
  97. # A batch is one transaction, so one bad post took every other post in
  98. # the batch down with it. Go back over them one at a time: a migration
  99. # wants everything that can land to land, and a list naming exactly what
  100. # could not -- not five hundred posts rejected on account of one.
  101. say STDERR "A batch of " . scalar(@batch) . " failed ($error); retrying it one post at a time.";
  102. foreach my $post (@batch) {
  103. local $@;
  104. eval {
  105. $to->write( [$post] );
  106. $written++;
  107. 1;
  108. } or do {
  109. my $why = $@;
  110. $why =~ s/\n.*//s;
  111. $failed++;
  112. say STDERR " could not write $post->{id} version " . ( $post->{version} // 0 ) . ": $why";
  113. };
  114. }
  115. };
  116. @batch = ();
  117. return;
  118. }
  119. foreach my $post (@posts) {
  120. if ( !$post->{id} ) {
  121. say STDERR "Skipping a post with no id, which is not something this can carry over";
  122. $failed++;
  123. next;
  124. }
  125. if ( $present->{ $post->{id} }{ $post->{version} // 0 } ) {
  126. $skipped++;
  127. next;
  128. }
  129. push( @batch, $post );
  130. flush_batch() if @batch >= $batch_size;
  131. }
  132. flush_batch();
  133. my $elapsed = Time::HiRes::tv_interval($started);
  134. if ($dry_run) {
  135. say_unless_quiet( sprintf( "Would carry over %d post version(s); %d already present.", $written, $skipped ) );
  136. say_unless_quiet("Nothing was written. Re-run without --dry-run to do it.");
  137. exit( $failed ? 1 : 0 );
  138. }
  139. # Worth stating plainly rather than trusting the counters: they say what this
  140. # thought it did, and the databases say what is actually there.
  141. my $expected = scalar( grep { $_->{id} } @posts );
  142. my $have = 0;
  143. $have += scalar( keys %$_ ) foreach values %{ $to->versions_present() };
  144. say_unless_quiet( sprintf( "Carried over %d post version(s), skipped %d already present, in %.1fs.", $written, $skipped, $elapsed ) );
  145. say_unless_quiet( sprintf( "data/files holds %d post version(s); data/posts.sqlite now holds %d.", $expected, $have ) );
  146. if ( $failed || $have < $expected ) {
  147. say STDERR sprintf( "%d post version(s) did not make it across. Fix what the errors above name and re-run; what landed will be skipped.", $failed || ( $expected - $have ) );
  148. exit 1;
  149. }
  150. say_unless_quiet("Set general.data_model to SQLite in config/main.cfg and restart to serve from it.");
  151. exit 0;