-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
74 lines (59 loc) · 1.87 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#from main import db
from sys import exception
from psycopg2 import connect, OperationalError, errorcodes, extensions
from config import Config
# from models.asignacion_repuestos import AsignacionRepuestos
# from models.mantenimiento import Mantenimiento
# from models.marca_vehiculo import MarcaVehiculo
# from models.modelo_vehiculo import ModeloVehiculo
# from models.repuesto import Repuesto
# from models.vehiculo import Vehiculo
def crear_base():
base = abrir_conexion('postgres')
cur = base.cursor()
try:
cur.execute(f'DROP DATABASE IF EXISTS {Config.DB}')
cur.execute(f'CREATE DATABASE {Config.DB}')
except OperationalError as err:
if err.pgerror == errorcodes.DUPLICATE_DATABASE:
pass
cur.close()
base.close()
def crear_tablas():
base = abrir_conexion(Config.DB)
cur = base.cursor()
with open('tablas.sql', 'r') as file:
sql = file.read()
try:
cur.execute(sql)
except Exception as e:
print(e)
finally:
cur.close()
base.close()
def abrir_conexion(db_nombre):
base = connect(host=Config.HOST,
port=Config.PORT,
user=Config.USER,
password=Config.PASSWORD,
database=db_nombre) # Config.DB)
base.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
return base
def crearTriggers():
base = abrir_conexion(Config.DB)
cur = base.cursor()
with open('triggers.sql', 'r') as file:
sql = file.read()
try:
cur.execute(sql)
except Exception as e:
print(e)
finally:
cur.close()
base.close()
if __name__ == '__main__':
crear_base() #esto instancia crea la base de datos
crear_tablas() #esto crea las tablas
crearTriggers()
#db.drop_all() #esto borra las tablas
#db.create_all() #esto las crea nuevamente