-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBMirror.pl
executable file
·156 lines (122 loc) · 4.35 KB
/
DBMirror.pl
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
#!/usr/bin/env perl
# This file is part of MusicBrainz, the open internet music database.
# Copyright (C) 2021 MetaBrainz Foundation
# Licensed under the GPL version 2, or (at your option) any later version:
# http://www.gnu.org/licenses/gpl-2.0.txt
use strict;
use warnings;
use DBI;
use Getopt::Long;
use JSON::XS;
my $database = '';
my $username = '';
GetOptions(
'database=s' => \$database,
'username=s' => \$username,
);
die 'database not specified' unless $database;
die 'username not specified' unless $username;
my $dbh = DBI->connect("dbi:Pg:dbname=$database", $username, '', {
AutoCommit => 0,
RaiseError => 1,
});
my $json = JSON::XS->new->allow_nonref;
sub get_columns_and_values {
my $data = $_[0];
return unless defined $data;
my @columns = sort { $a cmp $b } keys %{$data};
my @values = map {
my $value = $data->{$_};
ref($value) ? $json->encode($value) : $value;
} @columns;
return (\@columns, \@values);
}
$dbh->do(q{
DECLARE csr1 CURSOR WITH HOLD FOR
SELECT xid
FROM dbmirror2.pending_data
GROUP BY xid
ORDER BY max(seqid)
});
$dbh->commit;
my $sth_cursor = $dbh->prepare('FETCH 100 FROM csr1');
my $sth_pending = $dbh->prepare(q{
SELECT pd.seqid, pd.tablename, pd.op, pd.olddata, pd.newdata, pk.keys
FROM dbmirror2.pending_data pd
JOIN (
SELECT table_schema, table_name,
array_agg(column_name)::TEXT[] AS keys
FROM dbmirror2.column_info
WHERE is_primary = TRUE
GROUP BY table_schema, table_name
) pk ON parse_ident(pd.tablename) =
ARRAY[pk.table_schema, pk.table_name]::TEXT[]
WHERE pd.xid = ?
ORDER BY pd.seqid ASC
});
my $sth_delete_xid = $dbh->prepare(
'DELETE FROM dbmirror2.pending_data WHERE xid = ?');
my $sth_table_exists = $dbh->prepare(
'SELECT 1 FROM pg_catalog.pg_class WHERE oid = ?::regclass');
while (1) {
$sth_cursor->execute;
last unless $sth_cursor->rows;
while (my $row1 = $sth_cursor->fetchrow_hashref) {
$dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
$dbh->do('SET CONSTRAINTS ALL DEFERRED');
$sth_pending->execute($row1->{xid});
while (my $row2 = $sth_pending->fetchrow_arrayref) {
my ($seq_id, $table_name, $op, $old_data_json, $new_data_json, $keys) = @{$row2};
my $old_data = defined $old_data_json ? $json->decode($old_data_json) : undef;
my $new_data = defined $new_data_json ? $json->decode($new_data_json) : undef;
$sth_table_exists->execute($table_name);
my ($table_exists) = @{ $sth_table_exists->fetchrow_arrayref // [] };
unless ($table_exists) {
print 'Warning: ';
next;
}
my ($old_columns, $old_values) = get_columns_and_values($old_data);
my ($new_columns, $new_values) = get_columns_and_values($new_data);
if ($op eq 'i') {
my $columns = join q(, ),
map { $dbh->quote_identifier($_) } @$new_columns;
my $placholders = join q(, ), (('?') x @$new_columns);
$dbh->do(
"INSERT INTO $table_name ($columns) VALUES ($placholders)",
undef,
@$new_values,
);
next;
}
my $conditions = join ' AND ',
map { $dbh->quote_identifier($_) . ' = ?' }
@$keys;
my @key_data = map { $old_data->{$_} } @$keys;
if ($op eq 'u') {
my $updates = join q(, ),
map { $dbh->quote_identifier($_) . ' = ?' }
@$new_columns;
$dbh->do(
"UPDATE $table_name SET $updates WHERE $conditions",
undef,
@$new_values,
@key_data,
);
} elsif ($op eq 'd') {
$dbh->do(
"DELETE FROM $table_name WHERE $conditions",
undef,
@key_data,
);
}
}
$sth_delete_xid->execute($row1->{xid});
$dbh->commit;
}
}
$dbh->do('CLOSE csr1');
$sth_table_exists->finish;
$sth_delete_xid->finish;
$sth_pending->finish;
$sth_cursor->finish;
$dbh->disconnect;