CREATE TABLE `users` (
	`id` serial AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`password` varchar(128) NOT NULL,
	`email` varchar(255) NOT NULL,
	`user_profile` enum('USER','CUSTOMER','ADMIN','FINANCIAL','FINANCIAL_N2','SUPPORT','SUPPORT_N2') NOT NULL DEFAULT 'USER',
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `users_id` PRIMARY KEY(`id`),
	CONSTRAINT `users_email_unique` UNIQUE(`email`)
);
--> statement-breakpoint
CREATE TABLE `entity_logs` (
	`id` serial AUTO_INCREMENT NOT NULL,
	`user_id` bigint unsigned,
	`ip` varchar(45),
	`device` varchar(255),
	`description` varchar(500) NOT NULL,
	`entity_table` varchar(100) NOT NULL,
	`entity_id` int NOT NULL,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`updated_at` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `entity_logs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `dial-plan-group` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`domain` varchar(255) NOT NULL,
	`dial_plan_group` int NOT NULL,
	`rule_id` int NOT NULL,
	`dial_id` int NOT NULL,
	`priority` int NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `dial-plan-group_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `dial-plan` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`dialplangroup_id` int NOT NULL,
	`opensip_dialplan_id` int NOT NULL,
	`opensip_dpid` int NOT NULL,
	`opensip_pr` int NOT NULL,
	`opensip_matchOp` int NOT NULL,
	`opensip_matchExp` varchar(255) NOT NULL,
	`opensip_matchFlags` int NOT NULL,
	`opensip_substExp` varchar(255),
	`opensip_replExp` varchar(255),
	`opensip_timeRec` varchar(255),
	`opensip_disable` int NOT NULL DEFAULT 0,
	`opensip_attrs` varchar(255),
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `dial-plan_id` PRIMARY KEY(`id`),
	CONSTRAINT `dial-plan_opensip_dialplan_id_unique` UNIQUE(`opensip_dialplan_id`)
);
--> statement-breakpoint
CREATE TABLE `rule_groups` (
	`id` int AUTO_INCREMENT NOT NULL,
	`title` varchar(255) NOT NULL,
	`gwlist` varchar(255) NOT NULL,
	`reference` varchar(255) NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `rule_groups_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `rules` (
	`id` int AUTO_INCREMENT NOT NULL,
	`rulegroupd_id` int NOT NULL,
	`opensip_ruleid` int,
	`opensip_groupid` varchar(255) NOT NULL,
	`opensip_prefix` varchar(255) NOT NULL,
	`opensip_timerec` varchar(255),
	`opensip_priority` int NOT NULL DEFAULT 0,
	`opensip_routeid` varchar(255),
	`opensip_gwlist` varchar(255),
	`opensip_sort_alg` varchar(255) NOT NULL DEFAULT 'N',
	`opensip_sort_profile` int,
	`opensip_attrs` varchar(255),
	`opensip_description` varchar(255),
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `rules_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `profiles` (
	`id` int AUTO_INCREMENT NOT NULL,
	`title` varchar(128) NOT NULL,
	`acls` varchar(255) NOT NULL DEFAULT '',
	`dial_plan_group_id` int NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `profiles_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `domain` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensip_domain_id` int NOT NULL,
	`domain` varchar(255) NOT NULL,
	`attrs` varchar(255),
	`default_domain` boolean NOT NULL DEFAULT false,
	`active` boolean NOT NULL DEFAULT false,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `domain_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `tariffs` (
	`id` int AUTO_INCREMENT NOT NULL,
	`title` varchar(255) NOT NULL,
	`domain_id` int NOT NULL,
	`rule_id` int NOT NULL,
	`tariff_per_call` boolean NOT NULL DEFAULT false,
	`markup` varchar(255) NOT NULL DEFAULT '0.00',
	`plan_type` varchar(255) NOT NULL DEFAULT 'postpaid',
	`closing_type` varchar(255) NOT NULL DEFAULT 'transaction',
	`rounding_type` varchar(255) NOT NULL DEFAULT 'truncate',
	`purchase_connection_fee` varchar(255) NOT NULL DEFAULT '',
	`purchase_cadence` int NOT NULL DEFAULT 0,
	`initial_delay` int NOT NULL DEFAULT 0,
	`disposal_fee` varchar(255) NOT NULL DEFAULT '',
	`connection_value` varchar(255) NOT NULL DEFAULT '',
	`quota_limit` varchar(255) NOT NULL DEFAULT '',
	`call_0800` varchar(12),
	`call_0300` varchar(12),
	`call_0500` varchar(12),
	`call_special_number` varchar(12),
	`call_emergency_numbers` varchar(12),
	`call_local_numbers` varchar(12),
	`call_ldn_numbers` varchar(12),
	`call_local_mobile_numbers` varchar(12),
	`call_mobile_numbers_ldn_vc2` varchar(12),
	`call_mobile_numbers_ldn_vc3` varchar(12),
	`call_local_numbers_reduced_value` varchar(12),
	`call_international_numbers` varchar(12),
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `tariffs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `clients` (
	`id` int AUTO_INCREMENT NOT NULL,
	`company_name` varchar(255) NOT NULL,
	`fantasy_name` varchar(255) NOT NULL,
	`email` varchar(64) NOT NULL,
	`identifier` varchar(255) NOT NULL DEFAULT '',
	`profile_id` int NOT NULL,
	`tariff_id` int NOT NULL,
	`record_calls` boolean NOT NULL DEFAULT false,
	`contract_number` varchar(255) NOT NULL,
	`cpf_cnpj` varchar(255) NOT NULL,
	`address_number` varchar(255) NOT NULL,
	`address_neighborhood` varchar(255) NOT NULL,
	`address_street` varchar(255) NOT NULL,
	`address_state` varchar(255) NOT NULL,
	`address_city` varchar(255) NOT NULL,
	`cep` varchar(255) NOT NULL,
	`telphone` varchar(255) NOT NULL,
	`notification_low_balance` boolean NOT NULL DEFAULT false,
	`limite_type` varchar(255) NOT NULL DEFAULT 'monetary',
	`minute_limit_day` varchar(255) NOT NULL DEFAULT '',
	`minute_limit_month` varchar(255) NOT NULL DEFAULT '',
	`cnl` varchar(255) NOT NULL,
	`carried` boolean NOT NULL DEFAULT false,
	`area_code` int NOT NULL,
	`country_code` int NOT NULL,
	`call_limit_receptive` int NOT NULL DEFAULT 5,
	`call_limit_carried_out` int NOT NULL DEFAULT 5,
	`status` varchar(255) NOT NULL DEFAULT 'ACTIVE',
	`bina_mode` enum('DISABLED','ENABLED','ENABLED_AREA') NOT NULL DEFAULT 'DISABLED',
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `clients_id` PRIMARY KEY(`id`),
	CONSTRAINT `clients_cpf_cnpj_unique` UNIQUE(`cpf_cnpj`)
);
--> statement-breakpoint
CREATE TABLE `notifications_sent` (
	`id` int AUTO_INCREMENT NOT NULL,
	`client_id` int NOT NULL,
	`start_date` timestamp NOT NULL,
	`end_date` timestamp NOT NULL,
	`threshold` int NOT NULL,
	`total_calls` int NOT NULL,
	`active_calls` int NOT NULL,
	`notified_at` timestamp NOT NULL DEFAULT (now()),
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `notifications_sent_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `user_clients` (
	`id` int AUTO_INCREMENT NOT NULL,
	`user_id` bigint unsigned NOT NULL,
	`client_id` int NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `user_clients_id` PRIMARY KEY(`id`),
	CONSTRAINT `user_clients_user_id_client_id_unique` UNIQUE(`user_id`,`client_id`)
);
--> statement-breakpoint
CREATE TABLE `carrier` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensip_carrier_id` int,
	`opensip_carrierid` varchar(255) NOT NULL,
	`opensip_gwlist` varchar(255) NOT NULL,
	`opensip_flags` int NOT NULL DEFAULT 0,
	`opensip_sort_alg` char(1) NOT NULL DEFAULT 'N',
	`opensip_state` int NOT NULL DEFAULT 0,
	`opensip_attrs` varchar(255),
	`opensip_description` varchar(128),
	`charging_on_connection` boolean NOT NULL DEFAULT false,
	`rounding` varchar(32) NOT NULL DEFAULT '',
	`purchase_connection_fee` varchar(255) NOT NULL DEFAULT '',
	`purchase_cadence` int NOT NULL DEFAULT 0,
	`initial_delay` int NOT NULL DEFAULT 0,
	`disposal_fee` varchar(255) NOT NULL DEFAULT '',
	`connection_value` varchar(255) NOT NULL DEFAULT '',
	`quota_limit` varchar(255) NOT NULL DEFAULT '',
	`call_0800` varchar(12) NOT NULL DEFAULT '',
	`call_0300` varchar(12) NOT NULL DEFAULT '',
	`call_0500` varchar(12) NOT NULL DEFAULT '',
	`call_special_number` varchar(12) NOT NULL DEFAULT '',
	`call_emergency_numbers` varchar(12) NOT NULL DEFAULT '',
	`call_local_numbers` varchar(12) NOT NULL DEFAULT '',
	`call_ldn_numbers` varchar(12) NOT NULL DEFAULT '',
	`call_local_mobile_numbers` varchar(12) NOT NULL DEFAULT '',
	`call_mobile_numbers_ldn_vc2` varchar(12) NOT NULL DEFAULT '',
	`call_mobile_numbers_ldn_vc3` varchar(12) NOT NULL DEFAULT '',
	`call_local_numbers_reduced_value` varchar(12) NOT NULL DEFAULT '',
	`call_international_numbers` varchar(12) NOT NULL DEFAULT '',
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `carrier_id` PRIMARY KEY(`id`),
	CONSTRAINT `carrier_opensip_carrierid_unique` UNIQUE(`opensip_carrierid`)
);
--> statement-breakpoint
CREATE TABLE `gateways` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensip_gateway_id` int NOT NULL,
	`gwid` varchar(255) NOT NULL,
	`type` int NOT NULL DEFAULT 0,
	`address` varchar(255) NOT NULL,
	`strip` int NOT NULL DEFAULT 0,
	`pri_prefix` varchar(255),
	`attrs` varchar(255),
	`probe_mode` int NOT NULL DEFAULT 0,
	`state` int NOT NULL DEFAULT 0,
	`socket` varchar(255),
	`description` varchar(255),
	`carrier` int NOT NULL,
	`cli_support` boolean NOT NULL DEFAULT false,
	`type_cli` varchar(255) NOT NULL DEFAULT 'None',
	`cli_format` varchar(255) NOT NULL DEFAULT 'None',
	`caller_id` varchar(255),
	`dial_plan_group` int NOT NULL DEFAULT 0,
	`max_calls_outbound` int NOT NULL DEFAULT 3,
	`max_calls_inbound` int NOT NULL DEFAULT 3,
	`timeout` int NOT NULL DEFAULT 0,
	`max_attempts_per_second` int NOT NULL DEFAULT 0,
	`rate_id` varchar(255),
	`transshipment_code` varchar(255),
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `gateways_id` PRIMARY KEY(`id`),
	CONSTRAINT `gateways_gwid_unique` UNIQUE(`gwid`)
);
--> statement-breakpoint
CREATE TABLE `proxy` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensips_proxy_id` int NOT NULL,
	`name` varchar(255) NOT NULL,
	`protocol` varchar(255) NOT NULL,
	`ip` varchar(255) NOT NULL,
	`port` varchar(255) NOT NULL,
	`type` varchar(255) NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `proxy_id` PRIMARY KEY(`id`),
	CONSTRAINT `proxy_opensips_proxy_id_unique` UNIQUE(`opensips_proxy_id`)
);
--> statement-breakpoint
CREATE TABLE `dids` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensip_dbaliases_id` int,
	`opensip_alias_username` varchar(255) NOT NULL DEFAULT '',
	`opensip_alias_domain` varchar(255) NOT NULL DEFAULT '',
	`opensip_username` varchar(255) NOT NULL DEFAULT '',
	`opensip_domain` varchar(255) NOT NULL DEFAULT '',
	`opensip_domain_id` int,
	`subscription_value` double NOT NULL DEFAULT 0,
	`client_id` int,
	`proxy_id` int,
	`domain_id` int NOT NULL,
	`cnl` varchar(255) NOT NULL DEFAULT '',
	`code_area` varchar(255) NOT NULL DEFAULT '',
	`status` varchar(255) NOT NULL DEFAULT 'ACTIVE',
	`request_uri` boolean NOT NULL DEFAULT false,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `dids_id` PRIMARY KEY(`id`),
	CONSTRAINT `dids_opensip_dbaliases_id_unique` UNIQUE(`opensip_dbaliases_id`)
);
--> statement-breakpoint
CREATE TABLE `logins` (
	`id` int AUTO_INCREMENT NOT NULL,
	`monitoration` boolean NOT NULL DEFAULT false,
	`caller_id` varchar(255) NOT NULL DEFAULT '',
	`username` varchar(255) NOT NULL,
	`domain` varchar(255) NOT NULL,
	`password` varchar(255) NOT NULL,
	`client_id` int NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `logins_id` PRIMARY KEY(`id`),
	CONSTRAINT `logins_username_unique` UNIQUE(`username`)
);
--> statement-breakpoint
CREATE TABLE `address` (
	`id` int AUTO_INCREMENT NOT NULL,
	`opensips_address_id` int NOT NULL,
	`client_id` int NOT NULL,
	`domain_id` int NOT NULL,
	`title` varchar(255) NOT NULL,
	`grp` int NOT NULL DEFAULT 0,
	`ip` varchar(50) NOT NULL,
	`mask` tinyint NOT NULL,
	`port` int NOT NULL DEFAULT 0,
	`proto` varchar(4) NOT NULL DEFAULT 'any',
	`pattern` varchar(64),
	`context_info` varchar(32),
	`arquived` boolean NOT NULL DEFAULT false,
	CONSTRAINT `address_id` PRIMARY KEY(`id`),
	CONSTRAINT `address_opensips_address_id_unique` UNIQUE(`opensips_address_id`)
);
--> statement-breakpoint
CREATE TABLE `did_authentications` (
	`id` int AUTO_INCREMENT NOT NULL,
	`type` varchar(255) NOT NULL DEFAULT 'login',
	`did_id` int NOT NULL,
	`login_id` int,
	`address_by_ip_id` int,
	CONSTRAINT `did_authentications_id` PRIMARY KEY(`id`),
	CONSTRAINT `did_authentications_did_id_unique` UNIQUE(`did_id`)
);
--> statement-breakpoint
CREATE TABLE `cdr` (
	`id` int AUTO_INCREMENT NOT NULL,
	`status` varchar(255) NOT NULL,
	`direction` varchar(255) NOT NULL,
	`code` varchar(255) NOT NULL,
	`client_id` int NOT NULL,
	`did_id` int,
	`callId` varchar(255) NOT NULL,
	`did_str` varchar(255),
	`from` varchar(255) NOT NULL,
	`to` varchar(255) NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `cdr_id` PRIMARY KEY(`id`),
	CONSTRAINT `cdr_callId_unique` UNIQUE(`callId`)
);
--> statement-breakpoint
CREATE TABLE `cdr-metrics` (
	`id` int AUTO_INCREMENT NOT NULL,
	`cdr_id` int NOT NULL,
	`client_id` int NOT NULL,
	`did_id` int NOT NULL,
	`callId` varchar(255) NOT NULL,
	`event` varchar(255) NOT NULL,
	`direction` varchar(255) NOT NULL,
	`code` varchar(255) NOT NULL,
	`did_str` varchar(255) NOT NULL,
	`from` varchar(255) NOT NULL,
	`to` varchar(255) NOT NULL,
	`arquived` boolean NOT NULL DEFAULT false,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`last_modified` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `cdr-metrics_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `AuditLogOpensips` (
	`id` int AUTO_INCREMENT NOT NULL,
	`action` varchar(255) NOT NULL,
	`table_name` varchar(255) NOT NULL,
	`record_id` int NOT NULL,
	`change_fields` text,
	`user_id` int NOT NULL,
	`ip_address` varchar(255),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `AuditLogOpensips_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `AuditLogVSR` (
	`id` int AUTO_INCREMENT NOT NULL,
	`action` varchar(255) NOT NULL,
	`table_name` varchar(255) NOT NULL,
	`record_id` int NOT NULL,
	`change_fields` text,
	`user_id` int NOT NULL,
	`ip_address` varchar(255),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `AuditLogVSR_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `entity_logs` ADD CONSTRAINT `entity_logs_user_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `dial-plan-group` ADD CONSTRAINT `dial-plan-group_rule_id_rule_groups_id_fk` FOREIGN KEY (`rule_id`) REFERENCES `rule_groups`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `dial-plan` ADD CONSTRAINT `dial-plan_dialplangroup_id_dial-plan-group_id_fk` FOREIGN KEY (`dialplangroup_id`) REFERENCES `dial-plan-group`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `rules` ADD CONSTRAINT `rules_rulegroupd_id_rule_groups_id_fk` FOREIGN KEY (`rulegroupd_id`) REFERENCES `rule_groups`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `profiles` ADD CONSTRAINT `profiles_dial_plan_group_id_dial-plan-group_id_fk` FOREIGN KEY (`dial_plan_group_id`) REFERENCES `dial-plan-group`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `tariffs` ADD CONSTRAINT `tariffs_domain_id_domain_id_fk` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `tariffs` ADD CONSTRAINT `tariffs_rule_id_rule_groups_id_fk` FOREIGN KEY (`rule_id`) REFERENCES `rule_groups`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `clients` ADD CONSTRAINT `clients_profile_id_profiles_id_fk` FOREIGN KEY (`profile_id`) REFERENCES `profiles`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `clients` ADD CONSTRAINT `clients_tariff_id_tariffs_id_fk` FOREIGN KEY (`tariff_id`) REFERENCES `tariffs`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `notifications_sent` ADD CONSTRAINT `notifications_sent_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `user_clients` ADD CONSTRAINT `user_clients_user_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `user_clients` ADD CONSTRAINT `user_clients_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `gateways` ADD CONSTRAINT `gateways_carrier_carrier_id_fk` FOREIGN KEY (`carrier`) REFERENCES `carrier`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `dids` ADD CONSTRAINT `dids_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `dids` ADD CONSTRAINT `dids_proxy_id_proxy_id_fk` FOREIGN KEY (`proxy_id`) REFERENCES `proxy`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `dids` ADD CONSTRAINT `dids_domain_id_domain_id_fk` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `logins` ADD CONSTRAINT `logins_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `address` ADD CONSTRAINT `address_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `address` ADD CONSTRAINT `address_domain_id_domain_id_fk` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_did_id_dids_id_fk` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_login_id_logins_id_fk` FOREIGN KEY (`login_id`) REFERENCES `logins`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_address_by_ip_id_address_id_fk` FOREIGN KEY (`address_by_ip_id`) REFERENCES `address`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_did_id_dids_id_fk` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_cdr_id_cdr_id_fk` FOREIGN KEY (`cdr_id`) REFERENCES `cdr`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_client_id_clients_id_fk` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_did_id_dids_id_fk` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `entity_logs_user_id_idx` ON `entity_logs` (`user_id`);--> statement-breakpoint
CREATE INDEX `entity_logs_entity_table_idx` ON `entity_logs` (`entity_table`);--> statement-breakpoint
CREATE INDEX `entity_logs_created_at_idx` ON `entity_logs` (`created_at`);--> statement-breakpoint
CREATE INDEX `entity_logs_user_created_at_idx` ON `entity_logs` (`user_id`,`created_at`);--> statement-breakpoint
CREATE INDEX `entity_logs_entity_table_entity_id_idx` ON `entity_logs` (`entity_table`,`entity_id`);