

CREATE DATABASE /*!32312 IF NOT EXISTS*/`upload_server` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `upload_server`;


CREATE TABLE `uploadserver_files` (
`id` int(10) unsigned NOT NULL PRIMARY KEY ,
`filename` VARCHAR( 100 ) NOT NULL ,
`filesize` INT NOT NULL ,
`download_count` INT NOT NULL ,
`id_owner` INT NOT NULL ,
`id_group` INT NOT NULL ,
`add_date` DATE NOT NULL ,
`permissions` VARCHAR( 9 ) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `uploadserver_directories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`id_parent` INT NOT NULL DEFAULT '1',
`id_user` INT NOT NULL DEFAULT '1',
`id_group` INT NOT NULL DEFAULT '1',
`permissions` VARCHAR( 9 ) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `uploadserver_user-group` (
  `id_user` int(10) unsigned NOT NULL,
  `id_group` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_user`,`id_group`),
  KEY `FK_user` (`id_user`),
  KEY `FK_group` (`id_group`),
  CONSTRAINT `FK_user` FOREIGN KEY (`id_user`) REFERENCES `uploadserver_users` (`id`),
  CONSTRAINT `FK_group` FOREIGN KEY (`id_group`) REFERENCES `uploadserver_groups` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `uploadserver_file-directory` (
  `id_file` int(10) unsigned NOT NULL,
  `id_directory` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_file`,`id_directory`),
  KEY `FK_file` (`id_file`),
  KEY `FK_directory` (`id_directory`),
  CONSTRAINT `FK_file` FOREIGN KEY (`id_file`) REFERENCES `uploadserver_files` (`id`),
  CONSTRAINT `FK_directory` FOREIGN KEY (`id_directory`) REFERENCES `uploadserver_directories` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `uploadserver_user-group` (`id_user`, `id_group`)
VALUES (1, 1);

INSERT INTO `uploadserver_directories` (`id`, `name`, `id_parent`, `id_user`, `id_group`, `permissions`)
VALUES (1 , '/', '1', '1', '1', 'rwrwrw');
