<?php
	error_reporting(0);
	require_once '../core/db.class.php';
	require_once '../../config/db.cfg.php';

	foreach (glob("*.sql") as $filename) {
		echo 'file ' . $filename . '...';
		$core_db->q('SELECT apply_datetime FROM _sys_db_migrations WHERE `filename` = "'.db_escape_string($filename).'" ');
		if (!$core_db->nr()) {
			  $sql = file_get_contents($filename);
	          $sql_a = preg_split("/;(\n|\r\n)/", $sql);
			  foreach($sql_a as $sql ) {
			      if (trim($sql)) {
		            $core_db->q($sql);
			        echo '.';
			      }
			  }
			 echo 'done' . PHP_EOL;
		 	 $core_db->q('INSERT INTO _sys_db_migrations (`filename`,`apply_datetime`) values ("'.db_escape_string($filename).'", "'.date('Y-m-d H:i:s').'" ) ');
		} else {
			 echo 'skip' . PHP_EOL;
		}
	}

?>