Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

psql splitter for php

by TheloniusMonk (Sexton)
on Jan 22, 2021 at 12:13 UTC ( #11127266=perlquestion: print w/replies, xml ) Need Help??

TheloniusMonk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am working on a migration project, where there will be parallel run for all the migrated servers for a period of time. One of these servers is a LAPP stack with cron jobs written in bash and php.

During the parallel run, it is necessary to be able to turn off production scripts in the old environment as soon as the migrated versions are tested because there are as yet unmigrated shared storage devices in the landscape.

So, although the storage will be updated by the new servers, instead of the old, for such scripts, a psql splitter is needed in the case of psql.

For bash, I have a working solution: In the bash scripts I alias psql to ~/bin/psql.pl, and this script contacts two databases instead of one, transparently to the bash script (see below).

My challenge for today is to do the same for php scripts. I.E., create a similar Perl script as the below, but find a way to alias calls like $link = pg_connect('host=localhost dbname=DBNAME user=USERNAME password=XYZ');
So that subsequent references to $link will send the SQL to two databases similarly to the below solution for bash. I can't begin to write the phppsql.pl for this not knowing how the interface will look. Bear in mind there are a large number of such php scripts.

#!/usr/bin/perl use strict; use warnings; my $g=main->new; $g->init; $g->parse; $g->enact; package main; sub new { return bless {}, shift; } sub init { my $this = shift; $this->{host1} = '<new-server>.com'; $this->{host2} = '<old-server>.com'; } sub parse { my $this = shift; my $cmd = join(' ', @ARGV); my ($host); if ($cmd =~ s/\-h\s*(\S*)//) { $host = $1; unless ($host eq $this->{host1}) { $this->{host1} = $host; $this->{host2} = ''; } } if ($cmd =~ s/\-c\s*(.*)$//) { $this->{sql} = $1; } else { $this->{sql} = ''; } $this->{cmd1} = '/bin/psql -h ' . $this->{host1} . ' ' . $cmd;; $this->{host2} and $this->{cmd2} = '/bin/psql -h ' . $this->{host2} . ' ' . $cm +d; } sub enact { my $this = shift; if ($this->{sql}) { my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; } } else { my @stdin = <STDIN>; my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph @stdin; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph @stdin; close $ph; waitpid $pid, 0; } } } 1; __END__
Many thanks in advance!

T.M.

Replies are listed 'Best First'.
Re: psql splitter for php
by tobyink (Canon) on Jan 22, 2021 at 17:07 UTC

    If you're using the PHP function-based interface for PostgreSQL, you're probably out of luck.

    If you use PDO (the object-oriented interface, which is basically PHP's version of Perl's DBI), then it's not a lot of work. Just write your own class that implements DBO's interface.

Re: psql splitter for php
by erix (Prior) on Jan 22, 2021 at 20:58 UTC

    Perhaps you can address the two postgres servers by using service names. The servicename can go into an environment variable. The environment setup can be bound to (xterm-)sessions.

    (The information in this question is inadequate)

Re: psql splitter for php
by betmatt (Scribe) on Jan 22, 2021 at 17:58 UTC
    In this situation I would produce SQL statement files. I would have these files duplicated and ordered by execution order. I would then have an agent based system for each database where the updates to the database are made in order for each copy of the SQL files for each of the database versions. There are some situation where this will not work, depending of the SQL logic. In most cases it would work. Do you have knowledge of what SQL you would need to perform?
Re: psql splitter for php
by LanX (Sage) on Jan 22, 2021 at 16:04 UTC

    > but find a way to alias calls like $link = pg_connect('host=localhost dbname=DBNAME user=USERNAME password=XYZ');

    You are asking for a PHP alias/overriding magic in a Perl board ...

    Quite off-topic, don't you think?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: psql splitter for php
by Anonymous Monk on Jan 22, 2021 at 12:23 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11127266]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2021-11-29 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?