Refactoring document and image management

This commit is contained in:
Vishwas Babu AJ
2013-06-03 10:33:01 +05:30
parent 9c1f9cd912
commit 15311f6425
51 changed files with 1204 additions and 1074 deletions
@@ -1,28 +0,0 @@
INSERT INTO c_configuration (name,enabled) VALUES ('amazon-S3',0);
CREATE TABLE IF NOT EXISTS m_external_services (
name VARCHAR(150) NOT NULL,
value VARCHAR(250),
UNIQUE(name)
);
INSERT INTO m_external_services (name) VALUES ('s3_bucket_name');
INSERT INTO m_external_services (name) VALUES ('s3_access_key');
INSERT INTO m_external_services (name) VALUES ('s3_secret_key');
ALTER TABLE m_document ADD COLUMN storage_type varchar(50);
CREATE TABLE IF NOT EXISTS `m_image`(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`client_id` BIGINT NOT NULL,
`key` varchar(500),
`storage_type` varchar(50),
FOREIGN KEY (`client_id`) REFERENCES m_client(`id`),
PRIMARY KEY (`id`),
UNIQUE(client_id)
);
UPDATE m_document set storage_type='file_system';
insert into m_image (`client_id`,`key`,`storage_type`) select id,`image_key`,'file_system' from m_client where `image_key` IS NOT NULL;
+70
View File
@@ -0,0 +1,70 @@
INSERT INTO c_configuration (name,enabled) VALUES ('amazon-S3',0);
/*New table for storing details of external services used*/
CREATE TABLE `c_external_service` (
`name` VARCHAR(150) NOT NULL,
`value` VARCHAR(250) NULL DEFAULT NULL,
UNIQUE INDEX `name` (`name`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
INSERT INTO c_external_service (name) VALUES ('s3_bucket_name');
INSERT INTO c_external_service (name) VALUES ('s3_access_key');
INSERT INTO c_external_service (name) VALUES ('s3_secret_key');
/*Image tables stores details of all images*/
CREATE TABLE IF NOT EXISTS `m_image`(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`location` varchar(500),
`storage_type_enum` SMALLINT(5),
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
/*Client points to image table*/
ALTER TABLE `m_client`
ADD COLUMN `image_id` BIGINT(20) NULL DEFAULT NULL AFTER `display_name`,
ADD CONSTRAINT `FK_m_client_m_image` FOREIGN KEY (`image_id`) REFERENCES `m_image` (`id`);
/*Move existing image locations to new table*/
drop procedure if exists migrate_customer_image_data;
delimiter #
create procedure migrate_customer_image_data()
begin
declare v_counter int unsigned default 0;
declare num_of_clients INT DEFAULT 0;
declare curr_client INT DEFAULT 0;
declare curr_image INT DEFAULT 0;
declare prev_image INT DEFAULT 0;
SELECT COUNT(*) FROM m_client INTO num_of_clients;
while v_counter < num_of_clients do
INSERT INTO m_image(`location`,`storage_type_enum`) select image_key, 1 FROM m_client where image_key is not null LIMIT v_counter,1;
select ifnull(max(`id`),0) from m_image INTO curr_image;
select id FROM m_client where image_key is not null limit v_counter,1 INTO curr_client;
IF (prev_image != curr_image) THEN UPDATE m_client set image_id = curr_image where id=curr_client;
END IF;
set prev_image=curr_image;
set v_counter=v_counter+1;
end while;
delete from m_image where `location` is null;
end#
delimiter ;
call migrate_customer_image_data();
/*Now drop the procedure*/
drop procedure if exists migrate_customer_image_data;
/*Delete image key from client*/
ALTER TABLE `m_client` DROP COLUMN `image_key`;
/*Add storage type for m_document table and update existing documents to file storage*/
ALTER TABLE m_document ADD COLUMN storage_type_enum SMALLINT(5);
UPDATE m_document set storage_type_enum=1;