Commit 97ca1233 authored by Nicolas Joyard's avatar Nicolas Joyard

Squash all migrations

parent d609894b
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.conf import settings
def set_site_name(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
Site.objects.filter(pk=settings.SITE_ID).update(
name=settings.SITE_NAME, domain=settings.SITE_DOMAIN)
class Migration(migrations.Migration):
dependencies = [
('sites', '0001_initial'),
]
operations = [
migrations.RunPython(set_site_name),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('memopol_scores', '0001_initial'),
]
operations = [
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_vote_score"
AS SELECT
"representatives_votes_vote"."id" AS "vote_id",
decay_score(
CAST(CASE
WHEN "representatives_votes_vote"."position"::text = "representatives_recommendations_recommendation"."recommendation"::text
THEN "representatives_recommendations_recommendation"."weight"
ELSE 0 - "representatives_recommendations_recommendation"."weight"
END AS NUMERIC),
"representatives_votes_proposal"."datetime",
"decay_num"."value",
"decay_denom"."value",
"exponent"."value",
"decimals"."value"
) AS "score"
FROM "representatives_votes_vote"
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_NUM') "decay_num" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_DENOM') "decay_denom" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_EXPONENT') "exponent" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS INTEGER) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECIMALS') "decimals" ON 1=1
JOIN "representatives_votes_proposal" ON "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id"
LEFT JOIN "representatives_recommendations_recommendation" ON "representatives_votes_proposal"."id" = "representatives_recommendations_recommendation"."proposal_id"
WHERE "representatives_recommendations_recommendation"."id" IS NOT NULL;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_dossier_score"
AS SELECT
"representatives_votes_vote"."representative_id" AS "representative_id",
"representatives_votes_proposal"."dossier_id" AS "dossier_id",
SUM("memopol_scores_votescore"."score") AS "score"
FROM
"memopol_scores_votescore"
INNER JOIN "representatives_votes_vote"
ON "memopol_scores_votescore"."vote_id" = "representatives_votes_vote"."id"
INNER JOIN "representatives_votes_proposal"
ON "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id"
GROUP BY
"representatives_votes_vote"."representative_id",
"representatives_votes_proposal"."dossier_id"
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_position_score"
AS SELECT
"representatives_positions_position"."id" AS "position_id",
decay_score(
"representatives_positions_position"."score",
"representatives_positions_position"."datetime",
"decay_num"."value",
"decay_denom"."value",
"exponent"."value",
"decimals"."value"
) AS "score"
FROM
"representatives_positions_position"
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_NUM') "decay_num" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_DENOM') "decay_denom" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_EXPONENT') "exponent" ON 1=1
JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS INTEGER) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECIMALS') "decimals" ON 1=1;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_representative_score"
AS SELECT
"source"."representative_id" AS "representative_id" ,
SUM("source"."score") AS "score"
FROM
(
SELECT
"memopol_scores_dossierscore"."representative_id" AS "representative_id",
"memopol_scores_dossierscore"."score" AS "score"
FROM "memopol_scores_dossierscore"
UNION ALL
SELECT
"representatives_positions_position"."representative_id" AS "representative_id",
"memopol_scores_positionscore"."score" AS "score"
FROM
"memopol_scores_positionscore"
INNER JOIN "representatives_positions_position"
ON "memopol_scores_positionscore"."position_id" = "representatives_positions_position"."id"
) "source"
GROUP BY
"source"."representative_id"
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_vote_scores()
RETURNS VOID AS $$
BEGIN
TRUNCATE TABLE "memopol_scores_votescore";
INSERT INTO "memopol_scores_votescore" ("vote_id", "score")
SELECT "vote_id", "score" FROM "memopol_scores_v_vote_score";
END;
$$ LANGUAGE PLPGSQL;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_dossier_scores()
RETURNS VOID AS $$
BEGIN
TRUNCATE TABLE "memopol_scores_dossierscore";
PERFORM refresh_vote_scores();
INSERT INTO "memopol_scores_dossierscore" ("representative_id", "dossier_id", "score")
SELECT "representative_id", "dossier_id", "score" FROM "memopol_scores_v_dossier_score";
END;
$$ LANGUAGE PLPGSQL;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_position_scores()
RETURNS VOID AS $$
BEGIN
TRUNCATE TABLE "memopol_scores_positionscore";
INSERT INTO "memopol_scores_positionscore" ("position_id", "score")
SELECT "position_id", "score" FROM "memopol_scores_v_position_score";
END;
$$ LANGUAGE PLPGSQL;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_representative_scores()
RETURNS VOID AS $$
BEGIN
TRUNCATE TABLE "memopol_scores_representativescore";
PERFORM refresh_dossier_scores();
PERFORM refresh_position_scores();
INSERT INTO "memopol_scores_representativescore" ("representative_id", "score")
SELECT
"representatives_representative"."id",
COALESCE("memopol_scores_v_representative_score"."score", 0)
FROM
"representatives_representative"
LEFT OUTER JOIN "memopol_scores_v_representative_score"
ON "memopol_scores_v_representative_score"."representative_id" = "representatives_representative"."id";
END;
$$ LANGUAGE PLPGSQL;
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_scores()
RETURNS VOID AS $$
BEGIN
PERFORM refresh_representative_scores();
END;
$$ LANGUAGE PLPGSQL;
"""
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('representatives', '0020_rep_unique_slug_remove_remoteid'),
('memopol_themes', '0003_remove_themescore'),
('memopol_scores', '0002_create_views'),
]
operations = [
migrations.CreateModel(
name='ThemeScore',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('score', models.FloatField()),
('representative', models.ForeignKey(related_name='theme_scores', to='representatives.Representative')),
('theme', models.ForeignKey(to='memopol_themes.Theme')),
],
),
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_theme_score"
AS SELECT
"scoresource"."representative_id" AS "representative_id",
"scoresource"."theme_id" AS "theme_id",
SUM("scoresource"."score") AS "score"
FROM
(
SELECT
"memopol_scores_dossierscore"."representative_id" AS "representative_id",
"memopol_themes_theme_dossiers"."theme_id" AS "theme_id",
"memopol_scores_dossierscore"."score" AS "score"
FROM
"memopol_scores_dossierscore"
INNER JOIN "memopol_themes_theme_dossiers"
ON "memopol_themes_theme_dossiers"."dossier_id" = "memopol_scores_dossierscore"."dossier_id"
UNION ALL
SELECT
"representatives_votes_vote"."representative_id" AS "representative_id",
"memopol_themes_theme_proposals"."theme_id" AS "theme_id",
"memopol_scores_votescore"."score" AS "score"
FROM
"memopol_scores_votescore"
INNER JOIN "representatives_votes_vote"
ON "representatives_votes_vote"."id" = "memopol_scores_votescore"."vote_id"
INNER JOIN "memopol_themes_theme_proposals"
ON "memopol_themes_theme_proposals"."proposal_id" = "representatives_votes_vote"."proposal_id"
UNION ALL
SELECT
"representatives_positions_position"."representative_id" AS "representative_id",
"memopol_themes_theme_positions"."theme_id" AS "theme_id",
"memopol_scores_positionscore"."score" AS "score"
FROM
"memopol_scores_positionscore"
INNER JOIN "representatives_positions_position"
ON "representatives_positions_position"."id" = "memopol_scores_positionscore"."position_id"
INNER JOIN "memopol_themes_theme_positions"
ON "memopol_themes_theme_positions"."position_id" = "memopol_scores_positionscore"."position_id"
) "scoresource"
GROUP BY
"scoresource"."representative_id",
"scoresource"."theme_id"
"""
),
migrations.RunSQL(
"""
DROP FUNCTION refresh_vote_scores();
"""
),
migrations.RunSQL(
"""
DROP FUNCTION refresh_dossier_scores();
"""
),
migrations.RunSQL(
"""
DROP FUNCTION refresh_position_scores();
"""
),
migrations.RunSQL(
"""
DROP FUNCTION refresh_representative_scores();
"""
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION refresh_scores()
RETURNS VOID AS $$
BEGIN
TRUNCATE TABLE "memopol_scores_representativescore";
TRUNCATE TABLE "memopol_scores_dossierscore";
TRUNCATE TABLE "memopol_scores_votescore";
INSERT INTO "memopol_scores_votescore" ("vote_id", "score")
SELECT "vote_id", "score" FROM "memopol_scores_v_vote_score";
INSERT INTO "memopol_scores_dossierscore" ("representative_id", "dossier_id", "score")
SELECT "representative_id", "dossier_id", "score" FROM "memopol_scores_v_dossier_score";
TRUNCATE TABLE "memopol_scores_positionscore";
INSERT INTO "memopol_scores_positionscore" ("position_id", "score")
SELECT "position_id", "score" FROM "memopol_scores_v_position_score";
TRUNCATE TABLE "memopol_scores_themescore";
INSERT INTO "memopol_scores_themescore" ("representative_id", "theme_id", "score")
SELECT
"representatives_representative"."id",
"memopol_themes_theme"."id",
COALESCE("memopol_scores_v_theme_score"."score", 0)
FROM
"representatives_representative"
INNER JOIN "memopol_themes_theme" ON 1=1
LEFT OUTER JOIN "memopol_scores_v_theme_score"
ON "memopol_scores_v_theme_score"."representative_id" = "representatives_representative"."id"
AND "memopol_scores_v_theme_score"."theme_id" = "memopol_themes_theme"."id";
INSERT INTO "memopol_scores_representativescore" ("representative_id", "score")
SELECT
"representatives_representative"."id",
COALESCE("memopol_scores_v_representative_score"."score", 0)
FROM
"representatives_representative"
LEFT OUTER JOIN "memopol_scores_v_representative_score"
ON "memopol_scores_v_representative_score"."representative_id" = "representatives_representative"."id";
END;
$$ LANGUAGE PLPGSQL;
"""
),
migrations.RunSQL(
"""
SELECT refresh_scores();
"""
)
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('memopol_scores', '0003_themescore'),
]
operations = [
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "memopol_scores_v_theme_score"
AS SELECT
"scoresource"."representative_id" AS "representative_id",
"scoresource"."theme_id" AS "theme_id",
SUM("scoresource"."score") AS "score"
FROM
(
-- Score contribution for proposals
SELECT
"representatives_votes_vote"."representative_id" AS "representative_id",
"proposal_themes"."theme_id" AS "theme_id",
"memopol_scores_votescore"."score" AS "score"
FROM
"memopol_scores_votescore"
INNER JOIN "representatives_votes_vote"
ON "representatives_votes_vote"."id" = "memopol_scores_votescore"."vote_id"
INNER JOIN (
-- Proposals with a theme
SELECT
"representatives_votes_proposal"."id" AS "proposal_id",
"memopol_themes_theme_proposals"."theme_id" AS "theme_id"
FROM
"representatives_votes_proposal"
INNER JOIN "memopol_themes_theme_proposals"
ON "representatives_votes_proposal"."id" = "memopol_themes_theme_proposals"."proposal_id"
UNION
-- Proposals in a dossier with a theme
SELECT
"representatives_votes_proposal"."id" AS "proposal_id",
"memopol_themes_theme_dossiers"."theme_id" AS "theme_id"
FROM
"representatives_votes_proposal"
INNER JOIN "representatives_votes_dossier"
ON "representatives_votes_dossier"."id" = "representatives_votes_proposal"."dossier_id"
INNER JOIN "memopol_themes_theme_dossiers"
ON "memopol_themes_theme_dossiers"."dossier_id" = "representatives_votes_dossier"."id"
) "proposal_themes"
ON "proposal_themes"."proposal_id" = "representatives_votes_vote"."proposal_id"
UNION ALL
-- Score contribution for positions
SELECT
"representatives_positions_position"."representative_id" AS "representative_id",
"memopol_themes_theme_positions"."theme_id" AS "theme_id",
"memopol_scores_positionscore"."score" AS "score"
FROM
"memopol_scores_positionscore"
INNER JOIN "representatives_positions_position"
ON "representatives_positions_position"."id" = "memopol_scores_positionscore"."position_id"
INNER JOIN "memopol_themes_theme_positions"
ON "memopol_themes_theme_positions"."position_id" = "memopol_scores_positionscore"."position_id"
) "scoresource"
GROUP BY
"scoresource"."representative_id",
"scoresource"."theme_id"
"""
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.core import serializers
from django.db import migrations, models
fixture_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../fixtures'))
fixture_filename = 'score_settings.json'
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()
class Migration(migrations.Migration):
dependencies = [
......@@ -18,4 +38,6 @@ class Migration(migrations.Migration):
('comment', models.TextField()),
],
),
migrations.RunPython(load_fixture),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.core import serializers
from django.db import migrations
fixture_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../fixtures'))
fixture_filename = 'score_settings.json'
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()
class Migration(migrations.Migration):
dependencies = [
('memopol_settings', '0001_initial'),
]
operations = [
migrations.RunPython(load_fixture),
]
......@@ -8,11 +8,23 @@ import autoslug.fields
class Migration(migrations.Migration):
dependencies = [
('representatives_votes', '0012_document'),
('representatives_positions', '0003_remove_position_tags'),
('representatives_votes', '0001_initial'),
('representatives_positions', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='ThemeScore',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('score', models.FloatField(default=0)),
],
options={
'ordering': ['theme__slug'],
'db_table': 'memopol_themes_themescore',
'managed': False,
},
),
migrations.CreateModel(
name='Theme',
fields=[
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('memopol_themes', '0001_initial'),
('representatives_positions', '0006_positionscore'),
]
operations = [
migrations.CreateModel(
name='ThemeScore',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('score', models.FloatField(default=0)),
],
options={
'ordering': ['theme__slug'],
'db_table': 'memopol_themes_themescore',
'managed': False,
},
),
migrations.RunSQL(
"""
CREATE VIEW "memopol_themes_themescore"
AS SELECT
"representatives_representative"."id" AS "representative_id",
"memopol_themes_theme"."id" AS "theme_id",
SUM(COALESCE("scoresource"."score", 0)) AS "score"
FROM
"representatives_representative"
LEFT OUTER JOIN "memopol_themes_theme"
ON 1=1
LEFT OUTER JOIN (
SELECT
"representatives_recommendations_dossierscores"."representative_id" AS "representative_id",
"memopol_themes_theme"."id" AS "theme_id",
"representatives_recommendations_dossierscores"."score" AS "score"
FROM
"representatives_recommendations_dossierscores"
INNER JOIN "memopol_themes_theme_dossiers"
ON "memopol_themes_theme_dossiers"."dossier_id" = "representatives_recommendations_dossierscores"."dossier_id"
INNER JOIN "memopol_themes_theme"
ON "memopol_themes_theme"."id" = "memopol_themes_theme_dossiers"."theme_id"
UNION ALL
SELECT
"representatives_recommendations_votescores"."representative_id" AS "representative_id",
"memopol_themes_theme"."id" AS "theme_id",
"representatives_recommendations_votescores"."score" AS "score"
FROM
"representatives_recommendations_votescores"
INNER JOIN "memopol_themes_theme_proposals"
ON "memopol_themes_theme_proposals"."proposal_id" = "representatives_recommendations_votescores"."proposal_id"
INNER JOIN "memopol_themes_theme"
ON "memopol_themes_theme"."id" = "memopol_themes_theme_proposals"."theme_id"
UNION ALL
SELECT
"representatives_positions_positionscore"."representative_id" AS "representative_id",
"memopol_themes_theme"."id" AS "theme_id",
"representatives_positions_positionscore"."score" AS "score"
FROM
"representatives_positions_positionscore"
INNER JOIN "memopol_themes_theme_positions"
ON "memopol_themes_theme_positions"."position_id" = "representatives_positions_positionscore"."position_id"
INNER JOIN "memopol_themes_theme"
ON "memopol_themes_theme"."id" = "memopol_themes_theme_positions"."theme_id"
) "scoresource"
ON "scoresource"."theme_id" = "memopol_themes_theme"."id"
AND "scoresource"."representative_id" = "representatives_representative"."id"
GROUP BY
"representatives_representative"."id",
"memopol_themes_theme"."id"
"""
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('memopol_themes', '0002_themescore'),
]
operations = [
migrations.RunSQL(
"""
DROP VIEW "memopol_themes_themescore";
"""
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.core import serializers
from django.db import migrations
fixture_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../fixtures'))
fixture_filename = 'country_initial_data.json'
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()
def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."
MyModel = apps.get_model("representatives", "Country")
MyModel.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
('representatives', '0001_initial'),
]
operations = [
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('representatives', '0002_fixtures'),
]
operations = [
migrations.CreateModel(
name='Address',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),