clickhouse-sqlalchemy-ng 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clickhouse_sqlalchemy/__init__.py +15 -0
- clickhouse_sqlalchemy/alembic/__init__.py +0 -0
- clickhouse_sqlalchemy/alembic/comparators.py +208 -0
- clickhouse_sqlalchemy/alembic/dialect.py +92 -0
- clickhouse_sqlalchemy/alembic/operations.py +170 -0
- clickhouse_sqlalchemy/alembic/renderers.py +114 -0
- clickhouse_sqlalchemy/alembic/toimpl.py +79 -0
- clickhouse_sqlalchemy/drivers/__init__.py +4 -0
- clickhouse_sqlalchemy/drivers/asynch/__init__.py +0 -0
- clickhouse_sqlalchemy/drivers/asynch/base.py +74 -0
- clickhouse_sqlalchemy/drivers/asynch/connector.py +206 -0
- clickhouse_sqlalchemy/drivers/base.py +578 -0
- clickhouse_sqlalchemy/drivers/compilers/__init__.py +0 -0
- clickhouse_sqlalchemy/drivers/compilers/ddlcompiler.py +305 -0
- clickhouse_sqlalchemy/drivers/compilers/sqlcompiler.py +525 -0
- clickhouse_sqlalchemy/drivers/compilers/typecompiler.py +181 -0
- clickhouse_sqlalchemy/drivers/http/__init__.py +0 -0
- clickhouse_sqlalchemy/drivers/http/base.py +61 -0
- clickhouse_sqlalchemy/drivers/http/connector.py +256 -0
- clickhouse_sqlalchemy/drivers/http/escaper.py +85 -0
- clickhouse_sqlalchemy/drivers/http/exceptions.py +3 -0
- clickhouse_sqlalchemy/drivers/http/transport.py +180 -0
- clickhouse_sqlalchemy/drivers/http/utils.py +14 -0
- clickhouse_sqlalchemy/drivers/native/__init__.py +0 -0
- clickhouse_sqlalchemy/drivers/native/base.py +88 -0
- clickhouse_sqlalchemy/drivers/native/connector.py +271 -0
- clickhouse_sqlalchemy/drivers/reflection.py +53 -0
- clickhouse_sqlalchemy/drivers/util.py +45 -0
- clickhouse_sqlalchemy/engines/__init__.py +40 -0
- clickhouse_sqlalchemy/engines/base.py +66 -0
- clickhouse_sqlalchemy/engines/mergetree.py +225 -0
- clickhouse_sqlalchemy/engines/misc.py +150 -0
- clickhouse_sqlalchemy/engines/replicated.py +176 -0
- clickhouse_sqlalchemy/engines/util.py +52 -0
- clickhouse_sqlalchemy/exceptions.py +9 -0
- clickhouse_sqlalchemy/ext/__init__.py +0 -0
- clickhouse_sqlalchemy/ext/clauses.py +65 -0
- clickhouse_sqlalchemy/ext/declarative.py +46 -0
- clickhouse_sqlalchemy/orm/__init__.py +5 -0
- clickhouse_sqlalchemy/orm/query.py +136 -0
- clickhouse_sqlalchemy/orm/session.py +14 -0
- clickhouse_sqlalchemy/sql/__init__.py +6 -0
- clickhouse_sqlalchemy/sql/ddl.py +80 -0
- clickhouse_sqlalchemy/sql/functions.py +61 -0
- clickhouse_sqlalchemy/sql/schema.py +129 -0
- clickhouse_sqlalchemy/sql/selectable.py +84 -0
- clickhouse_sqlalchemy/types/__init__.py +79 -0
- clickhouse_sqlalchemy/types/common.py +256 -0
- clickhouse_sqlalchemy/types/ip.py +155 -0
- clickhouse_sqlalchemy/types/nested.py +64 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/METADATA +170 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/RECORD +56 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/WHEEL +5 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/entry_points.txt +5 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/licenses/LICENSE +21 -0
- clickhouse_sqlalchemy_ng-0.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
from .ext.declarative import get_declarative_base
|
|
3
|
+
from .orm.session import make_session
|
|
4
|
+
from .sql import Table, MaterializedView, select
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
VERSION = (0, 4, 0)
|
|
8
|
+
__version__ = '.'.join(str(x) for x in VERSION)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
__all__ = (
|
|
12
|
+
'get_declarative_base',
|
|
13
|
+
'make_session',
|
|
14
|
+
'Table', 'MaterializedView', 'select'
|
|
15
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from alembic import __version__ as alembic_version
|
|
4
|
+
from alembic.autogenerate import comparators
|
|
5
|
+
from alembic.operations.ops import ModifyTableOps
|
|
6
|
+
from sqlalchemy import schema as sa_schema
|
|
7
|
+
from sqlalchemy import text
|
|
8
|
+
|
|
9
|
+
from clickhouse_sqlalchemy.sql.schema import Table
|
|
10
|
+
from . import operations
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
alembic_version = tuple(
|
|
15
|
+
(int(x) if x.isdigit() else x) for x in alembic_version.split('.')
|
|
16
|
+
)
|
|
17
|
+
_alembic_118 = alembic_version >= (1, 18, 0)
|
|
18
|
+
|
|
19
|
+
if _alembic_118:
|
|
20
|
+
from alembic.autogenerate.compare.tables import _compare_columns
|
|
21
|
+
else:
|
|
22
|
+
from alembic.autogenerate.compare import _compare_columns
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _extract_to_table_name(create_table_query):
|
|
26
|
+
query = create_table_query
|
|
27
|
+
# Naive inner name detection
|
|
28
|
+
brace = query.index('(')
|
|
29
|
+
inner_name = query[query.index(' TO ', 0, brace) + 4:brace - 1].strip(' `')
|
|
30
|
+
return inner_name.split('.')[1] if '.' in inner_name else inner_name
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Direct call .dispatch_for('schema', 'clickhouse') override an Alembic
|
|
34
|
+
# default ('schema', 'default') comparator. To avoid it (as we have own
|
|
35
|
+
# implementation only for a materialized views) register default "schema"
|
|
36
|
+
# comparators as "clickhouse" comparators too.
|
|
37
|
+
if _alembic_118:
|
|
38
|
+
for (target, qualifier, priority), fns in comparators._registry.items():
|
|
39
|
+
if target == 'schema' and qualifier == 'default':
|
|
40
|
+
for fn, _ in fns:
|
|
41
|
+
comparators.dispatch_for(
|
|
42
|
+
'schema', qualifier='clickhouse', priority=priority
|
|
43
|
+
)(fn)
|
|
44
|
+
else:
|
|
45
|
+
for fn in comparators._registry[('schema', 'default')]:
|
|
46
|
+
comparators.dispatch_for('schema', qualifier='clickhouse')(fn)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@comparators.dispatch_for('schema', qualifier='clickhouse')
|
|
50
|
+
def compare_mat_view(autogen_context, upgrade_ops, schemas):
|
|
51
|
+
connection = autogen_context.connection
|
|
52
|
+
dialect = autogen_context.dialect
|
|
53
|
+
metadata = autogen_context.metadata
|
|
54
|
+
|
|
55
|
+
database_engine = dialect._execute(
|
|
56
|
+
connection,
|
|
57
|
+
text(
|
|
58
|
+
'SELECT engine '
|
|
59
|
+
'FROM system.databases '
|
|
60
|
+
'WHERE database = currentDatabase()'
|
|
61
|
+
), scalar=True
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
is_atomic = database_engine.lower() == 'atomic'
|
|
65
|
+
logger.info('Database engine: %s', database_engine)
|
|
66
|
+
if is_atomic:
|
|
67
|
+
logger.info('Using "TO table" for materialized views storage')
|
|
68
|
+
else:
|
|
69
|
+
logger.info('Using ".inner" for materialized views storage by default')
|
|
70
|
+
|
|
71
|
+
all_mat_views = set(dialect.get_view_names(connection))
|
|
72
|
+
|
|
73
|
+
metadata_mat_views = metadata.info.setdefault('mat_views', set())
|
|
74
|
+
|
|
75
|
+
statement_compiler = dialect.statement_compiler(dialect, None)
|
|
76
|
+
ddl_compiler = dialect.ddl_compiler(dialect, None)
|
|
77
|
+
|
|
78
|
+
for name in sorted(metadata_mat_views.difference(all_mat_views)):
|
|
79
|
+
view = metadata.mat_views[name]
|
|
80
|
+
|
|
81
|
+
selectable = statement_compiler.process(
|
|
82
|
+
view.mv_selectable, literal_binds=True
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
logger.info('Detected added materialized view %s', name)
|
|
86
|
+
|
|
87
|
+
if is_atomic or view.to:
|
|
88
|
+
create = operations.CreateMatViewToTableOp(
|
|
89
|
+
view.name, selectable, view.inner_table.name
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
else:
|
|
93
|
+
inner_table = view.inner_table
|
|
94
|
+
engine = ddl_compiler.process(inner_table.engine)
|
|
95
|
+
|
|
96
|
+
create = operations.CreateMatViewOp(
|
|
97
|
+
view.name, selectable, engine, *inner_table.columns
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
upgrade_ops.ops.append(create)
|
|
101
|
+
|
|
102
|
+
existing_metadata = sa_schema.MetaData()
|
|
103
|
+
inspector = autogen_context.inspector
|
|
104
|
+
|
|
105
|
+
removed_mat_views = all_mat_views.difference(metadata_mat_views)
|
|
106
|
+
existing_mat_views = all_mat_views.intersection(metadata_mat_views)
|
|
107
|
+
|
|
108
|
+
mat_view_params_by_name = {}
|
|
109
|
+
if removed_mat_views | existing_mat_views:
|
|
110
|
+
rv = dialect._execute(
|
|
111
|
+
connection,
|
|
112
|
+
text(
|
|
113
|
+
'SELECT name, as_select, engine_full, create_table_query '
|
|
114
|
+
'FROM system.tables '
|
|
115
|
+
'WHERE database = currentDatabase() AND name IN :names'
|
|
116
|
+
), names=list(removed_mat_views | existing_mat_views)
|
|
117
|
+
)
|
|
118
|
+
mat_view_params_by_name = {x.name: x for x in rv}
|
|
119
|
+
|
|
120
|
+
for name in sorted(removed_mat_views):
|
|
121
|
+
logger.info('Detected removed materialized view %s', name)
|
|
122
|
+
params = mat_view_params_by_name[name]
|
|
123
|
+
|
|
124
|
+
try:
|
|
125
|
+
inner_name = _extract_to_table_name(params.create_table_query)
|
|
126
|
+
except ValueError:
|
|
127
|
+
inner_name = None
|
|
128
|
+
|
|
129
|
+
if inner_name:
|
|
130
|
+
drop = operations.DropMatViewToTableOp(
|
|
131
|
+
name, params.as_select, inner_name
|
|
132
|
+
)
|
|
133
|
+
else:
|
|
134
|
+
table = Table(name, existing_metadata)
|
|
135
|
+
inspector.reflect_table(table, include_columns=None)
|
|
136
|
+
|
|
137
|
+
drop = operations.DropMatViewOp(
|
|
138
|
+
name, params.as_select, params.engine_full, *table.columns
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
upgrade_ops.ops.append(drop)
|
|
142
|
+
|
|
143
|
+
for name in sorted(existing_mat_views):
|
|
144
|
+
view = metadata.mat_views[name]
|
|
145
|
+
params = mat_view_params_by_name[name]
|
|
146
|
+
if is_atomic or view.to:
|
|
147
|
+
inner_name = _extract_to_table_name(params.create_table_query)
|
|
148
|
+
else:
|
|
149
|
+
inner_name = '.inner.' + name
|
|
150
|
+
|
|
151
|
+
conn_table = Table(inner_name, existing_metadata)
|
|
152
|
+
inspector.reflect_table(conn_table, include_columns=None)
|
|
153
|
+
|
|
154
|
+
if not autogen_context.run_object_filters(
|
|
155
|
+
view, name, 'mat_view', False, conn_table
|
|
156
|
+
):
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
metadata_table = metadata.mat_views[name].inner_table
|
|
160
|
+
modify_table_ops = ModifyTableOps(name, [])
|
|
161
|
+
schema = None
|
|
162
|
+
with _compare_columns(
|
|
163
|
+
schema,
|
|
164
|
+
inner_name,
|
|
165
|
+
conn_table,
|
|
166
|
+
metadata_table,
|
|
167
|
+
modify_table_ops,
|
|
168
|
+
autogen_context,
|
|
169
|
+
inspector,
|
|
170
|
+
):
|
|
171
|
+
comparators.dispatch('table')(
|
|
172
|
+
autogen_context,
|
|
173
|
+
modify_table_ops,
|
|
174
|
+
schema,
|
|
175
|
+
inner_name,
|
|
176
|
+
conn_table,
|
|
177
|
+
metadata_table,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
if not modify_table_ops.is_empty():
|
|
181
|
+
selectable = statement_compiler.process(
|
|
182
|
+
view.mv_selectable, literal_binds=True
|
|
183
|
+
)
|
|
184
|
+
engine = ddl_compiler.process(metadata_table.engine)
|
|
185
|
+
|
|
186
|
+
if is_atomic or view.to:
|
|
187
|
+
ops = [
|
|
188
|
+
operations.DropMatViewToTableOp(
|
|
189
|
+
name, params.as_select, metadata_table.name
|
|
190
|
+
),
|
|
191
|
+
modify_table_ops,
|
|
192
|
+
operations.CreateMatViewToTableOp(
|
|
193
|
+
view.name, selectable, metadata_table.name
|
|
194
|
+
)
|
|
195
|
+
]
|
|
196
|
+
else:
|
|
197
|
+
ops = [
|
|
198
|
+
operations.DetachMatViewOp(
|
|
199
|
+
name, params.as_select, engine,
|
|
200
|
+
*metadata_table.columns
|
|
201
|
+
),
|
|
202
|
+
modify_table_ops,
|
|
203
|
+
operations.AttachMatViewOp(
|
|
204
|
+
name, selectable, engine, *metadata_table.columns
|
|
205
|
+
)
|
|
206
|
+
]
|
|
207
|
+
|
|
208
|
+
upgrade_ops.ops.extend(ops)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from sqlalchemy import func, Column, types as sqltypes
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
from alembic.ddl import impl
|
|
5
|
+
from alembic.ddl.base import (
|
|
6
|
+
compiles, ColumnComment, format_table_name, format_column_name
|
|
7
|
+
)
|
|
8
|
+
except ImportError:
|
|
9
|
+
raise RuntimeError('alembic must be installed')
|
|
10
|
+
|
|
11
|
+
from clickhouse_sqlalchemy import types, engines
|
|
12
|
+
from clickhouse_sqlalchemy.sql.ddl import DropTable
|
|
13
|
+
from .comparators import compare_mat_view
|
|
14
|
+
from .renderers import (
|
|
15
|
+
render_attach_mat_view, render_detach_mat_view,
|
|
16
|
+
render_create_mat_view, render_drop_mat_view
|
|
17
|
+
)
|
|
18
|
+
from .toimpl import (
|
|
19
|
+
create_mat_view, attach_mat_view
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ClickHouseDialectImpl(impl.DefaultImpl):
|
|
24
|
+
__dialect__ = 'clickhouse'
|
|
25
|
+
transactional_ddl = False
|
|
26
|
+
|
|
27
|
+
def drop_table(self, table):
|
|
28
|
+
table.dispatch.before_drop(
|
|
29
|
+
table, self.connection, checkfirst=False, _ddl_runner=self
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
self._exec(DropTable(table))
|
|
33
|
+
|
|
34
|
+
table.dispatch.after_drop(
|
|
35
|
+
table, self.connection, checkfirst=False, _ddl_runner=self
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def patch_alembic_version(context, **kwargs):
|
|
40
|
+
migration_context = context._proxy._migration_context
|
|
41
|
+
version = migration_context._version
|
|
42
|
+
|
|
43
|
+
dt = Column('dt', types.DateTime, server_default=func.now())
|
|
44
|
+
version_num = Column('version_num', types.String, primary_key=True)
|
|
45
|
+
version.append_column(dt)
|
|
46
|
+
version.append_column(version_num, replace_existing=True)
|
|
47
|
+
|
|
48
|
+
if 'cluster' in kwargs:
|
|
49
|
+
cluster = kwargs['cluster']
|
|
50
|
+
version.engine = engines.ReplicatedReplacingMergeTree(
|
|
51
|
+
kwargs['table_path'], kwargs['replica_name'],
|
|
52
|
+
version=dt, order_by=func.tuple()
|
|
53
|
+
)
|
|
54
|
+
version.kwargs['clickhouse_cluster'] = cluster
|
|
55
|
+
else:
|
|
56
|
+
version.engine = engines.ReplacingMergeTree(
|
|
57
|
+
version=dt, order_by=func.tuple()
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def include_object(object, name, type_, reflected, compare_to):
|
|
62
|
+
# skip inner matview tables in autogeneration.
|
|
63
|
+
if type_ == 'table' and object.info.get('mv_storage'):
|
|
64
|
+
return False
|
|
65
|
+
|
|
66
|
+
return True
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@compiles(ColumnComment, 'clickhouse')
|
|
70
|
+
def visit_column_comment(element, compiler, **kw):
|
|
71
|
+
ddl = "ALTER TABLE {table_name} COMMENT COLUMN {column_name} {comment}"
|
|
72
|
+
comment = (
|
|
73
|
+
compiler.sql_compiler.render_literal_value(
|
|
74
|
+
element.comment or '', sqltypes.String()
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
return ddl.format(
|
|
79
|
+
table_name=format_table_name(
|
|
80
|
+
compiler, element.table_name, element.schema
|
|
81
|
+
),
|
|
82
|
+
column_name=format_column_name(compiler, element.column_name),
|
|
83
|
+
comment=comment,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
__all__ = (
|
|
88
|
+
'ClickHouseDialectImpl', 'compare_mat_view',
|
|
89
|
+
'render_attach_mat_view', 'render_detach_mat_view',
|
|
90
|
+
'render_create_mat_view', 'render_drop_mat_view',
|
|
91
|
+
'create_mat_view', 'attach_mat_view'
|
|
92
|
+
)
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
from alembic.operations import Operations, MigrateOperation
|
|
2
|
+
from alembic.operations.ops import ExecuteSQLOp
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@Operations.register_operation('create_mat_view')
|
|
6
|
+
class CreateMatViewOp(MigrateOperation):
|
|
7
|
+
def __init__(self, name, selectable, engine, *columns, **kwargs):
|
|
8
|
+
self.name = name
|
|
9
|
+
self.selectable = selectable
|
|
10
|
+
self.engine = engine
|
|
11
|
+
self.columns = columns
|
|
12
|
+
self.kwargs = kwargs
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def create_mat_view(cls, operations, name, selectable, engine, *columns,
|
|
16
|
+
**kwargs):
|
|
17
|
+
"""Issue a "CREATE MATERIALIZED VIEW" instruction."""
|
|
18
|
+
|
|
19
|
+
op = CreateMatViewOp(name, selectable, engine, *columns, **kwargs)
|
|
20
|
+
return operations.invoke(op)
|
|
21
|
+
|
|
22
|
+
def reverse(self):
|
|
23
|
+
return DropMatViewOp(
|
|
24
|
+
self.name, self.selectable, self.engine, *self.columns,
|
|
25
|
+
**self.kwargs
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@Operations.register_operation('create_mat_view_to_table')
|
|
30
|
+
class CreateMatViewToTableOp(MigrateOperation):
|
|
31
|
+
def __init__(self, name, selectable, inner_name, **kwargs):
|
|
32
|
+
self.name = name
|
|
33
|
+
self.selectable = selectable
|
|
34
|
+
self.inner_name = inner_name
|
|
35
|
+
self.kwargs = kwargs
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def create_mat_view_to_table(cls, operations, name, selectable, inner_name,
|
|
39
|
+
**kwargs):
|
|
40
|
+
"""Issue a "CREATE MATERIALIZED VIEW" instruction wit "TO" clause."""
|
|
41
|
+
|
|
42
|
+
op = CreateMatViewToTableOp(name, selectable, inner_name, **kwargs)
|
|
43
|
+
return operations.invoke(op)
|
|
44
|
+
|
|
45
|
+
def reverse(self):
|
|
46
|
+
return DropMatViewToTableOp(
|
|
47
|
+
self.name, self.selectable, self.inner_name, **self.kwargs
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@Operations.register_operation('drop_mat_view_to_table')
|
|
52
|
+
class DropMatViewToTableOp(MigrateOperation):
|
|
53
|
+
def __init__(self, name, old_selectable, inner_name, **kwargs):
|
|
54
|
+
self.name = name
|
|
55
|
+
self.old_selectable = old_selectable
|
|
56
|
+
self.inner_name = inner_name
|
|
57
|
+
self.kwargs = kwargs
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def drop_mat_view_to_table(cls, operations, name, **kwargs):
|
|
61
|
+
"""Issue a "DROP VIEW" instruction."""
|
|
62
|
+
|
|
63
|
+
sql = 'DROP VIEW '
|
|
64
|
+
if kwargs.get('if_exists'):
|
|
65
|
+
sql += 'IF EXISTS '
|
|
66
|
+
|
|
67
|
+
sql += name
|
|
68
|
+
|
|
69
|
+
if kwargs.get('on_cluster'):
|
|
70
|
+
sql += ' ON CLUSTER ' + kwargs['on_cluster']
|
|
71
|
+
|
|
72
|
+
op = ExecuteSQLOp(sql)
|
|
73
|
+
return operations.invoke(op)
|
|
74
|
+
|
|
75
|
+
def reverse(self):
|
|
76
|
+
return CreateMatViewToTableOp(
|
|
77
|
+
self.name, self.old_selectable, self.inner_name, **self.kwargs
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@Operations.register_operation('drop_mat_view')
|
|
82
|
+
class DropMatViewOp(MigrateOperation):
|
|
83
|
+
def __init__(self, name, selectable, engine, *columns, **kwargs):
|
|
84
|
+
self.name = name
|
|
85
|
+
self.selectable = selectable
|
|
86
|
+
self.engine = engine
|
|
87
|
+
self.columns = columns
|
|
88
|
+
self.kwargs = kwargs
|
|
89
|
+
|
|
90
|
+
@classmethod
|
|
91
|
+
def drop_mat_view(cls, operations, name, **kwargs):
|
|
92
|
+
"""Issue a "DROP VIEW" instruction."""
|
|
93
|
+
|
|
94
|
+
sql = 'DROP VIEW '
|
|
95
|
+
if kwargs.get('if_exists'):
|
|
96
|
+
sql += 'IF EXISTS '
|
|
97
|
+
|
|
98
|
+
sql += name
|
|
99
|
+
|
|
100
|
+
if kwargs.get('on_cluster'):
|
|
101
|
+
sql += ' ON CLUSTER ' + kwargs['on_cluster']
|
|
102
|
+
|
|
103
|
+
op = ExecuteSQLOp(sql)
|
|
104
|
+
return operations.invoke(op)
|
|
105
|
+
|
|
106
|
+
def reverse(self):
|
|
107
|
+
return CreateMatViewOp(
|
|
108
|
+
self.name, self.selectable, self.engine, *self.columns,
|
|
109
|
+
**self.kwargs
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@Operations.register_operation('attach_mat_view')
|
|
114
|
+
class AttachMatViewOp(MigrateOperation):
|
|
115
|
+
def __init__(self, name, selectable, engine, *columns, **kwargs):
|
|
116
|
+
self.name = name
|
|
117
|
+
self.selectable = selectable
|
|
118
|
+
self.engine = engine
|
|
119
|
+
self.columns = columns
|
|
120
|
+
self.kwargs = kwargs
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def attach_mat_view(cls, operations, name, selectable, engine, *columns,
|
|
124
|
+
**kwargs):
|
|
125
|
+
"""Issue a "ATTACH MATERIALIZED VIEW" instruction."""
|
|
126
|
+
|
|
127
|
+
op = AttachMatViewOp(name, selectable, engine, *columns, **kwargs)
|
|
128
|
+
return operations.invoke(op)
|
|
129
|
+
|
|
130
|
+
def reverse(self):
|
|
131
|
+
return DetachMatViewOp(
|
|
132
|
+
self.name, self.selectable, self.engine, *self.columns,
|
|
133
|
+
**self.kwargs
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@Operations.register_operation('detach_mat_view')
|
|
138
|
+
class DetachMatViewOp(MigrateOperation):
|
|
139
|
+
def __init__(self, name, old_selectable, engine, *columns, **kwargs):
|
|
140
|
+
self.name = name
|
|
141
|
+
self.old_selectable = old_selectable
|
|
142
|
+
self.engine = engine
|
|
143
|
+
self.columns = columns
|
|
144
|
+
self.kwargs = kwargs
|
|
145
|
+
|
|
146
|
+
@classmethod
|
|
147
|
+
def detach_mat_view(cls, operations, name, **kwargs):
|
|
148
|
+
"""Issue a "DETACH VIEW" instruction."""
|
|
149
|
+
|
|
150
|
+
sql = 'DETACH VIEW '
|
|
151
|
+
|
|
152
|
+
if kwargs.get('if_exists'):
|
|
153
|
+
sql += 'IF EXISTS '
|
|
154
|
+
|
|
155
|
+
sql += name
|
|
156
|
+
|
|
157
|
+
if kwargs.get('on_cluster'):
|
|
158
|
+
sql += ' ON CLUSTER ' + kwargs['on_cluster']
|
|
159
|
+
|
|
160
|
+
if kwargs.get('permanently'):
|
|
161
|
+
sql += ' PERMANENTLY'
|
|
162
|
+
|
|
163
|
+
op = ExecuteSQLOp(sql)
|
|
164
|
+
return operations.invoke(op)
|
|
165
|
+
|
|
166
|
+
def reverse(self):
|
|
167
|
+
return AttachMatViewOp(
|
|
168
|
+
self.name, self.old_selectable, self.engine, *self.columns,
|
|
169
|
+
**self.kwargs
|
|
170
|
+
)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from alembic.autogenerate import render
|
|
2
|
+
from alembic.autogenerate import renderers
|
|
3
|
+
|
|
4
|
+
from . import operations
|
|
5
|
+
|
|
6
|
+
indent = ' ' * 4
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def escape(x):
|
|
10
|
+
return x.replace("'", "\\'")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@renderers.dispatch_for(operations.CreateMatViewOp)
|
|
14
|
+
def render_create_mat_view(autogen_context, op):
|
|
15
|
+
columns = [
|
|
16
|
+
col
|
|
17
|
+
for col in [
|
|
18
|
+
render._render_column(col, autogen_context) for col in op.columns
|
|
19
|
+
]
|
|
20
|
+
if col
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
templ = (
|
|
24
|
+
"{prefix}create_mat_view(\n"
|
|
25
|
+
"{indent}'{name}',\n"
|
|
26
|
+
"{indent}'{selectable}',\n"
|
|
27
|
+
"{indent}'{engine}',\n"
|
|
28
|
+
"{indent}{columns}\n"
|
|
29
|
+
")"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
join_indent = ("'\n" + indent + "'")
|
|
33
|
+
return templ.format(
|
|
34
|
+
prefix=render._alembic_autogenerate_prefix(autogen_context),
|
|
35
|
+
name=op.name,
|
|
36
|
+
selectable=join_indent.join(escape(op.selectable).split('\n')),
|
|
37
|
+
engine=join_indent.join(escape(op.engine.strip()).split('\n')),
|
|
38
|
+
columns=(',\n' + indent).join(str(arg) for arg in columns),
|
|
39
|
+
indent=indent
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@renderers.dispatch_for(operations.DropMatViewOp)
|
|
44
|
+
def render_drop_mat_view(autogen_context, op):
|
|
45
|
+
return (
|
|
46
|
+
render._alembic_autogenerate_prefix(autogen_context) +
|
|
47
|
+
"drop_mat_view('" + op.name + "')"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@renderers.dispatch_for(operations.CreateMatViewToTableOp)
|
|
52
|
+
def render_create_mat_view_to_table(autogen_context, op):
|
|
53
|
+
templ = (
|
|
54
|
+
"{prefix}create_mat_view_to_table(\n"
|
|
55
|
+
"{indent}'{name}',\n"
|
|
56
|
+
"{indent}'{selectable}',\n"
|
|
57
|
+
"{indent}'{inner_name}'\n"
|
|
58
|
+
")"
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
join_indent = ("'\n" + indent + "'")
|
|
62
|
+
return templ.format(
|
|
63
|
+
prefix=render._alembic_autogenerate_prefix(autogen_context),
|
|
64
|
+
name=op.name,
|
|
65
|
+
selectable=join_indent.join(escape(op.selectable).split('\n')),
|
|
66
|
+
inner_name=op.inner_name,
|
|
67
|
+
indent=indent
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@renderers.dispatch_for(operations.DropMatViewToTableOp)
|
|
72
|
+
def render_drop_mat_view_to_table(autogen_context, op):
|
|
73
|
+
return (
|
|
74
|
+
render._alembic_autogenerate_prefix(autogen_context) +
|
|
75
|
+
"drop_mat_view_to_table('" + op.name + "')"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@renderers.dispatch_for(operations.AttachMatViewOp)
|
|
80
|
+
def render_attach_mat_view(autogen_context, op):
|
|
81
|
+
columns = [
|
|
82
|
+
col
|
|
83
|
+
for col in [
|
|
84
|
+
render._render_column(col, autogen_context) for col in op.columns
|
|
85
|
+
]
|
|
86
|
+
if col
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
templ = (
|
|
90
|
+
"{prefix}attach_mat_view(\n"
|
|
91
|
+
"{indent}'{name}',\n"
|
|
92
|
+
"{indent}'{selectable}',\n"
|
|
93
|
+
"{indent}'{engine}',\n"
|
|
94
|
+
"{indent}{columns}\n"
|
|
95
|
+
")"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
join_indent = ("'\n" + indent + "'")
|
|
99
|
+
return templ.format(
|
|
100
|
+
prefix=render._alembic_autogenerate_prefix(autogen_context),
|
|
101
|
+
name=op.name,
|
|
102
|
+
selectable=join_indent.join(escape(op.selectable).split('\n')),
|
|
103
|
+
engine=join_indent.join(escape(op.engine.strip()).split('\n')),
|
|
104
|
+
columns=(',\n' + indent).join(str(arg) for arg in columns),
|
|
105
|
+
indent=indent
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@renderers.dispatch_for(operations.DetachMatViewOp)
|
|
110
|
+
def render_detach_mat_view(autogen_context, op):
|
|
111
|
+
return (
|
|
112
|
+
render._alembic_autogenerate_prefix(autogen_context) +
|
|
113
|
+
"detach_mat_view('" + op.name + "')"
|
|
114
|
+
)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from alembic.operations import Operations
|
|
2
|
+
from sqlalchemy.sql.ddl import CreateColumn
|
|
3
|
+
|
|
4
|
+
from . import operations
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@Operations.implementation_for(operations.CreateMatViewOp)
|
|
8
|
+
def create_mat_view(operations, operation):
|
|
9
|
+
impl = operations.impl
|
|
10
|
+
ddl_compiler = impl.dialect.ddl_compiler(impl.dialect, None)
|
|
11
|
+
|
|
12
|
+
text = 'CREATE MATERIALIZED VIEW '
|
|
13
|
+
|
|
14
|
+
if operation.kwargs.get('if_not_exists'):
|
|
15
|
+
text += 'IF NOT EXISTS '
|
|
16
|
+
|
|
17
|
+
text += operation.name
|
|
18
|
+
|
|
19
|
+
if operation.kwargs.get('on_cluster'):
|
|
20
|
+
text += ' ON CLUSTER ' + operation.kwargs['on_cluster']
|
|
21
|
+
|
|
22
|
+
text += ' (' + ', '.join(
|
|
23
|
+
ddl_compiler.process(CreateColumn(c)) for c in operation.columns
|
|
24
|
+
) + ') '
|
|
25
|
+
|
|
26
|
+
text += 'ENGINE = ' + operation.engine
|
|
27
|
+
|
|
28
|
+
if operation.kwargs.get('populate'):
|
|
29
|
+
text += ' POPULATE'
|
|
30
|
+
|
|
31
|
+
text += ' AS ' + operation.selectable
|
|
32
|
+
|
|
33
|
+
operations.execute(text)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@Operations.implementation_for(operations.CreateMatViewToTableOp)
|
|
37
|
+
def create_mat_view_to_table(operations, operation):
|
|
38
|
+
text = 'CREATE MATERIALIZED VIEW '
|
|
39
|
+
|
|
40
|
+
if operation.kwargs.get('if_not_exists'):
|
|
41
|
+
text += 'IF NOT EXISTS '
|
|
42
|
+
|
|
43
|
+
text += operation.name
|
|
44
|
+
|
|
45
|
+
if operation.kwargs.get('on_cluster'):
|
|
46
|
+
text += ' ON CLUSTER ' + operation.kwargs['on_cluster']
|
|
47
|
+
|
|
48
|
+
text += ' TO ' + operation.inner_name
|
|
49
|
+
|
|
50
|
+
if operation.kwargs.get('populate'):
|
|
51
|
+
text += ' POPULATE'
|
|
52
|
+
|
|
53
|
+
text += ' AS ' + operation.selectable
|
|
54
|
+
|
|
55
|
+
operations.execute(text)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@Operations.implementation_for(operations.AttachMatViewOp)
|
|
59
|
+
def attach_mat_view(operations, operation):
|
|
60
|
+
impl = operations.impl
|
|
61
|
+
ddl_compiler = impl.dialect.ddl_compiler(impl.dialect, None)
|
|
62
|
+
|
|
63
|
+
text = 'ATTACH MATERIALIZED VIEW '
|
|
64
|
+
|
|
65
|
+
if operation.kwargs.get('if_not_exists'):
|
|
66
|
+
text += 'IF NOT EXISTS '
|
|
67
|
+
|
|
68
|
+
text += operation.name + ' '
|
|
69
|
+
|
|
70
|
+
if operation.kwargs.get('on_cluster'):
|
|
71
|
+
text += ' ON CLUSTER ' + operation.kwargs['on_cluster']
|
|
72
|
+
|
|
73
|
+
text += ' (' + ', '.join(
|
|
74
|
+
ddl_compiler.process(CreateColumn(c)) for c in operation.columns
|
|
75
|
+
) + ') '
|
|
76
|
+
|
|
77
|
+
text += 'ENGINE = ' + operation.engine + ' AS ' + operation.selectable
|
|
78
|
+
|
|
79
|
+
operations.execute(text)
|
|
File without changes
|