AshScylla.Migration (AshScylla v0.4.0)

Copy Markdown View Source

CQL schema generation helpers for ScyllaDB.

This module generates raw CQL DDL statements (CREATE TABLE, CREATE INDEX, CREATE TYPE, etc.) from Ash resource definitions. It is NOT an Ecto SQL migration runner — CQL has no transactional DDL concept.

These helpers return CQL strings that you execute via Ecto.Migration.execute/1 in your migration modules, or directly through your repo at runtime.

Example Migration

defmodule MyApp.Repo.Migrations.CreateUsers do
  use Ecto.Migration

  def change do
    AshScylla.Migration.create_table_cql(MyApp.User)
    |> then(&execute/1)
  end
end

Important Note on create_table_cql/1

This function reads compile-time module attributes set by the Ash resource DSL. For runtime use, prefer Ash.Resource.Info.attributes/1 combined with AshScylla.DataLayer.Dsl.table/1.

Summary

Functions

Generates CQL CREATE INDEX statements for secondary indexes.

Generates a CQL CREATE TABLE statement for an Ash resource.

Define a User Defined Type (UDT) in ScyllaDB.

Generates a CQL DROP INDEX statement for a secondary index.

Drop a User Defined Type (UDT) in ScyllaDB.

Returns the keyspace for a resource if configured via DSL. Note: This is a placeholder for future DSL implementation.

Functions

create_secondary_indexes_cql(resource)

@spec create_secondary_indexes_cql(module()) :: [String.t()]

Generates CQL CREATE INDEX statements for secondary indexes.

Returns a list of CQL strings that should be executed in migrations.

Example

defmodule MyApp.Repo.Migrations.CreateUserIndexes do
  use Ecto.Migration

  def change do
    AshScylla.Migration.create_secondary_indexes_cql(MyApp.User)
    |> Enum.each(&execute/1)
  end
end

create_table_cql(resource)

@spec create_table_cql(module()) :: String.t()

Generates a CQL CREATE TABLE statement for an Ash resource.

Returns a raw CQL string. Execute it in a migration via Ecto.Migration.execute/1 or directly through your repo at runtime.

create_type(type_name, list)

@spec create_type(
  String.t(),
  keyword()
) :: String.t()

Define a User Defined Type (UDT) in ScyllaDB.

Example

create_type "full_name" do
  field :first_name, :text
  field :last_name, :text
end

This generates:

CREATE TYPE full_name (first_name TEXT, last_name TEXT)

drop_secondary_index_cql(resource, index_name)

@spec drop_secondary_index_cql(module(), String.t()) :: String.t()

Generates a CQL DROP INDEX statement for a secondary index.

drop_type(type_name)

@spec drop_type(String.t()) :: String.t()

Drop a User Defined Type (UDT) in ScyllaDB.

keyspace(resource)

@spec keyspace(module()) :: String.t() | nil

Returns the keyspace for a resource if configured via DSL. Note: This is a placeholder for future DSL implementation.