diff --git a/init.php b/init.php index a0a02122..da96770f 100644 --- a/init.php +++ b/init.php @@ -52,3 +52,7 @@ download('http://raw.github.com/aehlke/tag-it/master/js/tag-it.min.js', $libPath //fonts download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans.ttf', $fontsPath . 'DroidSans.ttf'); download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans-Bold.ttf', $fontsPath . 'DroidSans-Bold.ttf'); + + + +require_once 'upgrade.php'; diff --git a/src/Upgrades/Upgrade1.sql b/src/Upgrades/Upgrade1.sql new file mode 100644 index 00000000..8d167f6a --- /dev/null +++ b/src/Upgrades/Upgrade1.sql @@ -0,0 +1,84 @@ +CREATE TABLE user +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT, + pass_salt TEXT, + pass_hash TEXT, + staff_confirmed INTEGER, + email_unconfirmed TEXT, + email_confirmed TEXT, + email_token TEXT, + join_date INTEGER, + access_rank INTEGER, + settings TEXT +); + +CREATE TABLE tag +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT +); + +CREATE TABLE post_tag +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tag_id INTEGER, + post_id INTEGER, + FOREIGN KEY(tag_id) REFERENCES tag(id) ON DELETE CASCADE ON UPDATE SET NULL, + FOREIGN KEY(post_id) REFERENCES post(id) ON DELETE CASCADE ON UPDATE SET NULL +); +CREATE INDEX idx_fk_post_tag_post_id ON post_tag(post_id); +CREATE INDEX idx_fk_post_tag_tag_id ON post_tag(tag_id); +CREATE UNIQUE INDEX idx_uq_post_tag_tag_id_post_id ON post_tag(tag_id, post_id); + +CREATE TABLE favoritee +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + post_id INTEGER, + user_id INTEGER, + FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE ON UPDATE SET NULL, + FOREIGN KEY(post_id) REFERENCES post(id) ON DELETE CASCADE ON UPDATE SET NULL +); +CREATE INDEX idx_fk_favoritee_post_id ON favoritee(post_id); +CREATE INDEX idx_fk_favoritee_user_id ON favoritee(user_id); +CREATE UNIQUE INDEX idx_uq_favoritee_post_id_user_id ON favoritee(post_id, user_id); + +CREATE TABLE comment +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + post_id INTEGER, + commenter_id INTEGER, + comment_date INTEGER, + text TEXT, + FOREIGN KEY(post_id) REFERENCES post(id) ON DELETE CASCADE ON UPDATE SET NULL, + FOREIGN KEY(commenter_id) REFERENCES user(id) ON DELETE SET NULL ON UPDATE SET NULL +); +CREATE INDEX idx_fk_comment_commenter_id ON comment(commenter_id); +CREATE INDEX idx_fk_comment_post_id ON comment(post_id); + +CREATE TABLE post +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + type INTEGER, + name TEXT, + orig_name TEXT, + file_hash TEXT, + file_size INTEGER, + mime_type TEXT, + safety INTEGER, + hidden INTEGER, + upload_date INTEGER, + image_width INTEGER, + image_height INTEGER, + uploader_id INTEGER, + source TEXT, + FOREIGN KEY(uploader_id) REFERENCES user(id) ON DELETE SET NULL ON UPDATE SET NULL +); +CREATE INDEX idx_fk_post_uploader_id ON post(uploader_id); + +CREATE TABLE property +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + prop_id INTEGER, + value TEXT +); diff --git a/src/core.php b/src/core.php index 476872a8..30511856 100644 --- a/src/core.php +++ b/src/core.php @@ -43,6 +43,7 @@ function configFactory() $config = configFactory(); R::setup('sqlite:' . $config->main->dbPath); +R::freeze(true); R::dependencies(['tag' => ['post'], 'favoritee' => ['post', 'user'], 'comment' => ['post', 'user']]); //wire models diff --git a/upgrade.php b/upgrade.php new file mode 100644 index 00000000..93f89e17 --- /dev/null +++ b/upgrade.php @@ -0,0 +1,31 @@ + $dbVersion) + { + printf('Executing %s...' . PHP_EOL, $upgradePath); + $upgradeSql = file_get_contents($upgradePath); + $queries = preg_split('/;\s*[\r\n]+/s', $upgradeSql); + $queries = array_map('trim', $queries); + foreach ($queries as $query) + { + echo $query . PHP_EOL; + R::exec($query); + echo PHP_EOL; + } + } + + Model_Property::set('db-version', $upgradeVersion); +}