VOOZH about

URL: https://deepwiki.com/hypervel/devtool/6.2-session-table-migration

⇱ Session Table Migration | hypervel/devtool | DeepWiki


Loading...
Menu

Session Table Migration

Purpose and Scope

This document explains the make:session-table command, which generates a database migration file for creating a sessions table. This table is used to store session data in the database when using database-backed session storage in a Hypervel application.

For information about other database infrastructure table migrations, see Database Migration Generators. For model-related generation, see Model System Generators.

Command Overview

The SessionTableCommand class provides the make:session-table command (with alias session:table) to scaffold a migration file that creates the sessions table structure required for database session storage.

AspectDetails
Command Namemake:session-table
Aliassession:table
Base ClassHyperf\Devtool\Generator\GeneratorCommand
Generated Filedatabase/migrations/YYYY_MM_DD_000000_create_sessions_table.php
Stub Templatesrc/Generator/stubs/sessions-table.stub

Sources: src/Generator/SessionTableCommand.php13-26

Command Registration and Invocation

The command is registered through the ConfigProvider as part of the 30 commands in the devtool package. Developers invoke it via CLI:


Or using the alias:



Sources: src/Generator/SessionTableCommand.php28-56

Command Options

The command accepts the following options:

OptionShortTypeDescription
--path-pOptionalCustom path for the migration file (default: database/migrations/{timestamp}_create_sessions_table.php)
--forceFlagOverwrite existing migration file

Additional options inherited from GeneratorCommand base class are also available.

Sources: src/Generator/SessionTableCommand.php73-78

Generated Migration Structure

The command generates a migration file with a timestamped filename format. The migration uses modern PHP syntax and Hypervel's migration components.

Migration File Structure


Sources: src/Generator/stubs/sessions-table.stub9-32

Key Components

The generated migration includes:

  1. Namespace declarations: Uses Hyperf\Database\Schema\Blueprint, Hypervel\Database\Migrations\Migration, and Hypervel\Support\Facades\Schema
  2. Anonymous class: Extends Migration base class
  3. Up method: Creates the sessions table with schema definition
  4. Down method: Drops the sessions table

Sources: src/Generator/stubs/sessions-table.stub1-8

Session Table Schema

The sessions table schema is designed to store all necessary session data for web applications:


Column Specifications

ColumnTypeConstraintsPurpose
idstringPRIMARY KEYUnique session identifier
user_idforeignIdNULLABLE, INDEXEDLinks session to authenticated user
ip_addressstring(45)NULLABLEIPv4 or IPv6 address (45 chars supports full IPv6)
user_agenttextNULLABLEBrowser/client identification string
payloadlongTextREQUIREDSerialized session data storage
last_activityintegerINDEXEDUnix timestamp for session expiration

The schema supports both authenticated and guest sessions through the nullable user_id field. The last_activity index enables efficient session cleanup queries.

Sources: src/Generator/stubs/sessions-table.stub15-21

Command Execution Flow

The command execution follows a specific sequence to ensure safe file generation:


Sources: src/Generator/SessionTableCommand.php28-56

Execution Steps

  1. Filename Generation src/Generator/SessionTableCommand.php33: Creates timestamp-based filename using Carbon::now()->format('Y_m_d_000000')
  2. Path Resolution src/Generator/SessionTableCommand.php34: Uses --path option or defaults to database/migrations/{filename}
  3. Existence Check src/Generator/SessionTableCommand.php39-42: Verifies if file exists (unless --force is used)
  4. Directory Creation src/Generator/SessionTableCommand.php47: Ensures parent directory structure exists
  5. Stub Loading src/Generator/SessionTableCommand.php49: Retrieves stub content from configured path or default location
  6. File Writing src/Generator/SessionTableCommand.php49: Writes stub content directly to target path
  7. IDE Integration src/Generator/SessionTableCommand.php53: Opens generated file in configured IDE

Sources: src/Generator/SessionTableCommand.php28-56

Stub Template System

Unlike many other generator commands that perform placeholder replacements, the SessionTableCommand copies the stub file directly without modifications. The stub is a complete, ready-to-use migration file.

Stub Resolution


The stub path can be customized through configuration, allowing applications to override the default session table structure if needed.

Sources: src/Generator/SessionTableCommand.php58-61

Implementation Details

Key Methods

MethodPurposeReturn Type
execute()Main command execution logicint
getStub()Returns path to stub templatestring
alreadyExists()Checks if migration file existsbool
getArguments()Returns command arguments (empty for this command)array
getOptions()Returns command optionsarray
getDefaultNamespace()Returns default namespace (empty for migrations)string

Sources: src/Generator/SessionTableCommand.php28-84

Unique Characteristics

  1. No Placeholder Replacement: Unlike most generators, this command performs a direct file copy without string replacements src/Generator/SessionTableCommand.php49
  2. Timestamp Format: Uses 000000 for the time portion, allowing multiple infrastructure migrations with the same date src/Generator/SessionTableCommand.php33
  3. Empty Namespace: Returns empty string from getDefaultNamespace() since migrations use anonymous classes src/Generator/SessionTableCommand.php80-83
  4. No Arguments: The command accepts no positional arguments, only options src/Generator/SessionTableCommand.php68-71

Sources: src/Generator/SessionTableCommand.php33-83

Integration with Session System

The generated migration provides the database foundation for session storage. When configured to use database sessions, the Hypervel framework will:

  1. Store session data as serialized strings in the payload column
  2. Track user associations via the user_id foreign key
  3. Record client information in ip_address and user_agent columns
  4. Maintain activity timestamps in last_activity for garbage collection
  5. Use the id column as the session identifier in cookies/headers

The indexed columns (user_id and last_activity) optimize common query patterns for session retrieval and cleanup operations.

Sources: src/Generator/stubs/sessions-table.stub15-21