diff --git a/back/transfer_bdd/v0.0...v1.0/create_bdd.py b/back/transfer_bdd/v0.0...v1.0/create_bdd.py new file mode 100755 index 0000000..9bf8100 --- /dev/null +++ b/back/transfer_bdd/v0.0...v1.0/create_bdd.py @@ -0,0 +1,148 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +## +## @author Edouard DUPIN +## +## @copyright 2012, Edouard DUPIN, all right reserved +## +## @license MPL v2.0 (see license file) +## +#pip install paho-mqtt --user + +from realog import debug +import json +import os +import random +import copy +from dateutil import parser + +import db +connection = db.connect_bdd(); + +debug.info("create the table:") + +c = connection.cursor() + +# Create table +c.execute(''' +CREATE OR REPLACE FUNCTION trigger_set_timestamp() +RETURNS TRIGGER AS $$ +BEGIN + NEW.modify_date = NOW(); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; +''') +connection.commit() + + +# Create table +c.execute(''' +CREATE TABLE object ( + id SERIAL PRIMARY KEY, + deleted BOOLEAN NOT NULL DEFAULT false, + create_date TIMESTAMPTZ NOT NULL DEFAULT NOW(), + modify_date TIMESTAMPTZ NOT NULL DEFAULT NOW()); +''') +connection.commit() + +c.execute(''' +CREATE TRIGGER set_timestamp +BEFORE UPDATE ON object +FOR EACH ROW +EXECUTE PROCEDURE trigger_set_timestamp(); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE data( + sha512 VARCHAR(129) NOT NULL, + mime_type VARCHAR(50) NOT NULL, + size BIGINT NOT NULL, + original_name TEXT + ) INHERITS (object) +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE node ( + name TEXT NOT NULL, + description TEXT + ) INHERITS (object); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE cover_link ( + id SERIAL PRIMARY KEY, + deleted BOOLEAN NOT NULL DEFAULT false, + node_id INTEGER REFERENCES object(id), + data_id INTEGER REFERENCES object(id) + ); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE grp () INHERITS (node); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE saison ( + group_id INTEGER REFERENCES object(id) + ) INHERITS (node); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE type () INHERITS (node); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE univers () INHERITS (node); +''') +connection.commit() + +# Create table +c.execute(''' +CREATE TABLE video ( + data_id INTEGER REFERENCES object(id), + type_id INTEGER REFERENCES object(id), + univers_id INTEGER REFERENCES object(id), + group_id INTEGER REFERENCES object(id), + saison_id INTEGER REFERENCES object(id), + episode INTEGER, + date INTEGER, -- simple date in years of the creation of the media + time INTEGER -- Time in second of the media + ) INHERITS (node); +''') + +# Save (commit) the changes +connection.commit() + +# We can also close the connection if we are done with it. +# Just be sure any changes have been committed or they will be lost. +connection.close() + +print(" =================================================== Send DATA "); +import transfert_data +print(" =================================================== Send TYPE "); +import transfert_type +print(" =================================================== Send GROUP "); +import transfert_group +print(" =================================================== Send SAISON "); +import transfert_saison +print(" =================================================== Send UNIVERS "); +import transfert_univers +print(" =================================================== Send VIDEO "); +import transfert_video + + diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_data.py b/back/transfer_bdd/v0.0...v1.0/transfert_data.py index 3570973..99e06c4 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_data.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_data.py @@ -36,24 +36,11 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE data( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - sha512 TEXT NOT NULL, - mime_type TEXT NOT NULL, - size BIGINT NOT NULL, - original_name TEXT) -''') - debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] time_create = elem["create_date"]; mime_type = elem["mime_type"] @@ -61,7 +48,7 @@ for elem in my_old_bdd: sha512 = elem["sha512"] size = elem["size"] request_insert = (id, time_create, sha512, mime_type, size, original_name) - c.execute('INSERT INTO data VALUES (%s,false,%s,CURRENT_TIMESTAMP,%s,%s,%s,%s)', request_insert) + c.execute('INSERT INTO data (id, create_date, sha512, mime_type, size, original_name) VALUES (%s,%s,%s,%s,%s,%s)', request_insert) # Save (commit) the changes connection.commit() diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_group.py b/back/transfer_bdd/v0.0...v1.0/transfert_group.py index f383fc6..93e4ba3 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_group.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_group.py @@ -40,33 +40,12 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE grp ( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - name TEXT NOT NULL, - description TEXT, - covers INTEGER[] REFERENCES data(id)) -''') - -def list_to_string(data): - out = ""; - for elem in data: - if out != "": - out += "/" - out +=str(elem) - return out - -#sqlite3 bdd_group.db3 "SELECT * from data" debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] name = elem["name"] if "description" not in elem.keys(): @@ -79,8 +58,14 @@ for elem in my_old_bdd: covers = elem["covers"] if covers == None: covers = []; - request_insert = (id, name, description, covers) - c.execute('INSERT INTO grp VALUES (%s,false,now,now,%s,%s,%s)', request_insert) + request_insert = (id, name, description) + c.execute('INSERT INTO grp (id, name, description) VALUES (%s,%s,%s)', request_insert) + connection.commit() + for elem_cover in covers: + request_insert = (id, elem_cover) + print(" insert cover " + str(request_insert)) + c.execute('INSERT INTO cover_link (node_id, data_id) VALUES (%s,%s)', request_insert) + connection.commit() # Save (commit) the changes connection.commit() diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_saison.py b/back/transfer_bdd/v0.0...v1.0/transfert_saison.py index 720ca2d..fbf1ca2 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_saison.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_saison.py @@ -38,34 +38,11 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE saison ( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - number INTEGER NOT NULL, - description TEXT, - group_id INTEGER REFERENCES grp(id), - covers INTEGER[] REFERENCES data(id)) -''') - -def list_to_string(data): - out = ""; - for elem in data: - if out != "": - out += "/" - out +=str(elem) - return out - -#sqlite3 bdd_group.db3 "SELECT * from data" - debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] name = elem["number"] if "group_id" not in elem.keys(): @@ -82,8 +59,14 @@ for elem in my_old_bdd: covers = elem["covers"] if covers == None: covers = []; - request_insert = (id, name, description, group_id, covers) - c.execute('INSERT INTO saison VALUES (%s,false,now,now,%s,%s,%s,%s)', request_insert) + request_insert = (id, name, description, group_id) + c.execute('INSERT INTO saison (id, name, description, group_id) VALUES (%s,%s,%s,%s)', request_insert) + connection.commit() + for elem_cover in covers: + request_insert = (id, elem_cover) + print(" insert cover " + str(request_insert)) + c.execute('INSERT INTO cover_link (node_id, data_id) VALUES (%s,%s)', request_insert) + connection.commit() # Save (commit) the changes connection.commit() diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_type.py b/back/transfer_bdd/v0.0...v1.0/transfert_type.py index 53ffc69..b079c22 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_type.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_type.py @@ -38,33 +38,11 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE type ( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - name TEXT NOT NULL, - description TEXT, - covers INTEGER[] REFERENCES data(id)) -''') - -def list_to_string(data): - out = ""; - for elem in data: - if out != "": - out += "/" - out +=str(elem) - return out - -#sqlite3 bdd_group.db3 "SELECT * from data" - debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] name = elem["name"] if "description" not in elem.keys(): @@ -77,8 +55,14 @@ for elem in my_old_bdd: covers = elem["covers"] if covers == None: covers = []; - request_insert = (id, name, description, covers) - c.execute('INSERT INTO type VALUES (%s,false,now,now,%s,%s,%s)', request_insert) + request_insert = (id, name, description) + c.execute('INSERT INTO type (id, name, description) VALUES (%s,%s,%s)', request_insert) + connection.commit() + for elem_cover in covers: + request_insert = (id, elem_cover) + print(" insert cover " + str(request_insert)) + c.execute('INSERT INTO cover_link (node_id, data_id) VALUES (%s,%s)', request_insert) + connection.commit() # Save (commit) the changes connection.commit() diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_univers.py b/back/transfer_bdd/v0.0...v1.0/transfert_univers.py index 4ea173a..c78bd31 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_univers.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_univers.py @@ -38,33 +38,11 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE univers ( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - name TEXT NOT NULL, - description TEXT, - covers INTEGER[] REFERENCES data(id)) -''') - -def list_to_string(data): - out = ""; - for elem in data: - if out != "": - out += "/" - out +=str(elem) - return out - -#sqlite3 bdd_group.db3 "SELECT * from data" - debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] name = elem["name"] if "description" not in elem.keys(): @@ -77,8 +55,14 @@ for elem in my_old_bdd: covers = elem["covers"] if covers == None: covers = []; - request_insert = (id, name, description, covers) - c.execute('INSERT INTO univers VALUES (%s,false,now,now,%s,%s,%s)', request_insert) + request_insert = (id, name, description) + c.execute('INSERT INTO univers (id, name, description) VALUES (%s,%s,%s)', request_insert) + connection.commit() + for elem_cover in covers: + request_insert = (id, elem_cover) + print(" insert cover " + str(request_insert)) + c.execute('INSERT INTO cover_link (node_id, data_id) VALUES (%s,%s)', request_insert) + connection.commit() # Save (commit) the changes connection.commit() diff --git a/back/transfer_bdd/v0.0...v1.0/transfert_video.py b/back/transfer_bdd/v0.0...v1.0/transfert_video.py index 70e16a8..47b4afd 100755 --- a/back/transfer_bdd/v0.0...v1.0/transfert_video.py +++ b/back/transfer_bdd/v0.0...v1.0/transfert_video.py @@ -38,40 +38,12 @@ debug.info("create the table:") c = connection.cursor() -# Create table -c.execute(''' -CREATE TABLE video ( - id SERIAL PRIMARY KEY, - deleted BOOLEAN, - create_date TIMESTAMPTZ NOT NULL, - modify_date TIMESTAMPTZ NOT NULL, - name TEXT NOT NULL, - description TEXT, - covers INTEGER[] REFERENCES data(id), - data_id INTEGER REFERENCES data(id), - type_id INTEGER REFERENCES type(id), - univers_id INTEGER REFERENCES univers(id), - group_id INTEGER REFERENCES grp(id), - saison_id INTEGER REFERENCES saison(id), - episode INTEGER, - time INTEGER) -''') - -def list_to_string(data): - out = ""; - for elem in data: - if out != "": - out += "/" - out +=str(elem) - return out - -#sqlite3 bdd_group.db3 "SELECT * from data" debug.info("insert elements: ") iii = 0; for elem in my_old_bdd: iii+=1; - debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element") + debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element " + str(elem["id"])) id = elem["id"] time_create = elem["create_date"]; name = elem["name"] @@ -117,8 +89,15 @@ for elem in my_old_bdd: time = None else: time = elem["time"] - request_insert = (id, time_create, name, description, covers, data_id, type_id, univers_id, group_id, saison_id, date, episode, time) - c.execute('INSERT INTO video VALUES (%s,false,%s,now,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', request_insert) + request_insert = (id, time_create, name, description, data_id, type_id, univers_id, group_id, saison_id, date, episode, time) + c.execute('INSERT INTO video (id, create_date, name, description, data_id, type_id, univers_id, group_id, saison_id, date, episode, time) VALUES (%s,false,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', request_insert) + + connection.commit() + for elem_cover in covers: + request_insert = (id, elem_cover) + print(" insert cover " + str(request_insert)) + c.execute('INSERT INTO cover_link (node_id, data_id) VALUES (%s,%s)', request_insert) + connection.commit() # Save (commit) the changes connection.commit()