<?php
function init_db($server_name, $user, $password) {
    $dbconn = pg_connect(sprintf("host=%s user=%s password=%s", $server_name, $user, $password));
    $res = pg_query("select datname from pg_database");
    
    if (!in_array('scada', pg_fetch_all_columns($res, 0)))
        pg_query("CREATE DATABASE scada");
    pg_close($dbconn);

    $dbconn = pg_connect(sprintf("host=%s dbname=scada user=%s password=%s", $server_name, $user, $password));
    pg_query("CREATE TABLE IF NOT EXISTS hf_mes_scada_data_capture_node_configure (
        id serial2,
        code text primary key NOT NULL,
        name text NOT NULL,
        type text NOT NULL,
        flow_code text,
        working_subclass text NOT NULL,
        workstation text,
        create_date timestamp NOT NULL DEFAULT NOW(),
        note text
    )");
    pg_close($dbconn);
}
