-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2952 from centerofci/constraint_create_sql
Move constraint creation to SQL
- Loading branch information
Showing
9 changed files
with
823 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
from alembic.migration import MigrationContext | ||
from alembic.operations import Operations | ||
from sqlalchemy import MetaData | ||
"""TODO This needs to be consolidated with db.constraints.base""" | ||
import json | ||
|
||
from db.columns.operations.select import get_column_names_from_attnums | ||
from db.constraints.utils import get_constraint_type_from_char, ConstraintType, naming_convention | ||
from db.tables.operations.select import reflect_table_from_oid | ||
from db.metadata import get_empty_metadata | ||
from db.connection import execute_msar_func_with_engine | ||
from db.constraints.utils import get_constraint_type_from_char, ConstraintType | ||
|
||
|
||
def create_unique_constraint(table_name, schema, engine, columns, constraint_name=None): | ||
with engine.begin() as conn: | ||
metadata = MetaData(bind=engine, schema=schema, naming_convention=naming_convention) | ||
opts = { | ||
'target_metadata': metadata | ||
} | ||
ctx = MigrationContext.configure(conn, opts=opts) | ||
op = Operations(ctx) | ||
op.create_unique_constraint(constraint_name, table_name, columns, schema) | ||
return execute_msar_func_with_engine( | ||
engine, | ||
'add_constraints', | ||
schema, | ||
table_name, | ||
json.dumps([{'name': constraint_name, 'type': 'u', 'columns': columns}]) | ||
).fetchone()[0] | ||
|
||
|
||
def create_constraint(schema, engine, constraint_obj): | ||
with engine.begin() as conn: | ||
constraint_obj.add_constraint(schema, engine, conn) | ||
return constraint_obj.add_constraint(schema, engine, conn) | ||
|
||
|
||
def copy_constraint(table_oid, engine, constraint, from_column_attnum, to_column_attnum): | ||
# TODO reuse metadata | ||
metadata = get_empty_metadata() | ||
table = reflect_table_from_oid(table_oid, engine, metadata=metadata) | ||
def copy_constraint(_, engine, constraint, from_column_attnum, to_column_attnum): | ||
constraint_type = get_constraint_type_from_char(constraint.contype) | ||
if constraint_type == ConstraintType.UNIQUE.value: | ||
column_attnums = constraint.conkey | ||
changed_column_attnums = [to_column_attnum if attnum == from_column_attnum else attnum for attnum in column_attnums] | ||
columns = get_column_names_from_attnums(table_oid, changed_column_attnums, engine, metadata=metadata) | ||
create_unique_constraint(table.name, table.schema, engine, columns) | ||
return execute_msar_func_with_engine( | ||
engine, | ||
'copy_constraint', | ||
constraint.oid, | ||
from_column_attnum, | ||
to_column_attnum | ||
).fetchone()[0] | ||
else: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.