<?php

echo "
    ALTER TABLE `mland`.`users` 
ADD COLUMN `id_ref` INT(11) UNSIGNED NOT NULL AFTER `news_ntf`;

CREATE TABLE `users_dialogs` (
`id_dialog` int(11) NOT NULL AUTO_INCREMENT,
`time` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id_dialog`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `mland`.`users_dialogs` 
CHANGE COLUMN `id_dialog` `id_dialog` INT(10) UNSIGNED NOT NULL ;

CREATE TABLE `users_messages` (
`id_message` int(10) NOT NULL AUTO_INCREMENT,
`id_dialog` int(10) unsigned NOT NULL,
`id_user` int(10) unsigned NOT NULL,
`type` tinyint(1) unsigned NOT NULL,
`message` varchar(1024) DEFAULT NULL,
`money` int(11) unsigned NOT NULL,
`iron` int(11) unsigned NOT NULL,
`bottles` int(11) unsigned NOT NULL,
`item` int(11) unsigned NOT NULL,
`time` int(11) unsigned NOT NULL,
`status` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id_message`),
KEY `id_user` (`id_user`),
KEY `id_dialog` (`id_dialog`),
CONSTRAINT `users_messages_ibfk_1`FOREIGN KEY (id_user) REFERENCES users(id_user) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `users_messages_ibfk_2`FOREIGN KEY (id_dialog) REFERENCES users_dialogs(id_dialog) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `mland`.`users_messages` 
CHANGE COLUMN `id_message` `id_message` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ;

CREATE TABLE `users_messages_for_users` (
  `id_message` int(10) unsigned NOT NULL,
  `id_user` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_message`,`id_user`),
  KEY `id_user` (`id_user`),
  CONSTRAINT `users_messages_for_users_ibfk_1` FOREIGN KEY (`id_message`) REFERENCES `users_messages` (`id_message`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `users_messages_for_users_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `users_dialogs_for_users` (
  `id_dialog` int(10) unsigned NOT NULL,
  `id_user` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_dialog`,`id_user`),
  KEY `id_dialog` (`id_dialog`),
  CONSTRAINT `users_dialogs_for_users_ibfk_1` FOREIGN KEY (`id_dialog`) REFERENCES `users_dialogs` (`id_dialog`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `users_dialogs_for_users_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `mland`.`users_dialogs` 
CHANGE COLUMN `id_dialog` `id_dialog` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ;

INSERT INTO `mland`.`roles` (`id_role`, `id_cat_role`, `name_role_ru`, `name_role_ua`, `value_role`) VALUES ('9', '5', 'Системный бот', '', 'system');

";

