forked from DebaucheryLibrarian/traxxx
Normalized database. Updated seed files. Simplified seed upsert.
This commit is contained in:
@@ -17,6 +17,184 @@ exports.up = knex => Promise.resolve()
|
||||
table.integer('priority', 2)
|
||||
.defaultTo(0);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('media', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('path');
|
||||
table.string('thumbnail');
|
||||
table.integer('index');
|
||||
table.string('mime');
|
||||
|
||||
table.string('type');
|
||||
table.string('quality', 6);
|
||||
|
||||
table.string('hash');
|
||||
table.text('comment');
|
||||
table.string('source', 1000);
|
||||
|
||||
table.unique('hash');
|
||||
table.unique('source');
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags_groups', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.string('name', 32);
|
||||
table.text('description');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags', (table) => {
|
||||
table.increments('id', 12);
|
||||
table.string('name');
|
||||
|
||||
table.text('description');
|
||||
|
||||
table.integer('priority', 2)
|
||||
.defaultTo(0);
|
||||
|
||||
table.integer('group_id', 12)
|
||||
.references('id')
|
||||
.inTable('tags_groups');
|
||||
|
||||
table.integer('alias_for', 12)
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags_posters', (table) => {
|
||||
table.integer('tag_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique('tag_id');
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags_photos', (table) => {
|
||||
table.integer('tag_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique(['tag_id', 'media_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('networks', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
table.string('parameters');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('networks_social', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('url');
|
||||
table.string('platform');
|
||||
|
||||
table.integer('network_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('networks');
|
||||
|
||||
table.unique(['url', 'network_id']);
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('sites', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.integer('network_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('networks');
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
table.string('parameters');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('sites_tags', (table) => {
|
||||
table.integer('tag_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.integer('site_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('sites');
|
||||
|
||||
table.unique(['tag_id', 'site_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('sites_social', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('url');
|
||||
table.string('platform');
|
||||
|
||||
table.integer('site_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('sites');
|
||||
|
||||
table.unique(['url', 'site_id']);
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('studios', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.integer('network_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('networks');
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('actors', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
@@ -70,6 +248,48 @@ exports.up = knex => Promise.resolve()
|
||||
table.datetime('scraped_at');
|
||||
table.boolean('scrape_success');
|
||||
}))
|
||||
.then(() => knex.schema.createTable('actors_avatars', (table) => {
|
||||
table.integer('actor_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('actors');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique('actor_id');
|
||||
}))
|
||||
.then(() => knex.schema.createTable('actors_photos', (table) => {
|
||||
table.integer('actor_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('actors');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique(['actor_id', 'media_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('actors_social', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('url');
|
||||
table.string('platform');
|
||||
|
||||
table.integer('actor_id', 8)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('actors');
|
||||
|
||||
table.unique(['url', 'actor_id']);
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('directors', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
@@ -84,92 +304,6 @@ exports.up = knex => Promise.resolve()
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags_groups', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.string('name', 32);
|
||||
table.text('description');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags', (table) => {
|
||||
table.increments('id', 12);
|
||||
table.string('name');
|
||||
|
||||
table.text('description');
|
||||
|
||||
table.integer('priority', 2)
|
||||
.defaultTo(0);
|
||||
|
||||
table.integer('group_id', 12)
|
||||
.references('id')
|
||||
.inTable('tags_groups');
|
||||
|
||||
table.integer('alias_for', 12)
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('networks', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
table.string('parameters');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('sites', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.integer('network_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('networks');
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
table.string('parameters');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('studios', (table) => {
|
||||
table.increments('id', 12);
|
||||
|
||||
table.integer('network_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('networks');
|
||||
|
||||
table.string('name');
|
||||
table.string('url');
|
||||
table.text('description');
|
||||
|
||||
table.string('slug', 32)
|
||||
.unique();
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('releases', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
@@ -193,10 +327,6 @@ exports.up = knex => Promise.resolve()
|
||||
table.date('date');
|
||||
table.text('description');
|
||||
|
||||
table.integer('director', 12)
|
||||
.references('id')
|
||||
.inTable('directors');
|
||||
|
||||
table.integer('duration')
|
||||
.unsigned();
|
||||
|
||||
@@ -209,48 +339,7 @@ exports.up = knex => Promise.resolve()
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('media', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('path');
|
||||
table.string('thumbnail');
|
||||
table.integer('index');
|
||||
table.string('mime');
|
||||
|
||||
table.string('domain');
|
||||
table.integer('target_id', 16);
|
||||
|
||||
table.json('target');
|
||||
|
||||
table.string('role');
|
||||
table.string('quality', 6);
|
||||
|
||||
table.string('hash');
|
||||
table.text('comment');
|
||||
table.string('source', 1000);
|
||||
|
||||
table.unique(['domain', 'target_id', 'role', 'hash']);
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('social', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
table.string('url');
|
||||
table.string('platform');
|
||||
|
||||
table.string('domain');
|
||||
table.integer('target_id', 16);
|
||||
|
||||
table.unique(['url', 'domain', 'target_id']);
|
||||
|
||||
table.datetime('created_at')
|
||||
.defaultTo(knex.fn.now());
|
||||
}))
|
||||
.then(() => knex.schema.createTable('actors_associated', (table) => {
|
||||
// table.increments('id', 16);
|
||||
|
||||
.then(() => knex.schema.createTable('releases_actors', (table) => {
|
||||
table.integer('release_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
@@ -263,9 +352,7 @@ exports.up = knex => Promise.resolve()
|
||||
|
||||
table.unique(['release_id', 'actor_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('directors_associated', (table) => {
|
||||
table.increments('id', 16);
|
||||
|
||||
.then(() => knex.schema.createTable('releases_directors', (table) => {
|
||||
table.integer('release_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
@@ -278,65 +365,85 @@ exports.up = knex => Promise.resolve()
|
||||
|
||||
table.unique(['release_id', 'director_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('tags_associated', (table) => {
|
||||
.then(() => knex.schema.createTable('releases_posters', (table) => {
|
||||
table.integer('release_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('releases');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique('release_id');
|
||||
}))
|
||||
.then(() => knex.schema.createTable('releases_photos', (table) => {
|
||||
table.integer('release_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('releases');
|
||||
|
||||
table.integer('media_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('media');
|
||||
|
||||
table.unique(['release_id', 'media_id']);
|
||||
}))
|
||||
.then(() => knex.schema.createTable('releases_tags', (table) => {
|
||||
table.integer('tag_id', 12)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('tags');
|
||||
|
||||
table.string('domain');
|
||||
table.integer('target_id', 16);
|
||||
table.integer('release_id', 16)
|
||||
.notNullable()
|
||||
.references('id')
|
||||
.inTable('releases');
|
||||
|
||||
table.unique(['domain', 'tag_id', 'target_id']);
|
||||
table.unique(['tag_id', 'release_id']);
|
||||
}))
|
||||
.then(() => knex.raw(`
|
||||
CREATE VIEW releases_media AS SELECT * FROM media WHERE domain = 'releases';
|
||||
CREATE VIEW actors_media AS SELECT * FROM media WHERE domain = 'actors';
|
||||
CREATE VIEW tags_media AS SELECT * FROM media WHERE domain = 'tags';
|
||||
|
||||
CREATE VIEW releases_tags AS SELECT tags_associated.*,tags.slug FROM tags_associated LEFT JOIN tags ON tags_associated.tag_id = tags.id WHERE domain = 'releases';
|
||||
/* used for sorting release actors and tags */
|
||||
CREATE VIEW releases_actors AS SELECT actors_associated.*,actors.gender,actors.birthdate FROM actors_associated LEFT JOIN actors ON actors_associated.actor_id = actors.id;
|
||||
|
||||
CREATE VIEW actors_social AS SELECT * FROM social WHERE domain = 'actors';
|
||||
|
||||
COMMENT ON VIEW releases_media IS E'@foreignKey (target_id) references releases (id)|@fieldName releaseMedia';
|
||||
COMMENT ON VIEW actors_media IS E'@foreignKey (target_id) references actors (id)|@fieldName actorMedia';
|
||||
COMMENT ON VIEW tags_media IS E'@foreignKey (target_id) references tags (id)|@fieldName tagMedia';
|
||||
|
||||
COMMENT ON VIEW actors_social IS E'@foreignKey (target_id) references actors (id)|@fieldName actorSocial';
|
||||
|
||||
COMMENT ON VIEW releases_tags IS E'@foreignKey (target_id) references releases (id)|@fieldName tagRelease\n@foreignKey (tag_id) references tags (id)|@fieldName releaseTag';
|
||||
|
||||
/* restore foreign keys in view, used for sorting release actors */
|
||||
COMMENT ON VIEW releases_actors IS E'@foreignKey (release_id) references releases (id)|@fieldName actorRelease\n@foreignKey (actor_id) references actors (id)|@fieldName releaseActor';
|
||||
|
||||
/* allow conversion resolver to be added for height and weight */
|
||||
COMMENT ON COLUMN actors.height IS E'@omit read,update,create,delete,all,many';
|
||||
COMMENT ON COLUMN actors.weight IS E'@omit read,update,create,delete,all,many';
|
||||
|
||||
/*
|
||||
create function releases_by_tag_slugs(slugs text[]) returns setof releases as $$
|
||||
select distinct on (releases.id) releases.* from releases
|
||||
join releases_tags on (releases_tags.release_id = releases.id)
|
||||
join tags on (releases_tags.tag_id = tags.id)
|
||||
where tags.slug = ANY($1);
|
||||
$$ language sql stable
|
||||
*/
|
||||
`));
|
||||
|
||||
exports.down = knex => Promise.resolve()
|
||||
.then(() => knex.raw(`
|
||||
DROP VIEW releases_media;
|
||||
DROP VIEW actors_media;
|
||||
DROP VIEW tags_media;
|
||||
exports.down = knex => knex.raw(`
|
||||
DROP FUNCTION IF EXISTS releases_by_tag_slugs;
|
||||
|
||||
DROP VIEW releases_tags;
|
||||
DROP VIEW releases_actors;
|
||||
|
||||
DROP VIEW actors_social;
|
||||
`))
|
||||
.then(() => knex.schema.dropTable('tags_associated'))
|
||||
.then(() => knex.schema.dropTable('directors_associated'))
|
||||
.then(() => knex.schema.dropTable('actors_associated'))
|
||||
.then(() => knex.schema.dropTable('tags'))
|
||||
.then(() => knex.schema.dropTable('tags_groups'))
|
||||
.then(() => knex.schema.dropTable('media'))
|
||||
.then(() => knex.schema.dropTable('social'))
|
||||
.then(() => knex.schema.dropTable('actors'))
|
||||
.then(() => knex.schema.dropTable('releases'))
|
||||
.then(() => knex.schema.dropTable('sites'))
|
||||
.then(() => knex.schema.dropTable('studios'))
|
||||
.then(() => knex.schema.dropTable('directors'))
|
||||
.then(() => knex.schema.dropTable('networks'))
|
||||
.then(() => knex.schema.dropTable('countries'));
|
||||
DROP TABLE IF EXISTS releases_actors CASCADE;
|
||||
DROP TABLE IF EXISTS releases_directors CASCADE;
|
||||
DROP TABLE IF EXISTS releases_posters CASCADE;
|
||||
DROP TABLE IF EXISTS releases_photos CASCADE;
|
||||
DROP TABLE IF EXISTS releases_tags CASCADE;
|
||||
DROP TABLE IF EXISTS actors_avatars CASCADE;
|
||||
DROP TABLE IF EXISTS actors_photos CASCADE;
|
||||
DROP TABLE IF EXISTS actors_social CASCADE;
|
||||
DROP TABLE IF EXISTS sites_tags CASCADE;
|
||||
DROP TABLE IF EXISTS sites_social CASCADE;
|
||||
DROP TABLE IF EXISTS networks_social CASCADE;
|
||||
DROP TABLE IF EXISTS tags_posters CASCADE;
|
||||
DROP TABLE IF EXISTS tags_photos CASCADE;
|
||||
DROP TABLE IF EXISTS releases CASCADE;
|
||||
DROP TABLE IF EXISTS actors CASCADE;
|
||||
DROP TABLE IF EXISTS directors CASCADE;
|
||||
DROP TABLE IF EXISTS tags CASCADE;
|
||||
DROP TABLE IF EXISTS tags_groups CASCADE;
|
||||
DROP TABLE IF EXISTS social CASCADE;
|
||||
DROP TABLE IF EXISTS sites CASCADE;
|
||||
DROP TABLE IF EXISTS studios CASCADE;
|
||||
DROP TABLE IF EXISTS media CASCADE;
|
||||
DROP TABLE IF EXISTS countries CASCADE;
|
||||
DROP TABLE IF EXISTS networks CASCADE;
|
||||
`);
|
||||
|
||||
Reference in New Issue
Block a user