forked from ScoDoc/ScoDoc
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
|
# -*- 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()
|