0

I have an Application that provide web service, but i had a problem with the database. The problem is, my database will always update each day so my dba creates another db to backup the first db. lets say my first db name is db01 and the backup name is db02. is there any way to make autogenerate while the db01 was update and i use db02 to wait db01 finished to update?

Matt Magallo
  • 328
  • 1
  • 20
Bobby Z
  • 765
  • 1
  • 9
  • 21
  • Can you not ask your DBA to generate a new table and do a quick rename on them when it's done? Renaming is practically instant. – Kalkran Feb 18 '16 at 08:24
  • Is that possible? My database is using sql server. And my dba said its not possible. @kalkran – Bobby Z Feb 18 '16 at 08:27
  • I'm sorry, I read "database" but understood "table" as they're unfortunately used interchangeably. If it's just about tables, they're easy to rename, databases I'm not sure. – Kalkran Feb 18 '16 at 08:31
  • Hmm, thats so sad to know "not sure" @kalkran – Bobby Z Feb 18 '16 at 08:36
  • How would you know that `db01` is updating? Can you make a check in your application? Maybe if you're unable to connect to `db01` you can fall back to `db02` ? – Kalkran Feb 18 '16 at 08:38
  • The dba said that he will make 1 table that provide me to know the db01 is done with update and he said will add 1 in the table field. @kalkran – Bobby Z Feb 18 '16 at 08:40
  • There is your answer/solution, connect to `db01` by default, check if the table exists and if it does, disconnect and reconnect to `db02`. – Kalkran Feb 18 '16 at 08:51
  • The logic is good, but how should i do it? Im using codeigniter 3. @kalkran – Bobby Z Feb 18 '16 at 08:53

1 Answers1

0

Try something like the following, don't forget to substitute the proper variables and the table name in the query.

// https://www.codeigniter.com/userguide3/database/connecting.html
$config = [
    'hostname'  => 'localhost',
    'username'  => 'username',
    'password'  => 'password',
    'database'  => 'db01',
    'dbdriver'  => 'mysqli',
]
$conn = $this->load->database($config);
// http://stackoverflow.com/questions/167576/check-if-table-exists-in-sql-server
$query = $this->db->query('SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME="TheTable"');
if ($query->num_fields() > 0) {
    $config['database'] = 'db02';
    $conn = $this->load->database($config);
}

DISCLAIMER: I've never used CodeIgniter.

Kalkran
  • 324
  • 1
  • 8
  • thanks for the solution. from the guide i read, i guess i must create the connection for each model in code i wrote. am i right? – Bobby Z Feb 18 '16 at 17:54