File "table_migration.php"

Full Path: /home/analogde/www/Massage_debug_15_11_2017/table_migration.php
File size: 3.38 KB
MIME-type: text/x-php
Charset: utf-8

<?php
		
		error_reporting(0);
		
		include('database.php');
		include('fonctions.php');
		DB_connexion(); 

		//$sql = "DROP TABLE reservation2017";
		//mysqli_query($connexion, "SET NAMES 'utf8'");
		//$result = mysqli_query($connexion, $sql );

		// This will create an exact copy of the table.
		//CREATE TABLE Table2 LIKE Table1;
	/*	
		$sql = "CREATE TABLE copyTable LIKE reservation2017";
		$result = mysqli_query($connexion, $sql );

		//Fill in the Duplicate table with values from original table.
		//INSERT INTO Table2 SELECT * from Table1;
		$sql = "INSERT INTO copyTable SELECT * from reservation2017";
		$result = mysqli_query($connexion, $sql );
		
		$sql = "ALTER TABLE copyTable ADD formule VARCHAR(30) AFTER etendu";
		$result = mysqli_query($connexion, $sql );
		
		$sql = "UPDATE copyTable SET formule = 'courte'";
		$result = mysqli_query($connexion, $sql );
	*/	
	
		$table_array = array(array());
	  
		$table_array[0][0] = "reservation2015";
		$table_array[0][1] = "reservation2015_v3";
	  
		$table_array[1][0] = "reservation2016";
		$table_array[1][1] = "reservation2016_v3";
	  
		$table_array[2][0] = "reservation2017";
		$table_array[2][1] = "reservation2017_v3";
	  
		$table_array[3][0] = "gestion";
		$table_array[3][1] = "gestion_v3";
		
		$table_array[4][0] = "configuration";
		$table_array[4][1] = "configuration_v3";
		
		//echo "<pre>";
		//print_r($table_array);
	  
		$table_in_db = array();
		$sql = "SHOW TABLES FROM test";
		$result = mysql_query($sql );
		//$result = mysqli_query($connexion, $sql );
		while ($row = mysql_fetch_row($result))
		{
			$table_in_db[] = $row[0]; 
		}
		
		echo "<pre>";
		print_r($table_in_db);

		for($i = 0; $i< sizeof($table_array) ; $i++)
		{
			generation_table($i, $table_array, $table_in_db);
		}
		
		
		
			
function generation_table($i, $table_array, $table_in_db)
{		

	 // existe 
	 if(in_array($table_array[$i][1], $table_in_db))
	 {
		echo "********************************";
		echo "<br>";
		echo $table_array[$i][1] ." exists in DB";
		echo "<br>";
			 
		$sql = "DROP TABLE " .$table_array[$i][1];
		echo $sql;
		echo "<br>";
		
		//$result = mysqli_query($connexion, $sql );
		// This will create an exact copy of the table.
		//CREATE TABLE Table2 LIKE Table1;
		$sql = "CREATE TABLE " .$table_array[$i][1] ." LIKE " .$table_array[$i][0];
		echo $sql;
		echo "<br>";
		//$result = mysqli_query($connexion, $sql );

		//Fill in the Duplicate table with values from original table.
		//INSERT INTO Table2 SELECT * from Table1;
		$sql = "INSERT INTO " .$table_array[$i][1] ." SELECT * from " .$table_array[$i][0];
		echo $sql;
		echo "<br>";
		//$result = mysqli_query($connexion, $sql );
		
		$pos =  strstr($table_array[$i][1], "reservation");
		//echo "***** :" .$pos;
		
		// la table gestion et configuration  ne sont pas concernées par l'ajout de la colonne formule
		// donc on autorise le traitement seulement si on trouve la sous chaine "reservation"
		if($pos !== false)
		{
			$sql = "ALTER TABLE " .$table_array[$i][1] ." ADD formule VARCHAR(30) AFTER etendu";
			echo $sql;
			echo "<br>";
			
			//$result = mysqli_query($connexion, $sql );
		
			$sql = "UPDATE " .$table_array[$i][1] ." SET formule = 'courte'";
			echo $sql;
			echo "<br>";
			//$result = mysqli_query($connexion, $sql );
		
		}
		
		echo "********************************";
		echo "<br>";
		echo "<br>";
	 }
	 else
	 {
		echo $table ." dont' exists in DB";
		echo "<br>";
	 }	

	
		
}
	
?>