[DEV] migration db done

This commit is contained in:
Edouard DUPIN 2020-02-20 22:52:58 +01:00
parent 64713d2ed4
commit 1e90972c04
10 changed files with 69 additions and 133 deletions

View File

@ -42,19 +42,6 @@ tmp_value = 0
#curl -F 'file=@Totally_Spies.mp4;type=application/octet-stream' -H 'transfer-encoding:chunked' 127.0.0.1:15080/data -X POST -O; echo ;
# Create table
bdd_structure = '''
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME NOT NULL,
sha512 TEXT NOT NULL,
mime_type TEXT NOT NULL,
size BIGINT NOT NULL,
original_name TEXT)
''')
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)
"""
@ -136,7 +123,7 @@ def add(_app, _name_api):
if not os.path.exists(basic_data_path):
os.makedirs(basic_data_path)
destination_filename = os.path.join(basic_data_path, "video")
destination_filename = os.path.join(basic_data_path, "data")
"""
if os.path.isfile(destination_filename) == True:
answer_data = {
@ -177,7 +164,7 @@ def add(_app, _name_api):
if id[-4:] == ".mkv":
id = id[:-4]
"""
filename = os.path.join(_app.config['REST_MEDIA_DATA'], str(id), "video")
filename = os.path.join(_app.config['REST_MEDIA_DATA'], str(id), "data")
value = data_global_elements.get_interface(_name_api).get(id)
debug.info("plouuuuuuf " + str(value))
headers = {

View File

@ -30,17 +30,6 @@ import data_interface
import data_global_elements
# Create table
bdd_structure = '''
CREATE TABLE grp (
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME NOT NULL,
name TEXT NOT NULL,
description TEXT,
covers INTEGER[] REFERENCES data(id))
''')
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)

View File

@ -30,19 +30,6 @@ import data_interface
import data_global_elements
# Create table
bdd_structure = '''
CREATE TABLE saison (
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME NOT NULL,
number INTEGER NOT NULL,
description TEXT,
group_id INTEGER REFERENCES grp(id),
covers INTEGER[] REFERENCES data(id))
'''
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)

View File

@ -30,17 +30,6 @@ import data_interface
import data_global_elements
# Create table
bdd_structure = '''
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME NOT NULL,
name TEXT NOT NULL,
description TEXT,
covers INTEGER[] REFERENCES data(id))
'''
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)

View File

@ -29,18 +29,6 @@ import tools
import data_interface
import data_global_elements
# Create table
bdd_structure = '''
CREATE TABLE univers (
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME NOT NULL,
name TEXT NOT NULL,
description TEXT,
covers INTEGER[] REFERENCES data(id))
'''
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)

View File

@ -31,25 +31,6 @@ import data_interface
import data_global_elements
# Create table
bdd_structure = '''
CREATE TABLE video (
id INTEGER PRIMARY KEY,
deleted BOOLEAN,
create_date DATETIME NOT NULL,
modify_date DATETIME 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 generate_name(_value):
group_name = ""

View File

@ -112,19 +112,16 @@ default_values_type = [
]
def add_interface(_name, _default_value = None):
interface = data_interface.DataInterface(_name, os.path.join(tools.get_run_path(), app.config['REST_DATA'], "bdd_" + _name + ".db3"))
if _default_value != None:
if interface.count() == 0:
interface.reset_with_value(_default_value);
def add_interface(_name, _base_name):
interface = data_interface.DataInterface(_name, _base_name)
data_global_elements.add_interface(_name, interface)
add_interface(data_global_elements.API_DATA)
add_interface(data_global_elements.API_TYPE, default_values_type)
add_interface(data_global_elements.API_UNIVERS)
add_interface(data_global_elements.API_GROUP)
add_interface(data_global_elements.API_SAISON)
add_interface(data_global_elements.API_VIDEO)
add_interface(data_global_elements.API_DATA, data_global_elements.API_DATA)
add_interface(data_global_elements.API_TYPE, data_global_elements.API_TYPE)
add_interface(data_global_elements.API_UNIVERS, data_global_elements.API_UNIVERS)
add_interface(data_global_elements.API_GROUP, "grp")
add_interface(data_global_elements.API_SAISON, data_global_elements.API_SAISON)
add_interface(data_global_elements.API_VIDEO, data_global_elements.API_VIDEO)
import api.root as api_root
api_root.add(app)

View File

@ -14,50 +14,34 @@ from realog import debug
import random
import copy
from sanic.exceptions import ServerError
import sqlite3
from psycopg2.extras import RealDictCursor
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
if col[0] == "covers":
if row[idx] == "":
d[col[0]] = None
elif row[idx] != None:
d[col[0]] = row[idx].split("/")
else:
d[col[0]] = None
else:
d[col[0]] = row[idx]
return d
import db
##
## @breif Generic interface to access to the BDD (no BDD, direct file IO)
##
class DataInterface():
def __init__(self, _name, _file):
def __init__(self, _name, _base_name):
self.model = None
self.name = _name
self.file = _file
self.bdd = []
self.base_name = _base_name
self.connection = db.connect_bdd();
self.need_save = False
self.last_id = 0
if tools.exist(self.file) == False:
self.mark_to_store()
else:
self.conn = sqlite3.connect(self.file)
self.conn.row_factory = dict_factory
#self.cursor = self.conn.cursor()
##self.upgrade_global_bdd_id();
#self.conn = self.connection.cursor()
def __del__(self):
self.connection.commit()
self.connection.close()
def set_data_model(self, _data_model):
self.model = _data_model
def reset_with_value(self, _data):
self.bdd = _data
self.last_id = 0
self.mark_to_store()
##self.upgrade_global_bdd_id();
#self.bdd = _data
#self.last_id = 0
#self.mark_to_store()
pass
def check_with_model(self, _data):
return True
@ -105,6 +89,7 @@ class DataInterface():
return False
return True
"""
pass
def upgrade_global_bdd_id(self):
"""
@ -118,6 +103,7 @@ class DataInterface():
if self.last_id == 0:
self.last_id = random.randint(20, 100)
"""
pass
def get_table_index(self, _id):
"""
@ -129,6 +115,7 @@ class DataInterface():
id_in_bdd += 1
return None
"""
pass
##
## @brief Mark the current BDD to store all in File system (sync)
@ -144,12 +131,12 @@ class DataInterface():
if self.need_save == False:
return
debug.warning("Save bdd: " + self.file)
self.conn.commit()
self.connection.commit()
def gets(self, filter=None):
debug.info("gets " + self.name)
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM data WHERE deleted=0')
cursor = self.connection.cursor(cursor_factory=RealDictCursor)
cursor.execute('SELECT * FROM ' + self.base_name + ' WHERE deleted = false')
results = cursor.fetchall()
#debug.info("gets data = " + json.dumps(results, indent=4))
if filter == None:
@ -169,12 +156,12 @@ class DataInterface():
if type(_id) != int:
debug.warning("get wrong input type...")
debug.info("get " + self.name + ": " + str(_id))
cursor = self.conn.cursor()
cursor = self.connection.cursor(cursor_factory=RealDictCursor)
#cursor.execute('SELECT * FROM data WHERE deleted=0')
#results = cursor.fetchall()
#debug.info("display data = " + json.dumps(results, indent=4))
req = (_id,)
cursor.execute('SELECT * FROM data WHERE deleted=0 AND id=?', req)
cursor.execute('SELECT * FROM ' + self.base_name + ' WHERE deleted=false AND id=%s', req)
results = cursor.fetchone()
#debug.info("get specific data = " + json.dumps(results))
return results;
@ -196,16 +183,16 @@ class DataInterface():
def delete(self, _id):
debug.info("delete " + self.name + ": " + str(_id))
cursor = self.conn.cursor()
cursor = self.connection.cursor()
req = (_id,)
cursor.execute('UPDATE data SET deleted=1 WHERE id=?', req)
cursor.execute('UPDATE ' + self.base_name + ' SET deleted=true WHERE id=%s', req)
self.mark_to_store();
return True
def put(self, _id, _value):
debug.info("put in " + self.name + ": " + str(_id))
cursor = self.conn.cursor()
request = 'UPDATE data SET'
cursor = self.connection.cursor()
request = 'UPDATE ' + self.base_name + ' SET'
list_data = []
first = True;
for elem in _value.keys():
@ -216,8 +203,8 @@ class DataInterface():
else:
request += " , "
list_data.append(_value[elem])
request += " '" + elem + "' = ?"
request += " WHERE id = ? "
request += " '" + elem + "' = %s"
request += " WHERE id = %s "
list_data.append(_id)
debug.info("Request executed : '" + request + "'")
cursor.execute(request, list_data)

12
back/src/db.py Normal file
View File

@ -0,0 +1,12 @@
from realog import debug
import psycopg2
def connect_bdd():
debug.info("connect BDD: ")
conn = psycopg2.connect(dbname="karideo", user="root", password="postgress_password", host="localhost", port="15032")
return conn
base_bdd_name = "karideo_"

View File

@ -14,6 +14,7 @@ import json
import os
import random
import copy
import shutil
from dateutil import parser
import db
@ -27,6 +28,22 @@ def file_read_data(path):
return data_file
def create_directory_of_file(file):
debug.info("Create directory of path: '" + file + "'")
path = os.path.dirname(file)
debug.info("Create directory: '" + path + "'")
try:
os.stat(path)
except:
os.makedirs(path)
def file_move(path_src, path_dst):
#real write of data:
create_directory_of_file(path_dst)
shutil.move(path_src, path_dst)
return True
def transfert_db():
out = {}
out[str(None)] = None
@ -59,6 +76,8 @@ def transfert_db():
id_of_new_row = c.fetchone()[0]
debug.info("data transform: " + str(id) + " => " + str(id_of_new_row))
out[str(id)] = id_of_new_row
file_move("media/" + str(id) + "/video", "media2/" + str(id_of_new_row) + "/data")
file_move("media/" + str(id) + "/meta.json", "media2/" + str(id_of_new_row) + "/meta.json")
# Save (commit) the changes
connection.commit()