-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema.py
More file actions
1374 lines (1151 loc) · 47.4 KB
/
schema.py
File metadata and controls
1374 lines (1151 loc) · 47.4 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# cardinal_pythonlib/sqlalchemy/schema.py
"""
===============================================================================
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===============================================================================
**Functions to work with SQLAlchemy schemas (schemata) directly, via SQLAlchemy
Core.**
Functions that have to work with specific dialect information are marked
DIALECT-AWARE.
"""
import ast
import copy
import csv
from functools import lru_cache
import io
import re
from typing import (
Any,
Dict,
Generator,
List,
Optional,
Type,
Union,
TYPE_CHECKING,
)
from sqlalchemy import inspect
from sqlalchemy.engine import Connection, Engine
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.dialects import postgresql, mssql, mysql, sqlite
from sqlalchemy.dialects.mssql.base import TIMESTAMP as MSSQL_TIMESTAMP
from sqlalchemy.schema import (
Column,
CreateColumn,
DDL,
Identity,
Index,
Table,
)
from sqlalchemy.sql.ddl import DDLElement
from sqlalchemy.sql.expression import text
from sqlalchemy.sql.sqltypes import (
BigInteger,
Boolean,
Date,
DateTime,
Enum,
Float,
Integer,
LargeBinary,
Numeric,
SmallInteger,
String,
Text,
TypeEngine,
Unicode,
UnicodeText,
)
from sqlalchemy.sql.visitors import Visitable
from cardinal_pythonlib.logs import get_brace_style_log_with_null_handler
from cardinal_pythonlib.sqlalchemy.dialect import (
quote_identifier,
SqlaDialectName,
)
from cardinal_pythonlib.sqlalchemy.orm_inspect import coltype_as_typeengine
if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import ReflectedIndex
log = get_brace_style_log_with_null_handler(__name__)
try:
from sqlalchemy.sql.sqltypes import Double
except ImportError:
# This code present to allow testing with older SQLAlchemy 1.4.
log.warning(
"Can't import sqlalchemy.sql.sqltypes.Double "
"(are you using SQLAlchemy prior to 2.0?)"
)
Double = None
# =============================================================================
# Constants
# =============================================================================
# To avoid importing _Binary directly:
if len(LargeBinary.__bases__) != 1:
raise NotImplementedError(
"Unexpectedly, SQLAlchemy's LargeBinary class has more than one base "
"class"
)
BinaryBaseClass = LargeBinary.__bases__[0]
VisitableType = Type[Visitable] # for SQLAlchemy 2.0
MIN_TEXT_LENGTH_FOR_FREETEXT_INDEX = 1000
MSSQL_DEFAULT_SCHEMA = "dbo"
POSTGRES_DEFAULT_SCHEMA = "public"
DATABRICKS_SQLCOLTYPE_TO_SQLALCHEMY_GENERIC = {
# A bit nasty: https://github.com/databricks/databricks-sqlalchemy
# Part of the reverse mapping is via
# from databricks.sqlalchemy import DatabricksDialect
# print(DatabricksDialect.colspecs)
"BIGINT": BigInteger,
"BOOLEAN": Boolean,
"DATE": Date,
"TIMESTAMP_NTZ": DateTime,
"DOUBLE": Double if Double is not None else Float,
"FLOAT": Float,
"INT": Integer,
"DECIMAL": Numeric,
"SMALLINT": SmallInteger,
"STRING": Text,
"VARCHAR": String,
}
# =============================================================================
# Inspect tables (SQLAlchemy Core)
# =============================================================================
def get_table_names(engine: Engine) -> List[str]:
"""
Returns a list of database table names from the :class:`Engine`.
"""
insp = inspect(engine)
return insp.get_table_names()
def get_view_names(engine: Engine) -> List[str]:
"""
Returns a list of database view names from the :class:`Engine`.
"""
insp = inspect(engine)
return insp.get_view_names()
def table_exists(engine: Engine, tablename: str) -> bool:
"""
Does the named table exist in the database?
"""
return tablename in get_table_names(engine)
def view_exists(engine: Engine, viewname: str) -> bool:
"""
Does the named view exist in the database?
"""
return viewname in get_view_names(engine)
def table_or_view_exists(engine: Engine, table_or_view_name: str) -> bool:
"""
Does the named table/view exist (either as a table or as a view) in the
database?
"""
tables_and_views = get_table_names(engine) + get_view_names(engine)
return table_or_view_name in tables_and_views
class SqlaColumnInspectionInfo(object):
"""
Class to represent information from inspecting a database column.
A clearer way of getting information than the plain ``dict`` that
SQLAlchemy uses.
"""
def __init__(self, sqla_info_dict: Dict[str, Any]) -> None:
"""
Args:
sqla_info_dict:
see
- https://docs.sqlalchemy.org/en/latest/core/reflection.html#sqlalchemy.engine.reflection.Inspector.get_columns
- https://bitbucket.org/zzzeek/sqlalchemy/issues/4051/sqlalchemyenginereflectioninspectorget_col
""" # noqa: E501
# log.debug(repr(sqla_info_dict))
self.name = sqla_info_dict["name"] # type: str
self.type = sqla_info_dict["type"] # type: TypeEngine
self.nullable = sqla_info_dict["nullable"] # type: bool
self.default = sqla_info_dict[
"default"
] # type: Optional[str] # SQL string expression
self.attrs = sqla_info_dict.get("attrs", {}) # type: Dict[str, Any]
self.comment = sqla_info_dict.get("comment", "")
# ... NB not appearing in
def gen_columns_info(
engine: Engine, tablename: str
) -> Generator[SqlaColumnInspectionInfo, None, None]:
"""
For the specified table, generate column information as
:class:`SqlaColumnInspectionInfo` objects.
"""
# Dictionary structure: see
# http://docs.sqlalchemy.org/en/latest/core/reflection.html#sqlalchemy.engine.reflection.Inspector.get_columns # noqa: E501
insp = inspect(engine)
for d in insp.get_columns(tablename):
yield SqlaColumnInspectionInfo(d)
def get_column_info(
engine: Engine, tablename: str, columnname: str
) -> Optional[SqlaColumnInspectionInfo]:
"""
For the specified column in the specified table, get column information
as a :class:`SqlaColumnInspectionInfo` object (or ``None`` if such a
column can't be found).
"""
for info in gen_columns_info(engine, tablename):
if info.name == columnname:
return info
return None
def get_column_type(
engine: Engine, tablename: str, columnname: str
) -> Optional[TypeEngine]:
"""
For the specified column in the specified table, get its type as an
instance of an SQLAlchemy column type class (or ``None`` if such a column
can't be found).
For more on :class:`TypeEngine`, see
:func:`cardinal_pythonlib.orm_inspect.coltype_as_typeengine`.
"""
for info in gen_columns_info(engine, tablename):
if info.name == columnname:
return info.type
return None
def get_column_names(engine: Engine, tablename: str) -> List[str]:
"""
Get all the database column names for the specified table.
"""
return [info.name for info in gen_columns_info(engine, tablename)]
# =============================================================================
# More introspection
# =============================================================================
def get_pk_colnames(table_: Table) -> List[str]:
"""
If a table has a PK, this will return its database column name(s);
otherwise, ``None``.
"""
pk_names = [] # type: List[str]
for col in table_.columns:
if col.primary_key:
pk_names.append(col.name)
return pk_names
def get_single_int_pk_colname(table_: Table) -> Optional[str]:
"""
If a table has a single-field (non-composite) integer PK, this will
return its database column name; otherwise, None.
Note that it is legitimate for a database table to have both a composite
primary key and a separate ``IDENTITY`` (``AUTOINCREMENT``) integer field.
This function won't find such columns.
"""
n_pks = 0
int_pk_names = []
for col in table_.columns:
if col.primary_key:
n_pks += 1
if is_sqlatype_integer(col.type):
int_pk_names.append(col.name)
if n_pks == 1 and len(int_pk_names) == 1:
return int_pk_names[0]
return None
def is_int_autoincrement_column(c: Column, t: Table) -> bool:
"""
Is this an integer AUTOINCREMENT column? Used by
get_single_int_autoincrement_colname(); q.v.
"""
# https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column.params.autoincrement # noqa: E501
# "The setting only has an effect for columns which are:
# - Integer derived (i.e. INT, SMALLINT, BIGINT).
# - Part of the primary key
# - Not referring to another column via ForeignKey, unless the value is
# specified as 'ignore_fk':"
if not c.primary_key or not is_sqlatype_integer(c.type):
return False
a = c.autoincrement
if isinstance(a, bool):
# Specified as True or False.
return a
if a == "auto":
# "indicates that a single-column (i.e. non-composite) primary key that
# is of an INTEGER type with no other client-side or server-side
# default constructs indicated should receive auto increment semantics
# automatically." Therefore:
n_pk = sum(x.primary_key for x in t.columns)
return n_pk == 1 and c.default is None
if c.foreign_keys:
return a == "ignore_fk"
return False
def get_single_int_autoincrement_colname(table_: Table) -> Optional[str]:
"""
If a table has a single integer ``AUTOINCREMENT`` column, this will
return its name; otherwise, ``None``.
- It's unlikely that a database has >1 ``AUTOINCREMENT`` field anyway, but
we should check.
- SQL Server's ``IDENTITY`` keyword is equivalent to MySQL's
``AUTOINCREMENT``.
- Verify against SQL Server:
.. code-block:: sql
SELECT table_name, column_name
FROM information_schema.columns
WHERE COLUMNPROPERTY(OBJECT_ID(table_schema + '.' + table_name),
column_name,
'IsIdentity') = 1
ORDER BY table_name;
... https://stackoverflow.com/questions/87747
- Also:
.. code-block:: sql
sp_columns 'tablename';
... which is what SQLAlchemy does (``dialects/mssql/base.py``, in
:func:`get_columns`).
"""
int_autoinc_names = [] # type: List[str]
for col in table_.columns:
if is_int_autoincrement_column(col, table_):
int_autoinc_names.append(col.name)
n_autoinc = len(int_autoinc_names)
if n_autoinc == 1:
return int_autoinc_names[0]
if n_autoinc > 1:
log.warning(
"Table {!r} has {} integer autoincrement columns",
table_.name,
n_autoinc,
)
return None
def get_effective_int_pk_col(table_: Table) -> Optional[str]:
"""
If a table has a single integer primary key, or a single integer
``AUTOINCREMENT`` column, return its column name; otherwise, ``None``.
"""
return (
get_single_int_pk_colname(table_)
or get_single_int_autoincrement_colname(table_)
or None
)
# =============================================================================
# Execute DDL
# =============================================================================
def execute_ddl(
engine: Engine, sql: str = None, ddl: DDLElement = None
) -> None:
"""
Execute DDL, either from a plain SQL string, or from an SQLAlchemy DDL
element.
Previously we would use DDL(sql, bind=engine).execute(), but this has gone
in SQLAlchemy 2.0.
Note that creating the DDL object with e.g. ddl =
DDL(sql).execute_if(dialect=SqlaDialectName.SQLSERVER), and passing that
DDL object to this function, does NOT make execution condition; it executes
regardless. The execute_if() construct is used for listeners; see
https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.ExecutableDDLElement.execute_if
"""
assert bool(sql) ^ (ddl is not None) # one or the other.
if sql:
ddl = DDL(sql)
with engine.connect() as connection:
connection.execute(ddl)
# DDL may need a commit for some dialects:
#
# Generic (but generic may not be useful)
# https://stackoverflow.com/questions/730621/do-ddl-statements-always-give-you-an-implicit-commit-or-can-you-get-an-implicit
# Oracle - autocommitted?
# https://docs.oracle.com/cd/A97335_02/apps.102/a83723/keyprog6.htm
# but not Postgres?
# https://dba.stackexchange.com/questions/340916/why-must-i-commit-after-the-alter-table-ddl-to-make-changes-visible
# and in SQL Server they are "batched" so not entirely autocommitted
# https://www.mssqltips.com/sqlservertip/4591/ddl-commands-in-transactions-in-sql-server-versus-oracle/
connection.commit()
# =============================================================================
# Indexes
# =============================================================================
def index_exists(
engine: Engine,
tablename: str,
indexname: str = None,
colnames: Union[str, List[str]] = None,
raise_if_nonexistent_table: bool = True,
) -> bool:
"""
Does the specified index exist for the specified table?
You can specify either the name of the index, or the name(s) of columns.
But not both.
If the table doesn't exist, then if raise_if_nonexistent_table is True,
raise sqlalchemy.exc.NoSuchTableError; otherwise, warn and return False.
"""
assert bool(indexname) ^ bool(colnames) # one or the other
insp = inspect(engine)
if not raise_if_nonexistent_table and not insp.has_table(tablename):
log.warning(f"index_exists(): no such table {tablename!r}")
return False
indexes = insp.get_indexes(tablename) # type: List[ReflectedIndex]
if indexname:
# Look up by index name.
return any(i["name"] == indexname for i in indexes)
else:
# Look up by column names. All must be present in a given index.
if isinstance(colnames, str):
colnames = [colnames]
return any(
all(colname in i["column_names"] for colname in colnames)
for i in indexes
)
def mssql_get_pk_index_name(
engine: Engine, tablename: str, schemaname: str = MSSQL_DEFAULT_SCHEMA
) -> str:
"""
For Microsoft SQL Server specifically: fetch the name of the PK index
for the specified table (in the specified schema), or ``''`` if none is
found.
"""
# http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Connection.execute # noqa: E501
# http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text # noqa: E501
# http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.TextClause.bindparams # noqa: E501
# http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.CursorResult # noqa: E501
query = text(
"""
SELECT
kc.name AS index_name
FROM
sys.key_constraints AS kc
INNER JOIN sys.tables AS ta ON ta.object_id = kc.parent_object_id
INNER JOIN sys.schemas AS s ON ta.schema_id = s.schema_id
WHERE
kc.[type] = 'PK'
AND ta.name = :tablename
AND s.name = :schemaname
"""
).bindparams(tablename=tablename, schemaname=schemaname)
with engine.begin() as connection:
result = connection.execute(query)
row = result.fetchone()
return row[0] if row else ""
def mssql_table_has_ft_index(
engine: Engine, tablename: str, schemaname: str = MSSQL_DEFAULT_SCHEMA
) -> bool:
"""
For Microsoft SQL Server specifically: does the specified table (in the
specified schema) have at least one full-text index?
"""
query = text(
"""
SELECT
COUNT(*)
FROM
sys.key_constraints AS kc
INNER JOIN sys.tables AS ta ON ta.object_id = kc.parent_object_id
INNER JOIN sys.schemas AS s ON ta.schema_id = s.schema_id
INNER JOIN sys.fulltext_indexes AS fi ON fi.object_id = ta.object_id
WHERE
ta.name = :tablename
AND s.name = :schemaname
"""
).bindparams(tablename=tablename, schemaname=schemaname)
with engine.begin() as connection:
result = connection.execute(query)
row = result.fetchone()
return row[0] > 0
def mssql_transaction_count(engine_or_conn: Union[Connection, Engine]) -> int:
"""
For Microsoft SQL Server specifically: fetch the value of the ``TRANCOUNT``
variable (see e.g.
https://docs.microsoft.com/en-us/sql/t-sql/functions/trancount-transact-sql?view=sql-server-2017).
Returns ``None`` if it can't be found (unlikely?).
"""
query = text("SELECT @@TRANCOUNT")
if isinstance(engine_or_conn, Connection):
result = engine_or_conn.execute(query)
row = result.fetchone()
elif isinstance(engine_or_conn, Engine):
with engine_or_conn.begin() as connection:
result = connection.execute(query)
row = result.fetchone()
else:
raise ValueError(f"Unexpected {engine_or_conn=}")
return row[0] if row else None
def add_index(
engine: Engine,
sqla_column: Column = None,
multiple_sqla_columns: List[Column] = None,
unique: bool = False,
fulltext: bool = False,
length: int = None,
) -> None:
"""
Adds an index to a database column (or, in restricted circumstances,
several columns).
The table name is worked out from the :class:`Column` object.
DIALECT-AWARE.
Args:
engine: SQLAlchemy :class:`Engine` object
sqla_column: single column to index
multiple_sqla_columns: multiple columns to index (see below)
unique: make a ``UNIQUE`` index?
fulltext: make a ``FULLTEXT`` index?
length: index length to use (default ``None``)
Restrictions:
- Specify either ``sqla_column`` or ``multiple_sqla_columns``, not both.
- The normal method is ``sqla_column``.
- ``multiple_sqla_columns`` is only used for Microsoft SQL Server full-text
indexing (as this database permits only one full-text index per table,
though that index can be on multiple columns).
"""
# We used to process a table as a unit; this makes index creation faster
# (using ALTER TABLE).
# http://dev.mysql.com/doc/innodb/1.1/en/innodb-create-index-examples.html # noqa: E501
# ... ignored in transition to SQLAlchemy
def quote(identifier: str) -> str:
return quote_identifier(identifier, engine)
is_mssql = engine.dialect.name == SqlaDialectName.MSSQL
is_mysql = engine.dialect.name == SqlaDialectName.MYSQL
is_sqlite = engine.dialect.name == SqlaDialectName.SQLITE
multiple_sqla_columns = multiple_sqla_columns or [] # type: List[Column]
if multiple_sqla_columns and not (fulltext and is_mssql):
raise ValueError(
"add_index: Use multiple_sqla_columns only for mssql "
"(Microsoft SQL Server) full-text indexing"
)
if bool(multiple_sqla_columns) == (sqla_column is not None):
raise ValueError(
f"add_index: Use either sqla_column or multiple_sqla_columns, "
f"not both (sqla_column = {sqla_column!r}, "
f"multiple_sqla_columns = {multiple_sqla_columns!r})"
)
if sqla_column is not None:
colnames = [sqla_column.name]
sqla_table = sqla_column.table
tablename = sqla_table.name
else:
colnames = [c.name for c in multiple_sqla_columns]
sqla_table = multiple_sqla_columns[0].table
tablename = sqla_table.name
if any(c.table.name != tablename for c in multiple_sqla_columns[1:]):
raise ValueError(
f"add_index: tablenames are inconsistent in "
f"multiple_sqla_columns = {multiple_sqla_columns!r}"
)
if fulltext:
if is_mssql:
idxname = "" # they are unnamed
else:
idxname = "_idxft_{}".format("_".join(colnames))
else:
idxname = "_idx_{}".format("_".join(colnames))
if is_sqlite:
# SQLite doesn't allow indexes with the same names on different tables.
idxname = f"{tablename}_{idxname}"
if idxname and index_exists(engine, tablename, idxname):
log.info(
f"Skipping creation of index {idxname} on "
f"table {tablename}; already exists"
)
return
# because it will crash if you add it again!
log.info(
"Creating{ft} index {i} on table {t}, column(s) {c}",
ft=" full-text" if fulltext else "",
i=idxname or "<unnamed>",
t=tablename,
c=", ".join(colnames),
)
if fulltext:
if is_mysql:
log.info(
"OK to ignore this warning, if it follows next: "
'"InnoDB rebuilding table to add column FTS_DOC_ID"'
)
# https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html
sql = (
"ALTER TABLE {tablename} "
"ADD FULLTEXT INDEX {idxname} ({colnames})".format(
tablename=quote(tablename),
idxname=quote(idxname),
colnames=", ".join(quote(c) for c in colnames),
)
)
execute_ddl(engine, sql=sql)
elif is_mssql: # Microsoft SQL Server
# https://msdn.microsoft.com/library/ms187317(SQL.130).aspx
# Argh! Complex.
# Note that the database must also have had a
# CREATE FULLTEXT CATALOG somename AS DEFAULT;
# statement executed on it beforehand.
connection = Connection(engine)
schemaname = (
connection.schema_for_object(sqla_table)
or MSSQL_DEFAULT_SCHEMA
)
if mssql_table_has_ft_index(
engine=engine, tablename=tablename, schemaname=schemaname
):
log.info(
f"... skipping creation of full-text index on table "
f"{tablename}; a full-text index already exists for that "
f"table; you can have only one full-text index per table, "
f"though it can be on multiple columns"
)
return
pk_index_name = mssql_get_pk_index_name(
engine=engine, tablename=tablename, schemaname=schemaname
)
if not pk_index_name:
raise ValueError(
f"To make a FULLTEXT index under SQL Server, we need to "
f"know the name of the PK index, but couldn't find one "
f"via mssql_get_pk_index_name() for table {tablename!r}"
)
# We don't name the FULLTEXT index itself, but it has to relate
# to an existing unique index.
sql = (
"CREATE FULLTEXT INDEX ON {tablename} ({colnames}) "
"KEY INDEX {keyidxname} ".format(
tablename=quote(tablename),
keyidxname=quote(pk_index_name),
colnames=", ".join(quote(c) for c in colnames),
)
)
# SQL Server won't let you do this inside a transaction:
# "CREATE FULLTEXT INDEX statement cannot be used inside a user
# transaction."
# https://msdn.microsoft.com/nl-nl/library/ms191544(v=sql.105).aspx
# So let's ensure any preceding transactions are completed, and
# run the SQL in a raw way:
# engine.execute(sql).execution_options(autocommit=False)
# http://docs.sqlalchemy.org/en/latest/core/connections.html#understanding-autocommit
#
# ... lots of faff with this (see test code in no_transactions.py)
# ... ended up using explicit "autocommit=True" parameter (for
# pyodbc); see create_indexes()
transaction_count = mssql_transaction_count(engine)
if transaction_count != 0:
log.critical(
f"SQL Server transaction count (should be 0): "
f"{transaction_count}"
)
# Executing serial COMMITs or a ROLLBACK won't help here if
# this transaction is due to Python DBAPI default behaviour.
execute_ddl(engine, sql=sql)
# The reversal procedure is DROP FULLTEXT INDEX ON tablename;
else:
log.error(
f"Don't know how to make full text index on dialect "
f"{engine.dialect.name}"
)
else:
index = Index(idxname, sqla_column, unique=unique, mysql_length=length)
index.create(engine)
# Index creation doesn't require a commit.
# =============================================================================
# More DDL
# =============================================================================
# https://stackoverflow.com/questions/18835740/does-bigint-auto-increment-work-for-sqlalchemy-with-sqlite # noqa: E501
BigIntegerForAutoincrementType = BigInteger()
BigIntegerForAutoincrementType = BigIntegerForAutoincrementType.with_variant(
postgresql.BIGINT(), SqlaDialectName.POSTGRES
)
BigIntegerForAutoincrementType = BigIntegerForAutoincrementType.with_variant(
mssql.BIGINT(), SqlaDialectName.MSSQL
)
BigIntegerForAutoincrementType = BigIntegerForAutoincrementType.with_variant(
mysql.BIGINT(), SqlaDialectName.MYSQL
)
BigIntegerForAutoincrementType = BigIntegerForAutoincrementType.with_variant(
sqlite.INTEGER(), SqlaDialectName.SQLITE
)
def make_bigint_autoincrement_column(
column_name: str, nullable: bool = False, comment: str = None
) -> Column:
"""
Returns an instance of :class:`Column` representing a :class:`BigInteger`
``AUTOINCREMENT`` column, or the closest that the database engine can
manage.
"""
return Column(
column_name,
BigIntegerForAutoincrementType,
Identity(start=1, increment=1),
# https://docs.sqlalchemy.org/en/20/core/defaults.html#identity-ddl
autoincrement=True,
nullable=nullable,
comment=comment,
)
# see also: https://stackoverflow.com/questions/2937229
def column_creation_ddl(sqla_column: Column, dialect: Dialect) -> str:
"""
Returns DDL to create a column, using the specified dialect.
The column should already be bound to a table (because e.g. the SQL Server
dialect requires this for DDL generation). If you don't append the column
to a Table object, the DDL generation step gives
"sqlalchemy.exc.CompileError: mssql requires Table-bound columns in order
to generate DDL".
Testing: see schema_tests.py
"""
return str(CreateColumn(sqla_column).compile(dialect=dialect))
# noinspection PyUnresolvedReferences
def giant_text_sqltype(dialect: Dialect) -> str:
"""
Returns the SQL column type used to make very large text columns for a
given dialect.
DIALECT-AWARE.
Args:
dialect: a SQLAlchemy :class:`Dialect`
Returns:
the SQL data type of "giant text", typically 'LONGTEXT' for MySQL
and 'NVARCHAR(MAX)' for SQL Server.
"""
dname = dialect.name
if dname == SqlaDialectName.MSSQL:
return "NVARCHAR(MAX)"
# https://learn.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql?view=sql-server-ver16 # noqa: E501
elif dname == SqlaDialectName.MYSQL:
return "LONGTEXT"
# https://dev.mysql.com/doc/refman/8.4/en/blob.html
elif dname == SqlaDialectName.ORACLE:
return "LONG"
# https://docs.oracle.com/cd/A58617_01/server.804/a58241/ch5.htm
elif dname == SqlaDialectName.POSTGRES:
return "TEXT"
# https://www.postgresql.org/docs/current/datatype-character.html
elif dname == SqlaDialectName.SQLITE:
return "TEXT"
# https://www.sqlite.org/datatype3.html
elif dname == SqlaDialectName.DATABRICKS:
return "STRING"
# https://github.com/databricks/databricks-sqlalchemy
else:
raise ValueError(f"Unknown dialect: {dname}")
# =============================================================================
# SQLAlchemy column types
# =============================================================================
# -----------------------------------------------------------------------------
# Reverse a textual SQL column type to an SQLAlchemy column type
# -----------------------------------------------------------------------------
RE_MYSQL_ENUM_COLTYPE = re.compile(r"ENUM\((?P<valuelist>.+)\)")
RE_COLTYPE_WITH_COLLATE = re.compile(r"(?P<maintype>.+) COLLATE .+")
RE_COLTYPE_WITH_ONE_PARAM = re.compile(r"(?P<type>\w+)\((?P<size>\w+)\)")
# ... e.g. "VARCHAR(10)"
RE_COLTYPE_WITH_TWO_PARAMS = re.compile(
r"(?P<type>\w+)\((?P<size>\w+),\s*(?P<dp>\w+)\)"
)
# ... e.g. "DECIMAL(10, 2)"
# http://www.w3schools.com/sql/sql_create_table.asp
def _get_sqla_coltype_class_from_str(
coltype: str, dialect: Dialect
) -> Type[TypeEngine]:
"""
Returns the SQLAlchemy class corresponding to a particular SQL column
type in a given dialect.
DIALECT-AWARE.
Performs an upper- and lower-case search.
For example, the SQLite dialect uses upper case, and the
MySQL dialect uses lower case.
For exploratory thinking, see
dev_notes/convert_sql_string_coltype_to_sqlalchemy_type.py.
DISCUSSION AT: https://github.com/sqlalchemy/sqlalchemy/discussions/12230
"""
if hasattr(dialect, "ischema_names"):
# The built-in dialects all have this, even though it's an internal
# detail.
ischema_names = dialect.ischema_names
try:
return ischema_names[coltype.upper()]
except KeyError:
return ischema_names[coltype.lower()]
elif dialect.name == SqlaDialectName.DATABRICKS:
# Ugly hack.
# Databricks is an example that doesn't have ischema_names.
try:
return DATABRICKS_SQLCOLTYPE_TO_SQLALCHEMY_GENERIC[coltype.upper()]
except KeyError:
raise ValueError(
f"Don't know how to convert SQL column type {coltype!r} "
f"to SQLAlchemy dialect {dialect!r}"
)
else:
raise ValueError(
f"Don't know a generic way to convert SQL column types "
f"(in text format) to SQLAlchemy dialect {dialect.name!r}. "
)
def get_list_of_sql_string_literals_from_quoted_csv(x: str) -> List[str]:
"""
Used to extract SQL column type parameters. For example, MySQL has column
types that look like ``ENUM('a', 'b', 'c', 'd')``. This function takes the
``"'a', 'b', 'c', 'd'"`` and converts it to ``['a', 'b', 'c', 'd']``.
"""
f = io.StringIO(x)
reader = csv.reader(
f,
delimiter=",",
quotechar="'",
quoting=csv.QUOTE_ALL,
skipinitialspace=True,
)
for line in reader: # should only be one
return [x for x in line]
@lru_cache(maxsize=None)
def get_sqla_coltype_from_dialect_str(
coltype: str, dialect: Dialect
) -> TypeEngine:
"""
Returns an SQLAlchemy column type, given a column type name (a string) and
an SQLAlchemy dialect. For example, this might convert the string
``INTEGER(11)`` to an SQLAlchemy ``Integer(length=11)``.
NOTE that the reverse operation is performed by ``str(coltype)`` or
``coltype.compile()`` or ``coltype.compile(dialect)``; see
:class:`TypeEngine`.
DIALECT-AWARE.
Args:
dialect: a SQLAlchemy :class:`Dialect` class
coltype: a ``str()`` representation, e.g. from ``str(c['type'])`` where
``c`` is an instance of :class:`sqlalchemy.sql.schema.Column`.
Returns:
a Python object that is a subclass of
:class:`sqlalchemy.types.TypeEngine`
Example:
.. code-block:: python
get_sqla_coltype_from_string('INTEGER(11)', engine.dialect)
# gives: Integer(length=11)
Notes:
- :class:`sqlalchemy.engine.default.DefaultDialect` is the dialect base
class
- a dialect contains these things of interest:
- ``ischema_names``: string-to-class dictionary
- ``type_compiler``: instance of e.g.
:class:`sqlalchemy.sql.compiler.GenericTypeCompiler`. This has a
``process()`` method, but that operates on :class:`TypeEngine` objects.
- ``get_columns``: takes a table name, inspects the database
- example of the dangers of ``eval``:
https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
- An example of a function doing the reflection/inspection within
SQLAlchemy is
:func:`sqlalchemy.dialects.mssql.base.MSDialect.get_columns`,
which has this lookup: ``coltype = self.ischema_names.get(type, None)``
Caveats:
- the parameters, e.g. ``DATETIME(6)``, do NOT necessarily either work at
all or work correctly. For example, SQLAlchemy will happily spit out
``'INTEGER(11)'`` but its :class:`sqlalchemy.sql.sqltypes.INTEGER` class
takes no parameters, so you get the error ``TypeError: object() takes no
parameters``. Similarly, MySQL's ``DATETIME(6)`` uses the 6 to refer to
precision, but the ``DATETIME`` class in SQLAlchemy takes only a boolean
parameter (timezone).
- However, sometimes we have to have parameters, e.g. ``VARCHAR`` length.
- Thus, this is a bit useless.
- Fixed, with a few special cases.
"""
size = None # type: Optional[int]
dp = None # type: Optional[int]
args = [] # type: List[Any]
kwargs = {} # type: Dict[str, Any]
basetype = ""
# noinspection PyPep8,PyBroadException
try:
# Split e.g. "VARCHAR(32) COLLATE blah" into "VARCHAR(32)", "who cares"
m = RE_COLTYPE_WITH_COLLATE.match(coltype)
if m is not None:
coltype = m.group("maintype")
found = False
if not found:
# Deal with ENUM('a', 'b', 'c', ...)