(model, connection=None)
| 192 | |
| 193 | |
| 194 | def _sync_table(model, connection=None): |
| 195 | if not _allow_schema_modification(): |
| 196 | return |
| 197 | |
| 198 | if not issubclass(model, Model): |
| 199 | raise CQLEngineException("Models must be derived from base Model.") |
| 200 | |
| 201 | if model.__abstract__: |
| 202 | raise CQLEngineException("cannot create table from abstract model") |
| 203 | |
| 204 | cf_name = model.column_family_name() |
| 205 | raw_cf_name = model._raw_column_family_name() |
| 206 | |
| 207 | ks_name = model._get_keyspace() |
| 208 | connection = connection or model._get_connection() |
| 209 | |
| 210 | cluster = get_cluster(connection) |
| 211 | |
| 212 | try: |
| 213 | keyspace = cluster.metadata.keyspaces[ks_name] |
| 214 | except KeyError: |
| 215 | msg = format_log_context("Keyspace '{0}' for model {1} does not exist.", connection=connection) |
| 216 | raise CQLEngineException(msg.format(ks_name, model)) |
| 217 | |
| 218 | tables = keyspace.tables |
| 219 | |
| 220 | syncd_types = set() |
| 221 | for col in model._columns.values(): |
| 222 | udts = [] |
| 223 | columns.resolve_udts(col, udts) |
| 224 | for udt in [u for u in udts if u not in syncd_types]: |
| 225 | _sync_type(ks_name, udt, syncd_types, connection=connection) |
| 226 | |
| 227 | if raw_cf_name not in tables: |
| 228 | log.debug(format_log_context("sync_table creating new table %s", keyspace=ks_name, connection=connection), cf_name) |
| 229 | qs = _get_create_table(model) |
| 230 | |
| 231 | try: |
| 232 | execute(qs, connection=connection) |
| 233 | except CQLEngineException as ex: |
| 234 | # 1.2 doesn't return cf names, so we have to examine the exception |
| 235 | # and ignore if it says the column family already exists |
| 236 | if "Cannot add already existing column family" not in str(ex): |
| 237 | raise |
| 238 | else: |
| 239 | log.debug(format_log_context("sync_table checking existing table %s", keyspace=ks_name, connection=connection), cf_name) |
| 240 | table_meta = tables[raw_cf_name] |
| 241 | |
| 242 | _validate_pk(model, table_meta) |
| 243 | |
| 244 | table_columns = table_meta.columns |
| 245 | model_fields = set() |
| 246 | |
| 247 | for model_name, col in model._columns.items(): |
| 248 | db_name = col.db_field_name |
| 249 | model_fields.add(db_name) |
| 250 | if db_name in table_columns: |
| 251 | col_meta = table_columns[db_name] |
no test coverage detected