1
0
forked from ScoDoc/ScoDoc

WIP: creation fonctions SQL à l'init

This commit is contained in:
Emmanuel Viennet 2021-08-09 17:01:47 +02:00
parent 6bf4fe3762
commit c1e87c4552
3 changed files with 45 additions and 1 deletions

View File

@ -8,6 +8,8 @@ CODE_STR_LEN = 16 # chaine pour les codes
SHORT_STR_LEN = 32 # courtes chaine, eg acronymes
APO_CODE_STR_LEN = 16 # nb de car max d'un code Apogée
from app.models.raw_sql_init import init_scodoc_database
from app.models.absences import Absence, AbsenceNotification, BilletAbsence
from app.models.entreprises import (
Entreprise,

View File

@ -0,0 +1,40 @@
# -*- coding: UTF-8 -*
"""
Create some Postgresql sequences and functions used by ScoDoc
using raw SQL
"""
from app import db
def init_scodoc_database():
"""Create specific SQL functions and sequences"""
# Important: toujours utiliser IF NOT EXISTS
# car cette fonction peut être appelée plusieurs fois sur la même db
db.session.execute(
"""
CREATE SEQUENCE IF NOT EXISTS notes_idgen_fcod;
CREATE OR REPLACE FUNCTION notes_newid_fcod() RETURNS TEXT
AS $$ SELECT 'FCOD' || to_char(nextval('notes_idgen_fcod'), 'FM999999999'); $$
LANGUAGE SQL;
CREATE OR REPLACE FUNCTION notes_newid_ucod() RETURNS TEXT
AS $$ SELECT 'UCOD' || to_char(nextval('notes_idgen_fcod'), 'FM999999999'); $$
LANGUAGE SQL;
CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$
DECLARE
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tableowner = username AND schemaname = 'public'
AND tablename <> 'notes_semestres'
AND tablename <> 'notes_form_modalites';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
$$ LANGUAGE plpgsql;
"""
)
db.session.commit()

View File

@ -5,6 +5,7 @@ from flask_login import login_user, logout_user, current_user
import app as myapp
from app import db, create_app
from app import models
from app.auth.models import User, Role, Permission
from app.scodoc import sco_bulletins_standard
from app.scodoc import notesdb as ndb
@ -20,13 +21,14 @@ def truncate_database():
def test_client():
# Setup
myapp.Config.TESTING = True
myapp.Config.SQLALCHEMY_DATABASE_URI = "sqlite://"
myapp.Config.SQLALCHEMY_DATABASE_URI = "postgresql://scodoc@localhost/SCOTEST00"
myapp.Config.SERVER_NAME = "test.gr"
apptest = create_app()
# Run tests:
with apptest.test_client() as client:
with apptest.app_context():
with apptest.test_request_context():
models.init_scodoc_database()
db.create_all()
Role.insert_roles()
u = User(user_name="admin")