New Architecture for SourceCode management

<!-- 1st STEP: Compatibility with previous and "current" architecture -->
<!-- MySQL -->

CREATE TABLE `projects_code` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `real_type_id` integer NOT NULL,
    `project_id` integer NOT NULL,
    `upload_time` datetime NOT NULL,
    `exclude_paths` longtext,
    `lines_ready` bool NOT NULL
)
;
ALTER TABLE `projects_code` ADD CONSTRAINT `project_id_refs_id_992dd950` FOREIGN KEY (`project_id`) REFERENCES `projects_project` (`id`);
ALTER TABLE `projects_code` ADD CONSTRAINT `real_type_id_refs_id_a0185c2b` FOREIGN KEY (`real_type_id`) REFERENCES `django_content_type` (`id`);

CREATE TABLE `projects_repository` (
    `code_ptr_id` integer NOT NULL PRIMARY KEY,
    `url` varchar(200) NOT NULL,
    `software` varchar(3) NOT NULL
)
;
ALTER TABLE `projects_repository` ADD CONSTRAINT `code_ptr_id_refs_id_e7e4320` FOREIGN KEY (`code_ptr_id`) REFERENCES `projects_code` (`id`);

ALTER TABLE `projects_sourcecode` ADD COLUMN `code_ptr_id` integer NOT NULL AFTER `id`;

ALTER TABLE `projects_sourcetotechnolines` ADD COLUMN `code_id` integer NOT NULL AFTER `id`;

INSERT INTO `django_content_type` (`name`, `app_label`, `model`) VALUES ("code", "projects", "code");
INSERT INTO `django_content_type` (`name`, `app_label`, `model`) VALUES ("repository", "projects", "repository");

<!-- 2nd STEP: Copy previous data -->
<!-- manage.py shell -->

from django.db import connection
cursor = connection.cursor()

cursor.execute("SELECT id FROM django_content_type WHERE model='sourcecode'")
sourcecode_model_id = cursor.fetchone()[0]

cursor.execute("SELECT id FROM django_content_type WHERE model='repository'")
repository_model_id = cursor.fetchone()[0]

cursor.execute("SELECT id, project_id, upload_time, exclude_paths, lines_ready FROM projects_sourcecode")
existing_sourcecodes = cursor.fetchall()
for sc in existing_sourcecodes:
	print "insert: ", cursor.execute("INSERT INTO `projects_code` (id, real_type_id, project_id, upload_time, exclude_paths, lines_ready) VALUES (%s, %s, %s, %s, %s, %s)", [sc[0], sourcecode_model_id, sc[1], sc[2], sc[3], sc[4]])
	print "update: ", cursor.execute("UPDATE `projects_sourcecode` SET code_ptr_id=%s WHERE id=%s", [sc[0], sc[0]])

cursor.execute("SELECT id, sourcecode_id FROM projects_sourcetotechnolines")
existing_lines = cursor.fetchall()
for line in existing_lines:
	print "update: ", cursor.execute("UPDATE `projects_sourcetotechnolines` SET code_id=%s WHERE id=%s", [line[1], line[0]])

cursor.close()

from django.db import transaction
transaction.commit()

<!-- At this point: Both systems work for display -->

<!-- 3rd STEP: Add missing constraints        -->
<!--           Previous will not work anymore -->
<!-- MySQL -->

ALTER TABLE `projects_sourcecode` MODIFY `id` integer NOT NULL;
ALTER TABLE `projects_sourcecode` ADD CONSTRAINT `code_ptr_id_refs_id_14a10224` FOREIGN KEY (`code_ptr_id`) REFERENCES `projects_code` (`id`);

ALTER TABLE `projects_sourcetotechnolines` ADD UNIQUE (`code_id`, `technology_id`);
ALTER TABLE `projects_sourcetotechnolines` ADD CONSTRAINT `code_id_refs_id_d2c249ef` FOREIGN KEY (`code_id`) REFERENCES `projects_code` (`id`);

ALTER TABLE `projects_sourcetotechnolines` DROP FOREIGN KEY `sourcecode_id_refs_id_db168e34`;
ALTER IGNORE TABLE `projects_sourcetotechnolines` DROP COLUMN `sourcecode_id`;

ALTER TABLE `projects_sourcecode` DROP PRIMARY KEY;
ALTER TABLE `projects_sourcecode` ADD PRIMARY KEY (`code_ptr_id`);
ALTER TABLE `projects_sourcecode` DROP COLUMN id;
ALTER TABLE `projects_sourcecode` DROP COLUMN upload_time, DROP COLUMN exclude_paths, DROP COLUMN lines_ready;

DROP table projects_sourcetotechnolines;

<!-- 4th STEP: Generate table - projects_sourcetotechnolines -->

manage.py syncdb

<!-- 5th STEP: RE-evaluate lines-count -->
<!-- manage.py shell -->

from projects.models import *
from projects.script_count_lines import count_lines
scs = SourceCode.objects.all()
for sc in scs:
	count_lines(sc, True)