| 1 | #!/usr/bin/perl | ||
| 2 | # lib/db_migrate.pl -- schema update, by historisation or in place (the | ||
| 3 | # fork-integration plan, annexe D, steps S51b and S51c). A thin CLI over | ||
| 4 | # oEdtk::DBAdmin. | ||
| 5 | # | ||
| 6 | # Modes: | ||
| 7 | # --check-only detect the schema level and print the plan; NOTHING is written | ||
| 8 | # --dry-run like --check-only, plus the copy SQL and a DDL preview | ||
| 9 | # --historize build a T target, copy by named columns, validate, switch | ||
| 10 | # over and KEEP the old schema as history (recommended); a base | ||
| 11 | # already at T is a no-op unless --force is given | ||
| 12 | # --revert undo a --historize run before resuming the service | ||
| 13 | # --additive in-place additive update (S51c): ADD COLUMN, widening and | ||
| 14 | # CREATE ... IF NOT EXISTS only -- never a DROP, a narrowing or a | ||
| 15 | # rename; --with-constraints also changes the key (after a | ||
| 16 | # duplicate pre-check). Combined with --dry-run it prints the | ||
| 17 | # same update as a READ-ONLY preview and writes nothing. | ||
| 18 | # | ||
| 19 | # --historize never modifies the service schema in place and never drops. It is | ||
| 20 | # the recommended procedure when the key or the types change. --additive is | ||
| 21 | # strictly monotone and is suited to the cases without a key change. | ||
| 22 | # | ||
| 23 | # Usage: | ||
| 24 | # perl lib/db_migrate.pl --check-only --dsn <dsn> [--user U] [--pass P] | ||
| 25 | # perl lib/db_migrate.pl --dry-run --dsn <dsn> | ||
| 26 | # perl lib/db_migrate.pl --historize --dsn <dsn> [--stamp YYYYMMDD_HHMMSS] | ||
| 27 | # perl lib/db_migrate.pl --revert --dsn <dsn> --stamp YYYYMMDD_HHMMSS | ||
| 28 | # perl lib/db_migrate.pl --additive --dsn <dsn> [--with-constraints] | ||
| 29 | # perl lib/db_migrate.pl --additive --dry-run --dsn <dsn> | ||
| 30 | # | ||
| 31 | # READ-ONLY for --check-only / --dry-run. The password is never printed. | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | |||
| 36 | use FindBin; | ||
| 37 | use lib "$FindBin::Bin"; | ||
| 38 | use DBI; | ||
| 39 | use Getopt::Long qw(GetOptions); | ||
| 40 | |||
| 41 | use oEdtk::DBAdmin qw( | ||
| 42 | schema_MigrationPlan | ||
| 43 | schema_Historize | ||
| 44 | schema_Revert | ||
| 45 | schema_Additive | ||
| 46 | schema_AdditivePlan | ||
| 47 | schema_Report | ||
| 48 | ); | ||
| 49 | |||
| 50 | sub usage { | ||
| 51 | print STDERR "usage: $0 <mode> --dsn <dsn> [--user U] [--pass P]\n" | ||
| 52 | . " modes: --check-only | --dry-run | --historize [--force] | --revert --stamp S" | ||
| 53 | . " | --additive [--with-constraints] [--dry-run]\n"; | ||
| 54 | exit 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub mask_dsn { | ||
| 58 | my ($dsn) = @_; | ||
| 59 | $dsn =~ s/(pass(word)?=)[^;]*/$1***/gi; | ||
| 60 | return $dsn; | ||
| 61 | } | ||
| 62 | |||
| 63 | my ( $dsn, $user, $pass, $stamp ); | ||
| 64 | my ( $check, $dry, $historize, $revert, $additive, $force, $with_constraints ); | ||
| 65 | GetOptions( | ||
| 66 | 'dsn=s' => \$dsn, | ||
| 67 | 'user=s' => \$user, | ||
| 68 | 'pass=s' => \$pass, | ||
| 69 | 'stamp=s' => \$stamp, | ||
| 70 | 'check-only' => \$check, | ||
| 71 | 'dry-run' => \$dry, | ||
| 72 | 'historize' => \$historize, | ||
| 73 | 'revert' => \$revert, | ||
| 74 | 'additive' => \$additive, | ||
| 75 | 'force' => \$force, | ||
| 76 | 'with-constraints' => \$with_constraints, | ||
| 77 | ) or usage(); | ||
| 78 | |||
| 79 | my @modes = grep { $_ } ( $check, $dry, $historize, $revert, $additive ); | ||
| 80 | |||
| 81 | # --additive --dry-run previews the in-place update read-only; it is the only | ||
| 82 | # two-mode combination allowed. | ||
| 83 | my $additive_preview = ( $additive && $dry && @modes == 2 ) ? 1 : 0; | ||
| 84 | usage() unless @modes == 1 || $additive_preview; | ||
| 85 | if ( $with_constraints && !$additive ) { | ||
| 86 | print STDERR "db_migrate: --with-constraints requires --additive\n"; | ||
| 87 | usage(); | ||
| 88 | } | ||
| 89 | usage() unless defined $dsn; | ||
| 90 | |||
| 91 | my $dbh = DBI->connect( $dsn, $user, $pass, { RaiseError => 1, PrintError => 0, AutoCommit => 1 } ) | ||
| 92 | or die "db_migrate: cannot connect to " . mask_dsn($dsn) . ": $DBI::errstr\n"; | ||
| 93 | |||
| 94 | if ($additive_preview) { | ||
| 95 | my $plan = | ||
| 96 | schema_AdditivePlan( $dbh, ( $with_constraints ? ( with_constraints => 1 ) : () ) ); | ||
| 97 | print join( "\n", @{ $plan->{report} } ), "\n"; | ||
| 98 | print "\n-- in-place DDL (illustrative; nothing is written) --\n"; | ||
| 99 | print " $_;\n" | ||
| 100 | for @{ $plan->{add_sql} }, @{ $plan->{widen_sql} }, @{ $plan->{key_sql} }; | ||
| 101 | $dbh->disconnect; | ||
| 102 | exit 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( $check || $dry ) { | ||
| 106 | my $plan = schema_MigrationPlan( $dbh, ( defined $stamp ? ( stamp => $stamp ) : () ) ); | ||
| 107 | print join( "\n", @{ $plan->{report} } ), "\n"; | ||
| 108 | if ($dry) { | ||
| 109 | print "\n-- copy statements --\n"; | ||
| 110 | print " $_;\n" for @{ $plan->{copy_sql} }; | ||
| 111 | my @ddl = sort map { $_->{ddl} } values %{ $plan->{model} }; | ||
| 112 | print "\n-- target DDL (as generated for SQLite; illustrative) --\n"; | ||
| 113 | print " $_;\n" for grep { defined } @ddl; | ||
| 114 | } | ||
| 115 | $dbh->disconnect; | ||
| 116 | exit 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($additive) { | ||
| 120 | my $res = schema_Additive( | ||
| 121 | $dbh, | ||
| 122 | ( $force ? ( force => 1 ) : () ), | ||
| 123 | ( $with_constraints ? ( with_constraints => 1 ) : () ) | ||
| 124 | ); | ||
| 125 | if ( $res->{noop} ) { | ||
| 126 | print "db_migrate: already at T, nothing to add or widen.\n"; | ||
| 127 | $dbh->disconnect; | ||
| 128 | exit 0; | ||
| 129 | } | ||
| 130 | if ( $res->{ok} ) { | ||
| 131 | printf | ||
| 132 | "db_migrate: additive update %s -> T: %d table(s) created, %d column(s) added, %d widened\n", | ||
| 133 | $res->{from}, | ||
| 134 | scalar @{ $res->{created} }, | ||
| 135 | scalar( map { @$_ } values %{ $res->{added} } ), | ||
| 136 | scalar( map { @$_ } values %{ $res->{widened} } ); | ||
| 137 | print " unresolved: $_\n" for @{ $res->{violations} }; | ||
| 138 | $dbh->disconnect; | ||
| 139 | exit 0; | ||
| 140 | } | ||
| 141 | print STDERR "db_migrate: additive update aborted; schema unchanged: $res->{error}\n"; | ||
| 142 | $dbh->disconnect; | ||
| 143 | exit 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | if ($historize) { | ||
| 147 | my $res = schema_Historize( | ||
| 148 | $dbh, | ||
| 149 | ( defined $stamp ? ( stamp => $stamp ) : () ), | ||
| 150 | ( $force ? ( force => 1 ) : () ) | ||
| 151 | ); | ||
| 152 | if ( $res->{noop} ) { | ||
| 153 | print "db_migrate: already at T, nothing to do (use --force to re-historise).\n"; | ||
| 154 | $dbh->disconnect; | ||
| 155 | exit 0; | ||
| 156 | } | ||
| 157 | if ( $res->{ok} ) { | ||
| 158 | printf "db_migrate: historised %s -> T, stamp %s, %d table(s) kept as history\n", | ||
| 159 | $res->{from}, $res->{stamp}, scalar @{ $res->{renamed} }; | ||
| 160 | print " rows: ", join( ' ', map { "$_=$res->{rows}{$_}" } sort keys %{ $res->{rows} } ), | ||
| 161 | "\n"; | ||
| 162 | $dbh->disconnect; | ||
| 163 | exit 0; | ||
| 164 | } | ||
| 165 | print STDERR "db_migrate: historisation aborted; service schema untouched: $res->{error}\n"; | ||
| 166 | $dbh->disconnect; | ||
| 167 | exit 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | # --revert | ||
| 171 | usage() unless defined $stamp; | ||
| 172 | my $res = schema_Revert( $dbh, stamp => $stamp ); | ||
| 173 | if ( $res->{ok} ) { | ||
| 174 | printf "db_migrate: reverted %d table(s) from stamp %s\n", | ||
| 175 | scalar @{ $res->{reverted} }, $res->{stamp}; | ||
| 176 | $dbh->disconnect; | ||
| 177 | exit 0; | ||
| 178 | } | ||
| 179 | print STDERR "db_migrate: revert aborted: $res->{error}\n"; | ||
| 180 | $dbh->disconnect; | ||
| 181 | exit 1; |