[DEV] try heritage in posgresql ==> maybe not a good idea...

This commit is contained in:
Edouard DUPIN 2020-02-19 00:00:00 +01:00
parent 6b678eed88
commit d0790c7f1b
7 changed files with 196 additions and 146 deletions

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()