Skip to content

Commit

Permalink
Closed #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Kurczewski committed Oct 20, 2013
1 parent b4186a2 commit 230f555
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ function download($source, $destination = null)
//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';
84 changes: 84 additions & 0 deletions src/Upgrades/Upgrade1.sql
Original file line number Diff line number Diff line change
@@ -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
);
1 change: 1 addition & 0 deletions src/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
require_once 'src/core.php';
$config = configFactory();

$dbVersion = Model_Property::get('db-version');
printf('DB version = %d' . PHP_EOL, $dbVersion);

$upgrades = glob('src/Upgrades/*.sql');
natcasesort($upgrades);

foreach ($upgrades as $upgradePath)
{
preg_match('/(\d+)\.sql/', $upgradePath, $matches);
$upgradeVersion = intval($matches[1]);

if ($upgradeVersion > $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);
}

0 comments on commit 230f555

Please sign in to comment.