Skip to content

Commit 2e91e8d

Browse files
Improve migration support using CREATE INDEX ASYNC (#47)
This PR improves migration support by using the `CREATE INDEX ASYNC` syntax in `schema.py`. Changes extracted and modified from #46. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. The changes are detailed below. ### `sql_create_index` Previously defined as: ```python sql_create_index = ( "CREATE INDEX %(name)s ON %(table)s%(using)s " "(%(columns)s)%(include)s%(extra)s%(condition)s" ) ``` Now defined as: ```python sql_create_index = ( "CREATE INDEX ASYNC %(name)s ON %(table)s%(using)s " "(%(columns)s)%(include)s%(extra)s%(condition)s" ) ``` This matches the syntax defined by [Asynchronous indexes in Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-create-index-async.html). ### `sql_create_unique` Previously defined as: ```python sql_create_unique = ( "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s " "UNIQUE%(nulls_distinct)s (%(columns)s)%(deferrable)s" ) ``` Now defined as: ```python sql_create_unique = "CREATE UNIQUE INDEX ASYNC %(name)s ON %(table)s (%(columns)s)" ``` This avoids the `ALTER TABLE ... ADD CONSTRAINT` syntax which is not supported by DSQL according to [Supported subsets of SQL commands in Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-postgresql-compatibility-supported-sql-subsets.html#alter-table-syntax-support). ### General cleanup As part of this change I added some more documentation, and reorganized the position of a few definitions to better isolate concepts. This will make upcoming changes simpler too thanks to the improved consistency.
1 parent c5520e7 commit 2e91e8d

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

aurora_dsql_django/schema.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,38 @@
2222

2323

2424
class DatabaseSchemaEditor(schema.DatabaseSchemaEditor):
25-
# The PostgreSQL backend uses "SET CONSTRAINTS ... IMMEDIATE" before
26-
# "ALTER TABLE..." to run any any deferred checks to allow dropping the
27-
# foreign key in the same transaction. This doesn't apply to Aurora DSQL.
28-
sql_delete_fk = ""
25+
"""
26+
Aurora DSQL schema editor based on the PostgreSQL backend.
27+
28+
Aurora DSQL is PostgreSQL-compatible but supports a subset of PostgreSQL
29+
operations. This class overrides SQL templates and methods to work within
30+
DSQL's constraints.
31+
"""
2932

30-
# ALTER TABLE ADD CONSTRAINT PRIMARY KEY is not supported
31-
sql_create_pk = ""
33+
# Use DSQL's async index creation syntax.
34+
sql_create_index = (
35+
"CREATE INDEX ASYNC %(name)s ON %(table)s%(using)s "
36+
"(%(columns)s)%(include)s%(extra)s%(condition)s"
37+
)
38+
39+
# Create unique constraints as unique indexes instead of using "ALTER TABLE".
40+
sql_create_unique = "CREATE UNIQUE INDEX ASYNC %(name)s ON %(table)s (%(columns)s)"
3241

33-
# "ALTER TABLE ... DROP CONSTRAINT ..." not supported for dropping UNIQUE
34-
# constraints; must use this instead.
42+
# Delete unique constraints by dropping the underlying index.
3543
sql_delete_unique = "DROP INDEX %(name)s CASCADE"
3644

37-
# The PostgreSQL backend uses "SET CONSTRAINTS ... IMMEDIATE" after this
38-
# statement. This isn't supported by Aurora DSQL.
45+
# Remove constraint management from default updates.
3946
sql_update_with_default = (
4047
"UPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULL"
4148
)
4249

43-
# ALTER TABLE ADD CONSTRAINT is not supported
44-
sql_create_unique = ""
45-
46-
# ALTER TABLE ADD CONSTRAINT FOREIGN KEY is not supported
50+
# These "ALTER TABLE" operations are not supported.
51+
sql_create_pk = ""
4752
sql_create_fk = ""
48-
# ALTER TABLE ADD CONSTRAINT CHECK is not supported
53+
sql_delete_fk = ""
4954
sql_create_check = ""
5055
sql_delete_check = ""
51-
# ALTER TABLE DROP CONSTRAINT is not supported
5256
sql_delete_constraint = ""
53-
# ALTER TABLE DROP COLUMN is not supported
5457
sql_delete_column = ""
5558

5659
def __enter__(self):

aurora_dsql_django/tests/unit/test_schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ def setUp(self):
1313
def test_sql_attributes(self):
1414
self.assertEqual(self.schema_editor.sql_delete_fk, "")
1515
self.assertEqual(self.schema_editor.sql_create_pk, "")
16+
self.assertEqual(self.schema_editor.sql_create_index,
17+
"CREATE INDEX ASYNC %(name)s ON %(table)s%(using)s (%(columns)s)%(include)s%(extra)s%(condition)s")
18+
self.assertEqual(self.schema_editor.sql_create_unique,
19+
"CREATE UNIQUE INDEX ASYNC %(name)s ON %(table)s (%(columns)s)")
1620
self.assertEqual(
1721
self.schema_editor.sql_delete_unique,
1822
"DROP INDEX %(name)s CASCADE")
1923
self.assertEqual(
2024
self.schema_editor.sql_update_with_default,
2125
"UPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULL"
2226
)
23-
self.assertEqual(self.schema_editor.sql_create_unique, "")
2427
self.assertEqual(self.schema_editor.sql_create_fk, "")
2528
self.assertEqual(self.schema_editor.sql_create_check, "")
2629
self.assertEqual(self.schema_editor.sql_delete_check, "")

0 commit comments

Comments
 (0)