You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
table.transform() method for advanced alter table #114
Ideally this would all happen in a single transaction, such that other processes talking to the database would not see any inconsistent state while the table copy was taking place. Need to confirm that this is possible. Also refs transactions thoughts in #121.
I'm not so keen on that chained API - it's pretty complicated.
Here's an idea for a much simpler interface. Essentially it lets you say "take table X and migrate its contents to a new table with this structure - then atomically rename the tables to switch them":
Since neither the term "transform" or "migrate" are used in the codebase at the moment, I think I'll go with .transform_table() - that leaves the term "migrate" available for any future database migrations system (similar to Django's).
This requires the caller to provide the exact set of columns for the new table.
It would be useful if this was optional - if you could omit the columns and have it automatically use the previous columns. This would let you change things like the primary key or the column order using the other arguments.
Even better: allow column renaming using an optional rename={...} argument:
Does it make sense to support the pk= argument for changing the primary key?
If the user requests a primary key that doesn't make sense I think an integrity error will be raised when the SQL is being executed, which should hopefully cancel the transaction and raise an error. Need to check that this is what happens.
I think the fiddliest part of the implementation here is code that takes the existing columns_dict of the table and the incoming columns= and drop= and rename= parameters and produces the columns dictionary for the new table, ready to be fed to .create_table().
This logic probably also needs to return a structure that can be used to build the INSERT INTO ... SELECT ... FROM query.
The reason I'm working on this now is that I'd like to support many more options for data cleanup in the Datasette ecosystem - so being able to do things like convert the type of existing columns becomes increasingly important.
Activity
simonw commentedon May 30, 2020
Idea: use a chained API to define a complex transition and then execute it all at once. For example:
simonw commentedon Jul 8, 2020
Ideally this would all happen in a single transaction, such that other processes talking to the database would not see any inconsistent state while the table copy was taking place. Need to confirm that this is possible. Also refs transactions thoughts in #121.
simonw commentedon Jul 8, 2020
I'm not so keen on that chained API - it's pretty complicated.
Here's an idea for a much simpler interface. Essentially it lets you say "take table X and migrate its contents to a new table with this structure - then atomically rename the tables to switch them":
The
migrate_table()method would take the same exact signature as thetable.create()method:sqlite-utils/sqlite_utils/db.py
Lines 615 to 625 in a236a6b
simonw commentedon Jul 8, 2020
Alternative possible names:
.transform_table().migrate().transform()I'm torn between
.migrate_table()and.transform_table().simonw commentedon Jul 8, 2020
Since neither the term "transform" or "migrate" are used in the codebase at the moment, I think I'll go with
.transform_table()- that leaves the term "migrate" available for any future database migrations system (similar to Django's).simonw commentedon Jul 8, 2020
Don't forget this step:
simonw commentedon Jul 8, 2020
Thinking about the method signature:
This requires the caller to provide the exact set of columns for the new table.
It would be useful if this was optional - if you could omit the columns and have it automatically use the previous columns. This would let you change things like the primary key or the column order using the other arguments.
Even better: allow column renaming using an optional
rename={...}argument:simonw commentedon Jul 8, 2020
I can have a convenient
change_type={...}parameter for changing column types too.simonw commentedon Jul 8, 2020
Work in progress: not quite right yet, I need smarter logic for how renamed columns are reflected in the generated
INSERT INTO ... SELECT ...query:[-]Advanced alter table support[/-][+]table.transform_table() method for advanced alter table[/+]simonw commentedon Jul 8, 2020
According to https://www.sqlite.org/lang_altertable.html#making_other_kinds_of_table_schema_changes the hardest bits to consider are how to deal with existing foreign key relationships, triggers and views.
I'm OK leaving views as an exercise for the caller - many of these transformations may not need any view changes at all.
Foreign key relationships are important: it should handle these automatically as effectively as possible.
Likewise trigger changes: need to think about what this means.
simonw commentedon Jul 9, 2020
I'm going to add a second method
.transform_table_sql(...)- which returns the SQL that would have been executed but does NOT execute it.Advanced callers can use this to include their own additional steps in the same transaction - e.g. recreating views or triggers.
More importantly it gives me a useful hook for writing some unit tests against the generated SQL.
simonw commentedon Jul 27, 2020
Work in progress in
transformbranch here: https://github.com/simonw/sqlite-utils/tree/transform11 remaining items
simonw commentedon Sep 21, 2020
The
columns=argument is optional - so you can do just a rename operation like so:simonw commentedon Sep 21, 2020
Since I have a
column_order=Noneargument already, maybe I can ignore the order of the columns in that first argument and use that instead?simonw commentedon Sep 21, 2020
Does it make sense to support the
pk=argument for changing the primary key?If the user requests a primary key that doesn't make sense I think an integrity error will be raised when the SQL is being executed, which should hopefully cancel the transaction and raise an error. Need to check that this is what happens.
simonw commentedon Sep 21, 2020
A test that confirms that this mechanism can turn a
rowidinto a non-rowid table would be good too.simonw commentedon Sep 21, 2020
I think the fiddliest part of the implementation here is code that takes the existing
columns_dictof the table and the incomingcolumns=anddrop=andrename=parameters and produces the columns dictionary for the new table, ready to be fed to.create_table().This logic probably also needs to return a structure that can be used to build the
INSERT INTO ... SELECT ... FROMquery.New, improved implementation of transform(), refs #114
simonw commentedon Sep 22, 2020
The reason I'm working on this now is that I'd like to support many more options for data cleanup in the Datasette ecosystem - so being able to do things like convert the type of existing columns becomes increasingly important.
.transform(drop_foreign_keys=...), refs #114
Documentation for table.transform(), refs #114
table.transform() method - closes #114
simonw commentedon Sep 22, 2020
Documentation: https://sqlite-utils.readthedocs.io/en/latest/python-api.html#transforming-a-table
Release 2.20