ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. System architecture and implementation
  2. Migrations
  3. Working with migrations in ORM-CLI

Implementation of a pattern command for migration

Description of columns-manager

Functionality

In my system, a set of classes and design patterns are used for managing database migrations, which interact with each other to ensure the execution and rollback of migration changes. Here’s how it works:

  • MigrationInvoker is responsible for managing the execution and rollback of migration commands. It keeps a history of commands and ensures the execution of new commands and rollback of previous ones through the MigrationInvokerInterface.

  • Templates, such as ColumnDefaultValueOperationsTemplate, define the general template for handling changes in table columns. They implement common algorithms for handling column checks and default values, which can then be specifically customized in derived classes.

  • Operations, such as DeleteDefaultValueFromColumnOperation, implement specific changes to the database schema, such as removing or adding default values. They use template methods to generate the corresponding SQL queries.

  • Commands, such as DeleteDefaultValueCommand, implement the MigrationCommandInterface and provide a wrapper for operations to execute and rollback through MigrationInvoker.

Design Patterns

  • Command Pattern:

    • MigrationInvoker: Implements this pattern by managing the execution and rollback of commands. The class keeps a history of commands and allows their execution or rollback.

    • Operations: Concrete implementations of commands, such as DeleteDefaultValueFromColumnOperation, perform specific actions in the migration.

    • Commands: Classes, such as DeleteDefaultValueCommand, use operations to execute and rollback changes, implementing the MigrationCommandInterface.

  • Template Method Pattern:

    • ColumnDefaultValueOperationsTemplate: These abstract classes implement a general algorithm for handling changes in table columns. They define common steps that can then be customized in specific implementations.

Class Interaction

  • MigrationInvoker manages the execution and rollback of commands. It interacts with commands and operations, ensuring their correct execution and rollback.

  • Templates provide general algorithms for handling changes to the database schema, which are then used by specific operations.

  • Operations implement specific changes and use templates to create SQL queries.

  • Commands wrap operations for their execution and rollback through MigrationInvoker.

This structure provides flexibility and extensibility to the migration system, allowing new types of changes to be easily added and managed.

In my design pattern implementation, the successful use of Command and Template Method patterns is evident through several key aspects:

  • Command Pattern

    • Isolation of Operations:

      • Command: In my implementation, commands such as DeleteDefaultValueCommand are clearly separated from operations. This allows each command (e.g., removing a default value) to have its own way of execution and rollback.

      • Advantage: Such isolation allows changing specific details of command implementations or adding new ones without changing the code that executes these commands.

    • Flexibility and Scalability:

      • Command: Command classes (e.g., DeleteDefaultValueCommand) implement MigrationCommandInterface, providing a clear contract for executing and rolling back operations.

      • Advantage: This makes it easy to add new types of commands or modify existing ones without changing the code that manages command execution (MigrationInvoker).

    • History Management:

      • MigrationInvoker: Stores the history of commands, allowing rollback of previous changes when necessary.

      • Advantage: This simplifies rolling back changes by maintaining a history of all executed commands, which is critical for managing migrations.

  • Template Method Pattern

    • Common Algorithms:

      • Templates: Classes like ColumnDefaultValueOperationsTemplate define common algorithms for handling changes in table columns. Concrete implementations (operations) fill in specific details.

      • Advantage: This avoids code duplication and provides a single way to implement general processes, simplifying maintenance and development.

    • Behavior Specification:

      • Templates: Concrete operations, such as DeleteDefaultValueFromColumnOperation, implement specific behavior for handling changes using the general algorithm of the template.

      • Advantage: This allows easy adaptation of general algorithms to new needs without changing the core structure of the algorithm.

    • Change Management:

      • Templates: Templates handle general parts of the code for changing columns (checks, default values), while concrete implementations provide the details.

      • Advantage: This allows flexible and convenient management of changes to the database schema, simplifying the addition of new types of changes.

General Benefits of Using Patterns

  • Clear Structure: Using patterns helps maintain a clear and understandable code structure, making it easier to extend and maintain.

  • Reduction of Code Duplication: Patterns avoid code duplication and reduce the likelihood of errors by reusing general algorithms and interfaces.

  • Flexibility: Design patterns provide flexibility in adding new features or modifying existing ones without disrupting the overall system structure.

These patterns allow for creating a scalable, maintainable, and flexible system for managing database migrations.

Below is the link to columns-managers

PreviousWorking with migrations in ORM-CLINextQueryBuilder

Last updated 9 months ago

https://github.com/MyroslavShymon/ORM-CLI/tree/main/src/managers/migration-manager/columns-manager