From c1e87c455266afe2b759489acf41fe43f41ab307 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 9 Aug 2021 17:01:47 +0200 Subject: [PATCH] =?UTF-8?q?WIP:=20creation=20fonctions=20SQL=20=C3=A0=20l'?= =?UTF-8?q?init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/__init__.py | 2 ++ app/models/raw_sql_init.py | 40 ++++++++++++++++++++++++++++++++++++++ tests/conftest.py | 4 +++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/models/raw_sql_init.py diff --git a/app/models/__init__.py b/app/models/__init__.py index e4db11299d..3514f31067 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -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, diff --git a/app/models/raw_sql_init.py b/app/models/raw_sql_init.py new file mode 100644 index 0000000000..c06cc544fc --- /dev/null +++ b/app/models/raw_sql_init.py @@ -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() diff --git a/tests/conftest.py b/tests/conftest.py index 758250f4da..337b5afbb3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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")