-
Hi, one of the missing details that I've found when playing with Piccolo, is the inability to create and maintain composite (multiple columns/fields) indexes when defining tables. I've been reading #984 , #172 , #175 and haven't found any way to do so (other than using raw sql in migrations). In the other side, I've seen that there are some "internal" functions providing composite index creation and deletion (only not-unique ones, but that's better than nothing). So I've tried this simple migration script. The problem is that I get Here it is the testing migration code:
When I run I've tried some basic debugging, adding logging lines here and there and, somehow, it seems that the Am I missing anything? (note I'm completely new to Piccolo, so I may be missing something obvious!) It would really great to support (composite, both normal and unique) index managing from tables definition, wow! TIA and ciao :-) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
@stronk7 Yes, you are right. I think the problem is in this line, because with that line
I played around with this a bit and made these changes to the source code. # all previous code is unchanged
async def backwards():
manager = MigrationManager(migration_id=ID, app_name="{{ app_name }}", description=DESCRIPTION)
def run():
print(f"running {ID}")
manager.add_raw(run)
return manager Second, I change these lines response = await migration_module.backwards()
if isinstance(response, MigrationManager):
if self.preview:
response.preview = True
await response.run() After that I was able to drop the indexes using |
Beta Was this translation helpful? Give feedback.
-
Many thanks for your explanations, @sinisaos. I've (blindly - I certainly don't know what I'm doing!) applied your suggested changes and can confirm that, now, the backwards code is being executed ok (and the index is being dropped, as expected).
Two notes and a question:
And the question, should this be reported as bug? Does it affect to ALL backwards migrations, or only "particular" ones, like my one above? Again, many thanks for the help, ciao :-) |
Beta Was this translation helpful? Give feedback.
-
hello all, I also did the exact same way implementing the looks like nesting |
Beta Was this translation helpful? Give feedback.
-
@Jacky56 Yes, you are right. I didn't see it at all. Everything works with from piccolo.apps.migrations.auto.migration_manager import MigrationManager
from home.tables import TestTable
ID = "2024-11-10T12:04:39:118866"
VERSION = "1.22.0"
DESCRIPTION = ""
async def forwards():
manager = MigrationManager(
migration_id=ID, app_name="", description=DESCRIPTION
)
async def run():
await TestTable.create_index(
columns = ["col1", "col2"], # or ([TestTable.col1, TestTable.col2])
if_not_exists = True
).run()
manager.add_raw(run)
# method for backwards migration
async def run_backwards():
await TestTable.drop_index(
columns=["col1", "col2"], # or ([TestTable.col1, TestTable.col2])
if_exists=True
).run()
manager.add_raw_backwards(run_backwards)
return manager and everything works :). Creating indexes with testdb=# \d test_table
Table "public.test_table"
Column | Type | Collation | Nullable | Default
--------+------------------------+-----------+----------+----------------------------------------
id | integer | | not null | nextval('test_table_id_seq'::regclass)
col1 | character varying(255) | | not null | ''::character varying
col2 | character varying(255) | | not null | ''::character varying
Indexes:
"test_table_pkey" PRIMARY KEY, btree (id)
"test_table_col1_col2" btree (col1, col2) <-- created indexes
Dropping indexes: piccolo migrations backwards home
Reverse 1 migration? [y/N] y
- 2024-11-10T12:04:39:118866 [backwards]... ok! ✔️ Result is: testdb=# \d test_table
Table "public.test_table"
Column | Type | Collation | Nullable | Default
--------+------------------------+-----------+----------+----------------------------------------
id | integer | | not null | nextval('test_table_id_seq'::regclass)
col1 | character varying(255) | | not null | ''::character varying
col2 | character varying(255) | | not null | ''::character varying
Indexes:
"test_table_pkey" PRIMARY KEY, btree (id)
# indexes dropped |
Beta Was this translation helpful? Give feedback.
@stronk7 Yes, you are right. I think the problem is in this line, because with that line
piccolo/piccolo/apps/migrations/commands/backwards.py
Line 90 in 448a818
forwards
method is always called even if we usebackwards
in thecli
. For now I can only drop the indexes if I make another empty migration and passTable.drop_index
in theforwards
method.I played around with this a bit and made these changes to the source code.
First, I added a
backwards
method to Jinja template like this