Line # | Revision | Author | |
---|---|---|---|
1 | 760 | ahitrov | #!/usr/bin/env perl |
2 | 292 | ahitrov | |
3 | use strict; | ||
4 | use warnings "all"; | ||
5 | use locale; | ||
6 | |||
7 | use FindBin; | ||
8 | BEGIN { | ||
9 | require "$FindBin::RealBin/../lib/Modules.pm"; | ||
10 | } | ||
11 | |||
12 | use Contenido::Globals; | ||
13 | use Contenido::Init; | ||
14 | use ErrorTee; | ||
15 | use PidFile; | ||
16 | use Data::Dumper; | ||
17 | use JSON::XS; | ||
18 | |||
19 | # begin | ||
20 | Contenido::Init->init(); | ||
21 | |||
22 | my $keeper_module = $state->project.'::Keeper'; | ||
23 | $keeper = $keeper_module->new($state); | ||
24 | |||
25 | #PidFile->new($keeper, compat=>1); # db-based locking (run only on one host) | ||
26 | #PidFile->new($keeper, compat=>1, per_host=>1); # db-based locking (run on whole cluster) | ||
27 | |||
28 | ############################################ | ||
29 | # please use: | ||
30 | # $state->{log_dir} for logging | ||
31 | # $state->{tmp_dir} for temporary files | ||
32 | ########################################### | ||
33 | my $table = $ARGV[0] || 'sections'; | ||
34 | my $LIMIT = 10; | ||
35 | my $OFFSET = 0; | ||
36 | my $json = JSON::XS->new; | ||
37 | |||
38 | print "Alter table:\n"; | ||
39 | my $res = $keeper->SQL->do("alter table $table add column data__json text"); | ||
40 | print "Reading from $table...\n"; | ||
41 | my $i = 2; | ||
42 | while ( $i ) { | ||
43 | my $sql = $keeper->SQL->prepare("select id, data from $table order by id limit $LIMIT offset $OFFSET"); | ||
44 | my $res = $sql->execute; | ||
45 | if ( $res ne '0E0' ) { | ||
46 | while ( my $ln = $sql->fetchrow_hashref ) { | ||
47 | next unless $ln->{data}; | ||
48 | my $VAR1; eval "$ln->{data}"; | ||
49 | my $STRUCT = $VAR1; | ||
50 | print Dumper $STRUCT; | ||
51 | if ( ref $STRUCT eq 'HASH' ) { | ||
52 | while ( my ($field, $val) = each %$STRUCT ) { | ||
53 | if ( $val && $val =~ /\$VAR1/ ) { | ||
54 | my $VAL = eval "$val"; | ||
55 | if ( ref $VAL ) { | ||
56 | print Dumper $VAL; | ||
57 | $STRUCT->{$field} = $json->encode($VAL); | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | my $DATA = Encode::decode('utf-8', $json->encode( $STRUCT )); | ||
63 | my $store = $keeper->SQL->prepare("update $table set data__json = ? where id = ?"); | ||
64 | $store->execute( $DATA, $ln->{id} ); | ||
65 | } | ||
66 | } else { | ||
67 | print "Stop\n"; | ||
68 | last; | ||
69 | } | ||
70 | $OFFSET += $LIMIT; | ||
71 | } | ||
72 | print "Rename column [data] to [data__dumper]\n"; | ||
73 | $keeper->SQL->do("alter table $table rename COLUMN data TO data__dumper"); | ||
74 | print "Rename column [data__json] to [data]\n"; | ||
75 | $keeper->SQL->do("alter table $table rename COLUMN data__json TO data"); |