CREATE TABLE "ipypbxweb_connection" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(100) NOT NULL,
    "local_ip_address" char(15) NOT NULL,
    "local_port" integer unsigned NOT NULL,
    "freeswitch_ip_address" char(15) NOT NULL,
    "freeswitch_port" integer unsigned NOT NULL
)
;
CREATE TABLE "ipypbxweb_sipprofile" (
    "id" integer NOT NULL PRIMARY KEY,
    "connection_id" integer NOT NULL REFERENCES "ipypbxweb_connection" ("id"),
    "name" varchar(100) NOT NULL,
    "external_rtp_ip" varchar(100) NOT NULL,
    "external_sip_ip" varchar(100) NOT NULL,
    "rtp_ip" varchar(100) NOT NULL,
    "sip_ip" varchar(100) NOT NULL,
    "sip_port" integer unsigned NOT NULL,
    "accept_blind_registration" bool NOT NULL,
    "authenticate_calls" bool NOT NULL,
    "is_active" bool NOT NULL
)
;
CREATE TABLE "ipypbxweb_domain" (
    "id" integer NOT NULL PRIMARY KEY,
    "sip_profile_id" integer NOT NULL REFERENCES "ipypbxweb_sipprofile" ("id"),
    "host_name" varchar(100) NOT NULL,
    "is_active" bool NOT NULL
)
;
CREATE TABLE "ipypbxweb_gateway" (
    "id" integer NOT NULL PRIMARY KEY,
    "sip_profile_id" integer NOT NULL REFERENCES "ipypbxweb_sipprofile" ("id"),
    "name" varchar(100) NOT NULL,
    "username" varchar(100) NOT NULL,
    "password" varchar(100) NOT NULL,
    "realm" varchar(100) NOT NULL,
    "from_domain" varchar(100) NOT NULL,
    "expire_in_seconds" integer unsigned NOT NULL,
    "retry_in_seconds" integer unsigned NOT NULL,
    "caller_id_in_from_field" bool NOT NULL,
    "is_active" bool NOT NULL
)
;
CREATE TABLE "ipypbxweb_endpoint" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" varchar(100) NOT NULL,
    "password" varchar(100) NOT NULL,
    "domain_id" integer NOT NULL REFERENCES "ipypbxweb_domain" ("id"),
    "is_active" bool NOT NULL
)
;
CREATE TABLE "ipypbxweb_extension" (
    "id" integer NOT NULL PRIMARY KEY,
    "destination_match" varchar(100) NOT NULL,
    "xml_dialplan" text NOT NULL,
    "domain_id" integer NOT NULL REFERENCES "ipypbxweb_domain" ("id"),
    "endpoint_id" integer NOT NULL REFERENCES "ipypbxweb_endpoint" ("id"),
    "authenticate_calls" bool NOT NULL,
    "is_active" bool NOT NULL
)
;
CREATE INDEX "ipypbxweb_sipprofile_connection_id" ON "ipypbxweb_sipprofile" ("connection_id");
CREATE INDEX "ipypbxweb_domain_sip_profile_id" ON "ipypbxweb_domain" ("sip_profile_id");
CREATE INDEX "ipypbxweb_gateway_sip_profile_id" ON "ipypbxweb_gateway" ("sip_profile_id");
CREATE INDEX "ipypbxweb_endpoint_domain_id" ON "ipypbxweb_endpoint" ("domain_id");
CREATE INDEX "ipypbxweb_extension_domain_id" ON "ipypbxweb_extension" ("domain_id");
CREATE INDEX "ipypbxweb_extension_endpoint_id" ON "ipypbxweb_extension" ("endpoint_id");