Added banners to repo.
|
@ -0,0 +1,58 @@
|
|||
exports.up = async (knex) => Promise.resolve()
|
||||
.then(() => knex.schema.createTable('random_campaign', (table) => {
|
||||
table.text('banner_id')
|
||||
.references('id')
|
||||
.inTable('banners');
|
||||
|
||||
table.text('url');
|
||||
|
||||
table.integer('entity_id')
|
||||
.references('id')
|
||||
.inTable('entities');
|
||||
|
||||
table.string('affiliate_id')
|
||||
.references('id')
|
||||
.inTable('affiliates');
|
||||
|
||||
table.integer('parent_id')
|
||||
.references('id')
|
||||
.inTable('entities');
|
||||
}))
|
||||
.then(() => knex.raw(`
|
||||
ALTER TABLE banners ADD COLUMN ratio numeric GENERATED ALWAYS AS (ROUND(width::decimal/ height::decimal, 2)) STORED;
|
||||
`))
|
||||
.then(() => knex.raw(`
|
||||
DROP FUNCTION IF EXISTS get_random_campaign;
|
||||
DROP FUNCTION IF EXISTS banners_ratio;
|
||||
|
||||
CREATE FUNCTION get_random_campaign(min_ratio decimal default 0, max_ratio decimal default 1000.0) RETURNS random_campaign AS $$
|
||||
SELECT * FROM (
|
||||
SELECT DISTINCT ON (CASE WHEN parent_id IS NOT NULL THEN parent_id ELSE entity_id END)
|
||||
banner_id, url, entity_id, affiliate_id, parent_id
|
||||
FROM (
|
||||
SELECT
|
||||
campaigns.*, entities.parent_id as parent_id
|
||||
FROM campaigns
|
||||
LEFT JOIN entities ON entities.id = campaigns.entity_id
|
||||
LEFT JOIN banners ON banners.id = campaigns.banner_id
|
||||
WHERE banner_id IS NOT NULL
|
||||
AND ratio >= min_ratio
|
||||
AND ratio <= max_ratio
|
||||
ORDER BY RANDOM()
|
||||
) random_campaigns
|
||||
) random_banners
|
||||
ORDER BY RANDOM()
|
||||
LIMIT 1;
|
||||
$$ LANGUAGE SQL STABLE;
|
||||
`));
|
||||
|
||||
exports.down = async (knex) => knex.raw(`
|
||||
DROP FUNCTION IF EXISTS get_random_campaign;
|
||||
DROP TABLE IF EXISTS random_campaign;
|
||||
|
||||
ALTER TABLE banners DROP COLUMN ratio;
|
||||
|
||||
CREATE FUNCTION banners_ratio(banner banners) RETURNS numeric AS $$
|
||||
SELECT ROUND(banner.width::decimal / banner.height::decimal, 2);
|
||||
$$ LANGUAGE SQL STABLE;
|
||||
`);
|
After Width: | Height: | Size: 145 KiB |
After Width: | Height: | Size: 191 KiB |
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 100 KiB |
After Width: | Height: | Size: 116 KiB |
After Width: | Height: | Size: 99 KiB |
After Width: | Height: | Size: 101 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 114 KiB |
After Width: | Height: | Size: 105 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 93 KiB |
After Width: | Height: | Size: 109 KiB |
After Width: | Height: | Size: 106 KiB |
After Width: | Height: | Size: 105 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 110 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 93 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 90 KiB |
After Width: | Height: | Size: 103 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 72 KiB |
After Width: | Height: | Size: 75 KiB |
After Width: | Height: | Size: 96 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 83 KiB |
After Width: | Height: | Size: 74 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 87 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 99 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 77 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 93 KiB |
After Width: | Height: | Size: 87 KiB |
After Width: | Height: | Size: 75 KiB |
After Width: | Height: | Size: 87 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 93 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 95 KiB |
After Width: | Height: | Size: 97 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 86 KiB |
After Width: | Height: | Size: 98 KiB |
After Width: | Height: | Size: 100 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 107 KiB |
After Width: | Height: | Size: 115 KiB |
After Width: | Height: | Size: 103 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 58 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 87 KiB |