Created
February 27, 2010 06:30
-
-
Save alehmann/316522 to your computer and use it in GitHub Desktop.
rubyrep / postgres: syncing tables with circular foreign key constraints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
psql postgres -c "create database circular_fkeys_left" | |
psql circular_fkeys_left <<EOS | |
create table foos(id integer primary key, bar_id integer); | |
create table bars(id integer primary key, foo_id integer); | |
insert into foos(id, bar_id) values(1, 5); | |
insert into bars(id, foo_id) values(5, 1); | |
alter table foos add constraint bar_fk foreign key(bar_id) references bars; | |
alter table bars add constraint foo_fk foreign key(foo_id) references foos; | |
EOS | |
psql postgres -c "create database circular_fkeys_right" | |
psql circular_fkeys_right <<EOS | |
create table foos(id integer primary key, bar_id integer); | |
create table bars(id integer primary key, foo_id integer); | |
alter table foos add constraint bar_fk foreign key(bar_id) references bars; | |
alter table bars add constraint foo_fk foreign key(foo_id) references foos; | |
EOS | |
cat >circular_fkeys <<EOF | |
RR::Initializer::run do |config| | |
config.left = { | |
:adapter => 'postgresql', # or 'mysql' | |
:database => 'circular_fkeys_left', | |
:username => 'postgres', | |
:password => 'tiger', | |
:host => '127.0.0.1' | |
} | |
config.right = { | |
:adapter => 'postgresql', | |
:database => 'circular_fkeys_right', | |
:username => 'postgres', | |
:password => 'tiger', | |
:host => '127.0.0.1' | |
} | |
config.include_tables /./ | |
config.add_table_options 'foos', | |
:before_table_sync => "alter table foos disable trigger all; alter table bars disable trigger all" | |
config.add_table_options 'bars', | |
:after_table_sync => "alter table foos enable trigger all; alter table bars enable trigger all" | |
end | |
EOF | |
rubyrep scan -c circular_fkeys | |
rubyrep sync -c circular_fkeys | |
rubyrep scan -c circular_fkeys | |
psql postgres -c "drop database circular_fkeys_left" | |
psql postgres -c "drop database circular_fkeys_right" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
My rubyrep output: | |
alehmann@alehmann-laptop:~/tmp$ rubyrep scan -c circular_fkeys | |
bars 100% ......................... 1 | |
foos 100% ......................... 1 | |
alehmann@alehmann-laptop:~/tmp$ rubyrep sync -c circular_fkeys | |
foos 100% ......................... 1 | |
bars 100% ......................... 1 | |
alehmann@alehmann-laptop:~/tmp$ rubyrep scan -c circular_fkeys | |
bars 100% ......................... 0 | |
foos 100% ......................... 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment