1
|
1 |
|
from configparser import ConfigParser |
2
|
1 |
|
from typing import Dict, Optional |
3
|
|
|
|
4
|
1 |
|
from pystratum_backend.StratumStyle import StratumStyle |
5
|
1 |
|
from pystratum_common.backend.CommonRoutineLoaderWorker import CommonRoutineLoaderWorker |
6
|
|
|
|
7
|
1 |
|
from pystratum_pgsql.backend.PgSqlWorker import PgSqlWorker |
8
|
1 |
|
from pystratum_pgsql.helper.PgSqlRoutineLoaderHelper import PgSqlRoutineLoaderHelper |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
class PgSqlRoutineLoaderWorker(PgSqlWorker, CommonRoutineLoaderWorker): |
12
|
|
|
""" |
13
|
|
|
Class for loading stored routines into a PostgreSQL instance from (pseudo) SQL files. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
1 |
|
def __init__(self, io: StratumStyle, config: ConfigParser): |
18
|
|
|
""" |
19
|
|
|
Object constructor. |
20
|
|
|
|
21
|
|
|
:param PyStratumStyle io: The output decorator. |
22
|
|
|
""" |
23
|
1 |
|
PgSqlWorker.__init__(self, io, config) |
24
|
1 |
|
CommonRoutineLoaderWorker.__init__(self, io, config) |
25
|
|
|
|
26
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
27
|
1 |
|
def _get_column_type(self) -> None: |
28
|
|
|
""" |
29
|
|
|
Selects schema, table, column names and the column type from current database and information_schema and saves |
30
|
|
|
them as replace pairs. |
31
|
|
|
""" |
32
|
1 |
|
rows = self._dl.get_all_table_columns() |
33
|
1 |
|
for row in rows: |
34
|
1 |
|
key = '@' + row['table_name'] + '.' + row['column_name'] + '%type@' |
35
|
1 |
|
key = key.lower() |
36
|
1 |
|
value = row['data_type'] |
37
|
|
|
|
38
|
1 |
|
self._replace_pairs[key] = value |
39
|
|
|
|
40
|
1 |
|
self._io.text('Selected {0} column types for substitution'.format(len(rows))) |
41
|
|
|
|
42
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
43
|
1 |
|
def create_routine_loader_helper(self, |
44
|
|
|
routine_name: str, |
45
|
|
|
pystratum_old_metadata: Optional[Dict], |
46
|
|
|
rdbms_old_metadata: Optional[Dict]) -> PgSqlRoutineLoaderHelper: |
47
|
|
|
""" |
48
|
|
|
Creates a Routine Loader Helper object. |
49
|
|
|
|
50
|
|
|
:param str routine_name: The name of the routine. |
51
|
|
|
:param dict pystratum_old_metadata: The old metadata of the stored routine from PyStratum. |
52
|
|
|
:param dict rdbms_old_metadata: The old metadata of the stored routine from PostgreSQL. |
53
|
|
|
|
54
|
|
|
:rtype: PgSqlRoutineLoaderHelper |
55
|
|
|
""" |
56
|
1 |
|
return PgSqlRoutineLoaderHelper(self._io, |
57
|
|
|
self._dl, |
58
|
|
|
self._source_file_names[routine_name], |
59
|
|
|
self._source_file_encoding, |
60
|
|
|
pystratum_old_metadata, |
61
|
|
|
self._replace_pairs, |
62
|
|
|
rdbms_old_metadata) |
63
|
|
|
|
64
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
65
|
1 |
|
def _get_old_stored_routine_info(self) -> None: |
66
|
|
|
""" |
67
|
|
|
Retrieves information about all stored routines in the current schema. |
68
|
|
|
""" |
69
|
1 |
|
rows = self._dl.get_routines() |
70
|
1 |
|
self._rdbms_old_metadata = {} |
71
|
1 |
|
for row in rows: |
72
|
|
|
self._rdbms_old_metadata[row['routine_name']] = row |
73
|
|
|
|
74
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
75
|
1 |
|
def _drop_obsolete_routines(self) -> None: |
76
|
|
|
""" |
77
|
|
|
Drops obsolete stored routines (i.e. stored routines that exists in the current schema but for |
78
|
|
|
which we don't have a source file). |
79
|
|
|
""" |
80
|
1 |
|
for routine_name, values in self._rdbms_old_metadata.items(): |
81
|
|
|
if routine_name not in self._source_file_names: |
82
|
|
|
self._io.text("Dropping {0} <dbo>{1}</dbo>".format(values['routine_type'], routine_name)) |
83
|
|
|
self._dl.drop_stored_routine(values['routine_type'], routine_name, values['routine_args']) |
84
|
|
|
|
85
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
86
|
|
|
|