From 8517b9b8c8153193cb219119b2d36293ff07e97e Mon Sep 17 00:00:00 2001 From: DanB Date: Sat, 8 Nov 2014 18:23:48 +0100 Subject: [PATCH] PostgreSQL schema and user creation --- data/storage/postgres/create_cdrs_tables.sql | 84 ++++++ data/storage/postgres/create_db_with_users.sh | 9 + data/storage/postgres/create_rater_tables.sql | 101 ------- .../postgres/create_tariffplan_tables.sql | 272 ++++++++++++++++++ data/storage/postgres/setup_cgr_db.sh | 28 ++ 5 files changed, 393 insertions(+), 101 deletions(-) create mode 100644 data/storage/postgres/create_cdrs_tables.sql create mode 100755 data/storage/postgres/create_db_with_users.sh delete mode 100644 data/storage/postgres/create_rater_tables.sql create mode 100644 data/storage/postgres/create_tariffplan_tables.sql create mode 100755 data/storage/postgres/setup_cgr_db.sh diff --git a/data/storage/postgres/create_cdrs_tables.sql b/data/storage/postgres/create_cdrs_tables.sql new file mode 100644 index 000000000..267926f06 --- /dev/null +++ b/data/storage/postgres/create_cdrs_tables.sql @@ -0,0 +1,84 @@ + +-- +-- Table structure for table `cdrs_primary` +-- + +DROP TABLE IF EXISTS cdrs_primary; +CREATE TABLE cdrs_primary ( + id SERIAL PRIMARY KEY, + cgrid CHAR(40) NOT NULL, + tor VARCHAR(16) NOT NULL, + accid VARCHAR(64) NOT NULL, + cdrhost VARCHAR(64) NOT NULL, + cdrsource VARCHAR(64) NOT NULL, + reqtype VARCHAR(24) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + account VARCHAR(128) NOT NULL, + subject VARCHAR(128) NOT NULL, + destination VARCHAR(128) NOT NULL, + setup_time TIMESTAMP NOT NULL, + answer_time TIMESTAMP NOT NULL, + usage NUMERIC(30,9) NOT NULL, + UNIQUE (cgrid) +); + +-- +-- Table structure for table `cdrs_extra` +-- + +DROP TABLE IF EXISTS cdrs_extra; +CREATE TABLE cdrs_extra ( + id SERIAL PRIMARY KEY, + cgrid CHAR(40) NOT NULL, + extra_fields text NOT NULL, + UNIQUE (cgrid) +); + +-- +-- Table structure for table `cost_details` +-- + +DROP TABLE IF EXISTS cost_details; +CREATE TABLE cost_details ( + id SERIAL PRIMARY KEY, + cost_time TIMESTAMP NOT NULL, + cost_source VARCHAR(64) NOT NULL, + cgrid CHAR(40) NOT NULL, + runid VARCHAR(64) NOT NULL, + tor VARCHAR(16) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(128) NOT NULL, + category VARCHAR(32) NOT NULL, + account VARCHAR(128) NOT NULL, + subject VARCHAR(128) NOT NULL, + destination VARCHAR(128) NOT NULL, + cost NUMERIC(20,4) NOT NULL, + timespans text, + UNIQUE (cgrid, runid) +); + +-- +-- Table structure for table `rated_cdrs` +-- +DROP TABLE IF EXISTS rated_cdrs; +CREATE TABLE rated_cdrs ( + id SERIAL PRIMARY KEY, + mediation_time TIMESTAMP NOT NULL, + cgrid CHAR(40) NOT NULL, + runid VARCHAR(64) NOT NULL, + reqtype VARCHAR(24) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + account VARCHAR(128) NOT NULL, + subject VARCHAR(128) NOT NULL, + destination VARCHAR(128) NOT NULL, + setup_time TIMESTAMP NOT NULL, + answer_time TIMESTAMP NOT NULL, + usage NUMERIC(30,9) NOT NULL, + cost NUMERIC(20,4) DEFAULT NULL, + extra_info text, + UNIQUE (cgrid, runid) +); \ No newline at end of file diff --git a/data/storage/postgres/create_db_with_users.sh b/data/storage/postgres/create_db_with_users.sh new file mode 100755 index 000000000..1f7ff45b1 --- /dev/null +++ b/data/storage/postgres/create_db_with_users.sh @@ -0,0 +1,9 @@ + +# +# Sample db and users creation. Replace here with your own details +# + +sudo -u postgres dropdb -e cgrates +sudo -u postgres dropuser -e cgrates +sudo -u postgres createuser -S -D -R -e cgrates +sudo -u postgres createdb -e -O cgrates cgrates diff --git a/data/storage/postgres/create_rater_tables.sql b/data/storage/postgres/create_rater_tables.sql deleted file mode 100644 index 87e902706..000000000 --- a/data/storage/postgres/create_rater_tables.sql +++ /dev/null @@ -1,101 +0,0 @@ -CREATE TABLE ratingprofile IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - fallbackkey VARCHAR(512), -); -CREATE TABLE ratingdestinations IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE -); -CREATE TABLE destination IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - name VARCHAR(512), - prefixes TEXT -); -CREATE TABLE activationprofile IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - activationtime TIMESTAMP -); -CREATE TABLE interval IF NOT EXISTS( - id SERIAL PRIMARY KEY, - activationprofile INTEGER REFERENCES activationprofile(id) ON DELETE CASCADE, - years TEXT, - months TEXT, - monthdays TEXT, - weekdays TEXT, - starttime TIMESTAMP, - endtime TIMESTAMP, - weight FLOAT8, - connectfee FLOAT8, - price FLOAT8, - pricedunits FLOAT8, - rateincrements FLOAT8 -); -CREATE TABLE minutebucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - seconds FLOAT8, - weight FLOAT8, - price FLOAT8, - percent FLOAT8 -); -CREATE TABLE unitcounter IF NOT EXISTS( - id SERIAL PRIMARY KEY, - direction TEXT, - balance TEXT, - units FLOAT8 -); -CREATE TABLE unitcounterbucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE -); -CREATE TABLE actiontrigger IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - balance TEXT, - direction TEXT, - thresholdvalue FLOAT8, - weight FLOAT8, - executed BOOL -); -CREATE TABLE balance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - name TEXT; - value FLOAT8 -); -CREATE TABLE userbalance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE - actiontriggers INTEGER REFERENCES actiontrigger(id) ON DELETE CASCADE, - balances INTEGER REFERENCES balance(id) ON DELETE CASCADE, - type TEXT -); -CREATE TABLE actiontiming IF NOT EXISTS( - id SERIAL PRIMARY KEY, - tag TEXT, - userbalances INTEGER REFERENCES userbalance(id) ON DELETE CASCADE, - timing INTEGER REFERENCES interval(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - weight FLOAT8 -); -CREATE TABLE action IF NOT EXISTS( - id SERIAL PRIMARY KEY, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE, - actiontype TEXT, - balance TEXT, - direction TEXT, - units FLOAT8, - weight FLOAT8 -); - -CREATE TABLE sharedgroup IF NOT EXISTS( - id SERIAL PRIMARY KEY, - account TEXT, - strategy TEXT, - ratesubject TEXT, -); diff --git a/data/storage/postgres/create_tariffplan_tables.sql b/data/storage/postgres/create_tariffplan_tables.sql new file mode 100644 index 000000000..f73fb4fa6 --- /dev/null +++ b/data/storage/postgres/create_tariffplan_tables.sql @@ -0,0 +1,272 @@ +-- +-- Table structure for table `tp_timings` +-- +DROP TABLE IF EXISTS tp_timings; +CREATE TABLE tp_timings ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + years VARCHAR(255) NOT NULL, + months VARCHAR(255) NOT NULL, + month_days VARCHAR(255) NOT NULL, + week_days VARCHAR(255) NOT NULL, + time VARCHAR(16) NOT NULL, + UNIQUE (tpid, tag) +); + +-- +-- Table structure for table `tp_destinations` +-- + +DROP TABLE IF EXISTS tp_destinations; +CREATE TABLE tp_destinations ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + prefix VARCHAR(24) NOT NULL, + UNIQUE (tpid, tag, prefix) +); + +-- +-- Table structure for table `tp_rates` +-- + +DROP TABLE IF EXISTS tp_rates; +CREATE TABLE tp_rates ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + connect_fee NUMERIC(7,4) NOT NULL, + rate NUMERIC(7,4) NOT NULL, + rate_unit VARCHAR(16) NOT NULL, + rate_increment VARCHAR(16) NOT NULL, + group_interval_start VARCHAR(16) NOT NULL, + UNIQUE (tpid, tag, group_interval_start), +); + +-- +-- Table structure for table `destination_rates` +-- + +DROP TABLE IF EXISTS tp_destination_rates; +CREATE TABLE tp_destination_rates ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + destinations_tag VARCHAR(64) NOT NULL, + rates_tag VARCHAR(64) NOT NULL, + rounding_method VARCHAR(255) NOT NULL, + rounding_decimals SMALLINT(4) NOT NULL, + UNIQUE (tpid, tag , destinations_tag) +); + +-- +-- Table structure for table `tp_rating_plans` +-- + +DROP TABLE IF EXISTS tp_rating_plans; +CREATE TABLE tp_rating_plans ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + destrates_tag VARCHAR(64) NOT NULL, + timing_tag VARCHAR(64) NOT NULL, + weight NUMERIC(8,2) NOT NULL, + UNIQUE (tpid, tag, destrates_tag, timing_tag) +); + +-- +-- Table structure for table `tp_rate_profiles` +-- + +DROP TABLE IF EXISTS tp_rating_profiles; +CREATE TABLE tp_rating_profiles ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + loadid VARCHAR(64) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + subject VARCHAR(64) NOT NULL, + activation_time VARCHAR(24) NOT NULL, + rating_plan_tag VARCHAR(64) NOT NULL, + fallback_subjects VARCHAR(64), + UNIQUE (tpid, loadid, tenant, category, direction, subject, activation_time) +); + +-- +-- Table structure for table `tp_shared_groups` +-- + +DROP TABLE IF EXISTS tp_shared_groups; +CREATE TABLE tp_shared_groups ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + account VARCHAR(24) NOT NULL, + strategy VARCHAR(24) NOT NULL, + rating_subject VARCHAR(24) NOT NULL, + UNIQUE (tpid, tag, account , strategy , rating_subject) +); + +-- +-- Table structure for table `tp_actions` +-- + +DROP TABLE IF EXISTS tp_actions; +CREATE TABLE tp_actions ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + action VARCHAR(24) NOT NULL, + balance_type VARCHAR(24) NOT NULL, + direction VARCHAR(8) NOT NULL, + units NUMERIC(20,4) NOT NULL, + expiry_time VARCHAR(24) NOT NULL, + destination_tag VARCHAR(64) NOT NULL, + rating_subject VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + shared_group VARCHAR(64) NOT NULL, + balance_weight NUMERIC(8,2) NOT NULL, + extra_parameters VARCHAR(256) NOT NULL, + weight NUMERIC(8,2) NOT NULL, + UNIQUE (tpid, tag, action, balance_type, direction, expiry_time, destination_tag, shared_group, balance_weight, weight) +); + +-- +-- Table structure for table `tp_action_timings` +-- + +DROP TABLE IF EXISTS tp_action_plans; +CREATE TABLE tp_action_plans ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + actions_tag VARCHAR(64) NOT NULL, + timing_tag VARCHAR(64) NOT NULL, + weight NUMERIC(8,2) NOT NULL, + UNIQUE (tpid, tag, actions_tag) +); + +-- +-- Table structure for table tp_action_triggers +-- + +DROP TABLE IF EXISTS tp_action_triggers; +CREATE TABLE tp_action_triggers ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + balance_type VARCHAR(24) NOT NULL, + direction VARCHAR(8) NOT NULL, + threshold_type char(12) NOT NULL, + threshold_value NUMERIC(20,4) NOT NULL, + recurrent bool NOT NULL, + min_sleep INTEGER NOT NULL, + destination_tag VARCHAR(64) NOT NULL, + balance_weight NUMERIC(8,2) NOT NULL, + balance_expiry_time VARCHAR(24) NOT NULL, + balance_rating_subject VARCHAR(64) NOT NULL, + balance_category VARCHAR(16) NOT NULL, + balance_shared_group VARCHAR(64) NOT NULL, + min_queued_items INTEGER NOT NULL, + actions_tag VARCHAR(64) NOT NULL, + weight NUMERIC(8,2) NOT NULL, + UNIQUE (tpid, tag, balance_type, direction, threshold_type, threshold_value, destination_tag, actions_tag) +); + +-- +-- Table structure for table tp_account_actions +-- + +DROP TABLE IF EXISTS tp_account_actions; +CREATE TABLE tp_account_actions ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + loadid VARCHAR(64) NOT NULL, + tenant VARCHAR(64) NOT NULL, + account VARCHAR(64) NOT NULL, + direction VARCHAR(8) NOT NULL, + action_plan_tag VARCHAR(64), + action_triggers_tag VARCHAR(64), + UNIQUE (tpid, loadid, tenant, account, direction) +); + +-- +-- Table structure for table `tp_lcr_rules` +-- + +DROP TABLE IF EXISTS tp_lcr_rules; +CREATE TABLE tp_lcr_rules ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + customer VARCHAR(64) NOT NULL, + destination_tag VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + strategy VARCHAR(16) NOT NULL, + suppliers VARCHAR(64) NOT NULL, + activation_time VARCHAR(24) NOT NULL, + weight NUMERIC(8,2) NOT NULL +); + +-- +-- Table structure for table `tp_derived_chargers` +-- + +DROP TABLE IF EXISTS tp_derived_chargers; +CREATE TABLE tp_derived_chargers ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + loadid VARCHAR(64) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + account VARCHAR(24) NOT NULL, + subject VARCHAR(64) NOT NULL, + runid VARCHAR(24) NOT NULL, + run_filters VARCHAR(256) NOT NULL, + req_type_field VARCHAR(24) NOT NULL, + direction_field VARCHAR(24) NOT NULL, + tenant_field VARCHAR(24) NOT NULL, + category_field VARCHAR(24) NOT NULL, + account_field VARCHAR(24) NOT NULL, + subject_field VARCHAR(24) NOT NULL, + destination_field VARCHAR(24) NOT NULL, + setup_time_field VARCHAR(24) NOT NULL, + answer_time_field VARCHAR(24) NOT NULL, + usage_field VARCHAR(24) NOT NULL +); + + +-- +-- Table structure for table `tp_cdr_stats` +-- + +DROP TABLE IF EXISTS tp_cdr_stats; +CREATE TABLE tp_cdr_stats ( + id SERIAL PRIMARY KEY, + tpid VARCHAR(64) NOT NULL, + tag VARCHAR(64) NOT NULL, + queue_length INTEGER NOT NULL, + time_window VARCHAR(8) NOT NULL, + metrics VARCHAR(64) NOT NULL, + setup_interval VARCHAR(64) NOT NULL, + tor VARCHAR(64) NOT NULL, + cdr_host VARCHAR(64) NOT NULL, + cdr_source VARCHAR(64) NOT NULL, + req_type VARCHAR(64) NOT NULL, + direction VARCHAR(8) NOT NULL, + tenant VARCHAR(64) NOT NULL, + category VARCHAR(16) NOT NULL, + account VARCHAR(24) NOT NULL, + subject VARCHAR(64) NOT NULL, + destination_prefix VARCHAR(64) NOT NULL, + usage_interval VARCHAR(64) NOT NULL, + mediation_runids VARCHAR(64) NOT NULL, + rated_account VARCHAR(64) NOT NULL, + rated_subject VARCHAR(64) NOT NULL, + cost_interval VARCHAR(24) NOT NULL, + action_triggers VARCHAR(64) NOT NULL +); diff --git a/data/storage/postgres/setup_cgr_db.sh b/data/storage/postgres/setup_cgr_db.sh new file mode 100755 index 000000000..c3ccce9ff --- /dev/null +++ b/data/storage/postgres/setup_cgr_db.sh @@ -0,0 +1,28 @@ +#! /usr/bin/env sh + + +user=$1 +if [ -z "$1" ]; then + user="postgres" +fi + +host=$2 +if [ -z "$2" ]; then + host="localhost" +fi + +./create_db_with_users.sh + +sudo -u $user psql -d cgrates -f create_cdrs_tables.sql +cdrt=$? +sudo -u $user psql -d cgrates -f create_tariffplan_tables.sql +tpt=$? + +if [ $cdrt = 0 ] && [ $tpt = 0 ]; then + echo "" + echo "\t+++ CGR-DB successfully set-up! +++" + echo "" + exit 0 +fi + +