class CQL::SchemaDump

Overview

The SchemaDump class provides functionality to reverse-engineer a database schema and generate a CQL schema definition file by inspecting existing database tables, columns, indexes, and foreign keys.

Example Dumping a SQLite database schema

dumper = CQL::SchemaDump.new(CQL::Adapter::SQLite, "sqlite3://db/myapp.db")
dumper.dump_to_file("src/schemas/myapp_schema.cr", :MyApp, :myapp)

Example Dumping a PostgreSQL database schema

dumper = CQL::SchemaDump.new(CQL::Adapter::Postgres, "postgresql://user:pass@localhost/mydb")
dumper.dump_to_file("src/schemas/production_schema.cr", :ProductionDB, :production_db)

Defined in:

schema_dump.cr

Constant Summary

DEFAULT_SCHEMA_DUMP_FILE_PATH = "./src/schemas/schema.cr"
Log = CQL.config.logger

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(adapter : Adapter, uri : String) #

Initialize the schema dumper

  • @param adapter [Adapter] The database adapter to use
  • @param uri [String] The database connection URI
  • @raise [Error] If connection fails

[View source]

Class Method Detail

def self.from_schema(schema : CQL::Schema) #

Create a SchemaDump instance from an existing CQL::Schema This is useful for migration integration where you already have a schema instance

  • @param schema [CQL::Schema] The existing schema instance
  • @return [SchemaDump] New SchemaDump instance

[View source]
def self.open(schema : CQL::Schema, &) #

Yields a dumper for schema's database and closes its connections afterwards.

  • @return the block's value

[View source]

Instance Method Detail

def adapter : Adapter #

[View source]
def close #

Close the database connection


[View source]
def dump_to_file(file_path : String = DEFAULT_SCHEMA_DUMP_FILE_PATH, schema_name : Symbol = :AppSchema, schema_symbol : Symbol = :app_schema) #

Dump the database schema to a file

  • @param file_path [String] Path where to save the schema file
  • @param schema_name [Symbol] Name for the schema constant
  • @param schema_symbol [Symbol] Symbol name for the schema

[View source]
def generate_schema_content(schema_name : Symbol, schema_symbol : Symbol) : String #

Generate the schema content as a string

  • @param schema_name [Symbol] Name for the schema constant
  • @param schema_symbol [Symbol] Symbol name for the schema
  • @return [String] The generated schema content

[View source]
def uri : String #

[View source]