1 | """ |
||
2 | Enarksh |
||
3 | |||
4 | Copyright 2013-2016 Set Based IT Consultancy |
||
5 | |||
6 | Licence MIT |
||
7 | """ |
||
8 | import os |
||
9 | import subprocess |
||
10 | import sys |
||
11 | |||
12 | from pystratum_mysql.StaticDataLayer import StaticDataLayer |
||
13 | |||
14 | from enarksh.C import C |
||
15 | from enarksh.Credentials import Credentials |
||
16 | from enarksh.DataLayer import DataLayer |
||
17 | |||
18 | |||
19 | class Bootstrap: |
||
0 ignored issues
–
show
|
|||
20 | # ------------------------------------------------------------------------------------------------------------------ |
||
21 | @staticmethod |
||
22 | def _drop_routines(): |
||
23 | """ |
||
24 | Drops all stored routines form the databases. |
||
25 | """ |
||
26 | rows = DataLayer.execute_rows('select ROUTINE_TYPE routine_type ' |
||
27 | ', ROUTINE_NAME routine_name ' |
||
28 | 'from information_schema.ROUTINES ' |
||
29 | 'where ROUTINE_SCHEMA = database() ' |
||
30 | 'and ROUTINE_TYPE = "PROCEDURE" ' |
||
31 | 'order by ROUTINE_NAME') |
||
32 | for row in rows: |
||
33 | print('Dropping %s %s' % (row['routine_type'], row['routine_name'])) |
||
34 | DataLayer.execute_none('drop %s %s' % (row['routine_type'], row['routine_name'])) |
||
35 | |||
36 | # ------------------------------------------------------------------------------------------------------------------ |
||
37 | @staticmethod |
||
38 | def _drop_views(): |
||
39 | """ |
||
40 | Drops all views form the databases. |
||
41 | """ |
||
42 | rows = DataLayer.execute_rows('select TABLE_NAME table_name ' |
||
43 | 'from information_schema.TABLES ' |
||
44 | 'where TABLE_SCHEMA = database() ' |
||
45 | 'and TABLE_TYPE = VIEW ' |
||
46 | 'order by table_name') |
||
47 | for row in rows: |
||
48 | print('Dropping view %s' % row['table_name']) |
||
49 | DataLayer.execute_none('drop view %s' % row['table_name']) |
||
50 | |||
51 | # ------------------------------------------------------------------------------------------------------------------ |
||
52 | @staticmethod |
||
53 | def _drop_tables(): |
||
54 | """ |
||
55 | Drops all tables form the databases. |
||
56 | """ |
||
57 | DataLayer.execute_none('set foreign_key_checks = off') |
||
58 | |||
59 | rows = DataLayer.execute_rows('select TABLE_NAME table_name ' |
||
60 | 'from information_schema.TABLES ' |
||
61 | 'where TABLE_SCHEMA = database() ' |
||
62 | 'and TABLE_TYPE = "BASE TABLE" ' |
||
63 | 'order by TABLE_NAME') |
||
64 | for row in rows: |
||
65 | print('Dropping table %s' % row['table_name']) |
||
66 | DataLayer.execute_none('drop table %s cascade' % row['table_name']) |
||
67 | |||
68 | DataLayer.execute_none('set foreign_key_checks = on') |
||
69 | |||
70 | # ------------------------------------------------------------------------------------------------------------------ |
||
71 | @staticmethod |
||
72 | def _execute_sql_file(filename, encoding=None): |
||
73 | """ |
||
74 | Executes a statements in file. |
||
75 | |||
76 | :param filename: The file SQL statements. |
||
77 | :param encoding: The encoding of the file. |
||
78 | """ |
||
79 | print("Executing script '%s'." % filename) |
||
80 | |||
81 | file = open(os.path.join(C.HOME, filename), 'rt', encoding=encoding) |
||
82 | sql = file.read() |
||
83 | file.close() |
||
84 | |||
85 | StaticDataLayer.connection.cmd_query_iter(sql) |
||
86 | |||
87 | # ------------------------------------------------------------------------------------------------------------------ |
||
88 | def _drop_all_db_objects(self): |
||
89 | """ |
||
90 | Drops all objects form the databases. |
||
91 | """ |
||
92 | self._drop_tables() |
||
93 | self._drop_views() |
||
94 | self._drop_routines() |
||
95 | |||
96 | # ------------------------------------------------------------------------------------------------------------------ |
||
97 | @staticmethod |
||
98 | def _load_stored_routines(): |
||
99 | """ |
||
100 | Loads a stored routines and user defined functions into the database. |
||
101 | """ |
||
102 | # Python doesn't flush its stdout and stderr buffers before a subprocess.call (as a consequence the log of |
||
103 | # the subprocess can end up anywhere in the log of this process). So, we have to flush stdout and stderr our |
||
104 | # self. |
||
105 | sys.stdout.flush() |
||
106 | sys.stderr.flush() |
||
107 | |||
108 | ret = subprocess.call(['pystratum', 'stratum', os.path.join(C.HOME, 'etc/stratum.cfg')]) |
||
109 | if ret != 0: |
||
110 | raise RuntimeError('Error loading stored procedures and user defined functions.') |
||
111 | |||
112 | # ------------------------------------------------------------------------------------------------------------------ |
||
113 | def main(self): |
||
114 | """ |
||
115 | Bootstrap the database for our WOB enarksh. Removes all database objects and (re)creates all databases objects. |
||
116 | """ |
||
117 | credentials = Credentials.get() |
||
118 | |||
119 | # Set database configuration options. |
||
120 | DataLayer.config['host'] = credentials.get_host() |
||
121 | DataLayer.config['user'] = credentials.get_user() |
||
122 | DataLayer.config['password'] = credentials.get_password() |
||
123 | DataLayer.config['database'] = credentials.get_database() |
||
124 | DataLayer.config['port'] = credentials.get_port() |
||
125 | DataLayer.config['autocommit'] = False |
||
126 | |||
127 | # Connect to the MySQL. |
||
128 | DataLayer.connect() |
||
129 | |||
130 | # Remove all databases objects. |
||
131 | self._drop_all_db_objects() |
||
132 | |||
133 | # Create all types and tables. |
||
134 | self._execute_sql_file('lib/ddl/0100_create_tables.sql', 'utf-8') |
||
135 | |||
136 | # Load static data. |
||
137 | self._execute_sql_file('lib/ddl/0300_enk_consumption_type.sql') |
||
138 | self._execute_sql_file('lib/ddl/0300_enk_error.sql') |
||
139 | self._execute_sql_file('lib/ddl/0300_enk_host.sql') |
||
140 | self._execute_sql_file('lib/ddl/0300_enk_node_type.sql') |
||
141 | self._execute_sql_file('lib/ddl/0300_enk_port_type.sql') |
||
142 | self._execute_sql_file('lib/ddl/0300_enk_resource_type.sql') |
||
143 | self._execute_sql_file('lib/ddl/0300_enk_run_status.sql') |
||
144 | self._execute_sql_file('lib/ddl/0300_enk_rw_status.sql') |
||
145 | |||
146 | # self._execute_sql_file('lib/ddl/0500_create_views.sql') |
||
147 | |||
148 | # Load all stored procedure and functions. |
||
149 | DataLayer.commit() |
||
150 | self._load_stored_routines() |
||
151 | |||
152 | # ---------------------------------------------------------------------------------------------------------------------- |
||
153 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.