Automatic migration Tool:
charybdis-migrate tool that enables automatic migration to database without need to write
migrations by hand. It expects src/models files and generates migrations based on differences
between model definitions and database.
Announcements:
-
Breaking changes:
- As of 0.4.3 version
local_secondary_indexesare now defined as list of fields. Partition key part is derived frompartition_keyspart of macro declaration and each element in array will result with new local index.
- As of 0.4.3 version
Installation
Usage
)
Automatic migration
-
charybdis-migrateenables automatic migration to database without need to write migrations by hand. It expectssrc/modelsfiles and generates migrations based on differences between model definitions and database. It supports following operations:-
Create new tables
-
Create new columns
-
Drop columns
-
Change field types (drop and recreate column
--drop-and-replaceflag) -
Create secondary indexes
-
Drop secondary indexes
-
Create UDTs (
src/models/udts) -
Create materialized views (
src/models/materialized_views) -
Table options
⚠️ If table exists, table options will result in alter table query that without
CLUSTERING ORDERandCOMPACT STORAGEoptions.Model dropping is not added. If you don't define model within
src/modeldir it will leave db structure as it is.
-
-
Running migration
⚠️ If you are working with existing datasets, before running migration you need to make sure that your **model ** definitions structure matches the database in respect to table names, column names, column types, partition keys, clustering keys and secondary indexes so you don't alter structure accidentally. If structure is matched, it will not run any migrations. As mentioned above, in case there is no model definition for table, it will not drop it. In future, we will add
modelizecommand that will generatesrc/modelsfiles from existing data source. -
Global secondary indexes
If we have model:
resulting query will be:
CREATE INDEX ON users (username); -
Local secondary Indexes
Indexes that are scoped to the partition key
resulting query will be:
CREATE INDEX ON menus((location), dish_type);
Define Tables
- Declare model as a struct within
src/modelsdir:// src/models/user.rs use charybdis_model; use ;
(Note we use src/models as automatic migration tool expects that dir)
Define UDT
src/models/udts// src/models/udts/address.rs use charybdis_udt_model; use Text;
Define Materialized Views
-
src/models/materialized_views// src/models/materialized_views/users_by_username.rs use charybdis_view_model; use ;Resulting auto-generated migration query will be:
CREATE MATERIALIZED VIEW IF NOT EXISTS users_by_email AS SELECT created_at, updated_at, username, email, id FROM users WHERE email IS NOT NULL AND id IS NOT NULL PRIMARY KEY (email, id)