2014-04-09 23:11:33 +00:00
|
|
|
from sqlalchemy import (Table, MetaData, Column, ForeignKey, Integer, String, Boolean, Text,
|
2017-01-11 19:52:12 +00:00
|
|
|
DateTime, Date, BigInteger, Index, text)
|
2014-04-09 23:11:33 +00:00
|
|
|
from peewee import (PrimaryKeyField, CharField, BooleanField, DateTimeField, TextField,
|
2015-04-13 17:31:07 +00:00
|
|
|
ForeignKeyField, BigIntegerField, IntegerField, DateField)
|
2014-04-09 23:11:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
OPTIONS_TO_COPY = [
|
|
|
|
'null',
|
|
|
|
'default',
|
|
|
|
'primary_key',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
OPTION_TRANSLATIONS = {
|
|
|
|
'null': 'nullable',
|
|
|
|
}
|
|
|
|
|
|
|
|
def gen_sqlalchemy_metadata(peewee_model_list):
|
2014-10-02 14:46:20 +00:00
|
|
|
metadata = MetaData(naming_convention={
|
|
|
|
"ix": 'ix_%(column_0_label)s',
|
|
|
|
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
|
|
|
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
|
|
|
"pk": "pk_%(table_name)s"
|
|
|
|
})
|
2014-04-09 23:11:33 +00:00
|
|
|
|
|
|
|
for model in peewee_model_list:
|
|
|
|
meta = model._meta
|
|
|
|
|
|
|
|
all_indexes = set(meta.indexes)
|
2017-01-11 19:52:12 +00:00
|
|
|
fulltext_indexes = []
|
2014-04-09 23:11:33 +00:00
|
|
|
|
|
|
|
columns = []
|
2016-01-29 18:01:17 +00:00
|
|
|
for field in meta.sorted_fields:
|
2014-04-09 23:11:33 +00:00
|
|
|
alchemy_type = None
|
|
|
|
col_args = []
|
|
|
|
col_kwargs = {}
|
|
|
|
if isinstance(field, PrimaryKeyField):
|
|
|
|
alchemy_type = Integer
|
|
|
|
elif isinstance(field, CharField):
|
|
|
|
alchemy_type = String(field.max_length)
|
|
|
|
elif isinstance(field, BooleanField):
|
|
|
|
alchemy_type = Boolean
|
|
|
|
elif isinstance(field, DateTimeField):
|
|
|
|
alchemy_type = DateTime
|
2015-04-13 17:31:07 +00:00
|
|
|
elif isinstance(field, DateField):
|
|
|
|
alchemy_type = Date
|
2014-04-09 23:11:33 +00:00
|
|
|
elif isinstance(field, TextField):
|
|
|
|
alchemy_type = Text
|
|
|
|
elif isinstance(field, ForeignKeyField):
|
|
|
|
alchemy_type = Integer
|
|
|
|
all_indexes.add(((field.name, ), field.unique))
|
2015-11-17 20:55:18 +00:00
|
|
|
if not field.deferred:
|
2018-04-06 17:48:01 +00:00
|
|
|
target_name = '%s.%s' % (field.rel_model._meta.table_name, field.rel_field.column_name)
|
2015-11-17 20:55:18 +00:00
|
|
|
col_args.append(ForeignKey(target_name))
|
2014-04-09 23:11:33 +00:00
|
|
|
elif isinstance(field, BigIntegerField):
|
|
|
|
alchemy_type = BigInteger
|
|
|
|
elif isinstance(field, IntegerField):
|
|
|
|
alchemy_type = Integer
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Unknown column type: %s' % field)
|
|
|
|
|
2017-01-11 19:52:12 +00:00
|
|
|
if hasattr(field, '__fulltext__'):
|
|
|
|
# Add the fulltext index for the field, based on whether we are under MySQL or Postgres.
|
|
|
|
fulltext_indexes.append(field.name)
|
|
|
|
|
2014-04-09 23:11:33 +00:00
|
|
|
for option_name in OPTIONS_TO_COPY:
|
|
|
|
alchemy_option_name = (OPTION_TRANSLATIONS[option_name]
|
|
|
|
if option_name in OPTION_TRANSLATIONS else option_name)
|
|
|
|
if alchemy_option_name not in col_kwargs:
|
|
|
|
option_val = getattr(field, option_name)
|
|
|
|
col_kwargs[alchemy_option_name] = option_val
|
|
|
|
|
|
|
|
if field.unique or field.index:
|
|
|
|
all_indexes.add(((field.name, ), field.unique))
|
|
|
|
|
2018-04-06 17:48:01 +00:00
|
|
|
new_col = Column(field.column_name, alchemy_type, *col_args, **col_kwargs)
|
2014-04-09 23:11:33 +00:00
|
|
|
columns.append(new_col)
|
|
|
|
|
2018-04-06 17:48:01 +00:00
|
|
|
new_table = Table(meta.table_name, metadata, *columns)
|
2014-04-09 23:11:33 +00:00
|
|
|
|
|
|
|
for col_prop_names, unique in all_indexes:
|
2018-04-06 17:48:01 +00:00
|
|
|
col_names = [meta.fields[prop_name].column_name for prop_name in col_prop_names]
|
|
|
|
index_name = '%s_%s' % (meta.table_name, '_'.join(col_names))
|
2014-04-09 23:11:33 +00:00
|
|
|
col_refs = [getattr(new_table.c, col_name) for col_name in col_names]
|
|
|
|
Index(index_name, *col_refs, unique=unique)
|
|
|
|
|
2017-01-11 19:52:12 +00:00
|
|
|
for col_field_name in fulltext_indexes:
|
2018-04-06 17:48:01 +00:00
|
|
|
index_name = '%s_%s__fulltext' % (meta.table_name, col_field_name)
|
2017-01-11 19:52:12 +00:00
|
|
|
col_ref = getattr(new_table.c, col_field_name)
|
|
|
|
Index(index_name, col_ref, postgresql_ops={col_field_name: 'gin_trgm_ops'},
|
|
|
|
postgresql_using='gin',
|
|
|
|
mysql_prefix='FULLTEXT')
|
|
|
|
|
2014-04-09 23:11:33 +00:00
|
|
|
return metadata
|