Skip to content
This repository has been archived by the owner on Nov 27, 2019. It is now read-only.

Add database structure #1

Open
Alanaktion opened this issue Oct 4, 2015 · 6 comments
Open

Add database structure #1

Alanaktion opened this issue Oct 4, 2015 · 6 comments
Assignees

Comments

@Alanaktion
Copy link
Member

The database structure should be added as a part of the installation.

@Alanaktion Alanaktion self-assigned this Oct 4, 2015
@Alanaktion
Copy link
Member Author

Here's the current structure svlt-back.sql
Edit: This file is now outdated, use the new one a few comments down.

@Alanaktion
Copy link
Member Author

This should also be run since that last file
ALTER TABLE user ADD COLUMN tagline TEXT NULL AFTER name, ADD COLUMN bio BLOB NULL AFTER tagline

@Alanaktion
Copy link
Member Author

Also this, it adds the table definition for user_buddy:

CREATE TABLE `user_buddy` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `buddy_id` int(10) unsigned NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`buddy_id`),
  CONSTRAINT `user_buddy_buddy_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `user_buddy_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

@Alanaktion
Copy link
Member Author

Lots of changes! Here's the latest database structure: svlt.sql

@Alanaktion
Copy link
Member Author

Another update:

ALTER TABLE `user_key`   
  ADD COLUMN `buddy_id` INT(11) UNSIGNED NULL AFTER `user_id`,
  ADD CONSTRAINT `user_key_buddy_id` FOREIGN KEY (`buddy_id`) REFERENCES `user`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;

@Alanaktion
Copy link
Member Author

Alanaktion commented Jan 8, 2016

Here's the latest everything:

/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*Table structure for table `photo` */

CREATE TABLE `photo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `post_id` int(10) unsigned DEFAULT NULL,
  `content` mediumblob NOT NULL,
  `signature` blob NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `photo_user_id` (`user_id`),
  KEY `photo_post_id` (`post_id`),
  CONSTRAINT `photo_post_id` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `photo_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/*Table structure for table `post` */

CREATE TABLE `post` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `page_id` int(10) unsigned NOT NULL,
  `content` mediumblob NOT NULL,
  `signature` blob NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `post_user_id` (`user_id`),
  KEY `post_page_id` (`page_id`),
  CONSTRAINT `post_page_id` FOREIGN KEY (`page_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `post_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/*Table structure for table `post_comment` */

CREATE TABLE `post_comment` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `post_id` int(10) unsigned NOT NULL,
  `content` mediumblob NOT NULL,
  `signature` blob NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `post_comment_user_id` (`user_id`),
  KEY `post_comment_post_id` (`post_id`),
  CONSTRAINT `post_comment_post_id` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `post_comment_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/*Table structure for table `user` */

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(120) COLLATE utf8_unicode_ci NOT NULL,
  `fingerprint` binary(40) NOT NULL,
  `password_salt` binary(64) NOT NULL,
  `password_hash` varbinary(255) NOT NULL,
  `name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `tagline` text COLLATE utf8_unicode_ci,
  `bio` blob,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_username` (`username`),
  KEY `user_fingerprint` (`fingerprint`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/*Table structure for table `user_buddy` */

CREATE TABLE `user_buddy` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id_1` int(10) unsigned NOT NULL,
  `user_id_2` int(10) unsigned NOT NULL,
  `confirmed_at` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id_1`,`user_id_2`),
  KEY `user_buddy_user_id_2` (`user_id_2`),
  CONSTRAINT `user_buddy_user_id_1` FOREIGN KEY (`user_id_1`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `user_buddy_user_id_2` FOREIGN KEY (`user_id_2`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/*Table structure for table `user_key` */

CREATE TABLE `user_key` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned NOT NULL,
  `buddy_id` int(11) unsigned DEFAULT NULL,
  `fingerprint` binary(40) NOT NULL,
  `type` varchar(9) COLLATE ascii_bin NOT NULL,
  `key` blob NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_key_user_id` (`user_id`),
  KEY `user_key_fingerprint` (`fingerprint`),
  KEY `user_key_buddy_id` (`buddy_id`),
  CONSTRAINT `user_key_buddy_id` FOREIGN KEY (`buddy_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `user_key_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=ascii COLLATE=ascii_bin;

/*Table structure for table `user_token` */

CREATE TABLE `user_token` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `token` char(128) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  `expires_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_token_token` (`token`),
  KEY `user_token_user_id` (`user_id`),
  CONSTRAINT `user_token_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE VIEW `post_detail` AS (
    SELECT p.*,
        u1.username user_username, u1.fingerprint AS user_fingerprint, u1.name AS user_name,
        u2.username page_username, u2.fingerprint AS page_fingerprint, u2.name AS page_name
    FROM post p
    JOIN `user` u1 ON u1.id = p.user_id
    JOIN `user` u2 ON u2.id = p.page_id
);

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant