PdbSync
in package
Provides a system for syncing a database to a database definition.
The database definition is stored in one or more XML files, which get merged together before the sync is done.
Contains code that may be MySQL specific.
Table of Contents
Constants
- QUERY_TYPES : array<string|int, string> = ['alter_table', 'alter_column', 'rename_table',...
- Types of queries generated by running a sync, in the order that they should be run.
Properties
- $fixes : array<string, array<string|int, SyncFix>>
- $heading : string
- Temporarily stores heading to attach to next query generated.
- $pdb : Pdb
- $queries : array<string, array<string|int, SyncQuery>>
- $warnings : array<string|int, string>
Methods
- __construct() : mixed
- checkConnPermissions() : array<string|int, string>|true
- Are the permissions of the current user adequate?
- execute() : PdbLog
- Execute the stored queries.
- getMigration() : array<string|int, string>
- Create a dump of SQL that can be executed later.
- getQueries() : iterable<string|int, SyncQuery>
- Get the stored queries.
- hasQueries() : bool
- Does this sync have queries to execute?
- migrate() : void
- Process the diff between the real database and the provided schema from the PdbParser object.
- migrateTables() : void
- Prepare a set of ALTER TABLE statements which will sync the database to the provided layout.
- migrateViews() : void
- Prepare queries for creating (or replacing) views.
- updateDatabase() : PdbLog
- Migrate and execute the sync - all in one.
- changePrimary() : bool
- Changes the primary key of a table
- checkColumnMatches() : bool
- Checks that the specified column exists in the specified table If the column is incorrect, it will be altered If the column does not exist, it will be created
- checkForeignKeyMatches() : bool
- Checks that the specified foreign key exists in the table
- checkIndexMatches() : bool
- Checks that the specified index exists in the table.
- checkPrimaryMatches() : bool
- Checks that the specified primary key is correct for the table
- checkRemovedColumns() : void
- Find and remove unused columns.
- checkRemovedForeignKeys() : void
- Find and remove unused foreign keys
- checkRemovedIndexes() : void
- Find and remove unused indexes.
- checkTableMatches() : bool
- Checks that the specified table exists If the table does not exist, it will be created.
- createSqlColumnSpec() : string
- Generates the SQL that should be used for a column spec
- createTable() : bool
- Creates a table which matches the specified definition
- createView() : void
- Create or update a view
- storeFix() : void
- Store a 'fix query'.
- storeQuery() : void
- Stores a query in a list of queries to run.
- storeWarning() : void
- User messages.
Constants
QUERY_TYPES
Types of queries generated by running a sync, in the order that they should be run.
public
array<string|int, string>
QUERY_TYPES
= ['alter_table', 'alter_column', 'rename_table', 'rename_col', 'drop_fk', 'drop_index', 'drop_column', 'add_table', 'insert_record', 'add_column', 'add_index', 'alter_pk', 'add_fk', 'views']
Properties
$fixes
private
array<string, array<string|int, SyncFix>>
$fixes
= []
type => fixes[]
$heading
Temporarily stores heading to attach to next query generated.
private
string
$heading
= 'TBA'
$pdb
private
Pdb
$pdb
$queries
private
array<string, array<string|int, SyncQuery>>
$queries
= []
type => queries[]
$warnings
private
array<string|int, string>
$warnings
= []
Methods
__construct()
public
__construct(Pdb|PdbConfig|array<string|int, mixed> $config) : mixed
Parameters
checkConnPermissions()
Are the permissions of the current user adequate?
public
checkConnPermissions() : array<string|int, string>|true
Return values
array<string|int, string>|true —true on success, or an array of missing permissions on failure.
execute()
Execute the stored queries.
public
execute([bool $act = true ]) : PdbLog
This returns a 'pdb log' that can be converted to whichever format you please. The PdbLog::print() method has a sample implementation.
Parameters
- $act : bool = true
-
Whether to execute the queries.
Return values
PdbLoggetMigration()
Create a dump of SQL that can be executed later.
public
getMigration() : array<string|int, string>
Return values
array<string|int, string>getQueries()
Get the stored queries.
public
getQueries() : iterable<string|int, SyncQuery>
Return values
iterable<string|int, SyncQuery>hasQueries()
Does this sync have queries to execute?
public
hasQueries() : bool
Return values
boolmigrate()
Process the diff between the real database and the provided schema from the PdbParser object.
public
migrate(PdbSchemaInterface $schema[, SyncActions|array<string|int, mixed>|null $do = null ]) : void
This will populate the internal queries.
- Get them with getQueries()
- Store them for later with getMigration()
- Process with execute()
Optionally limit the migration actions with the 'do' parameter. SyncActions
Parameters
- $schema : PdbSchemaInterface
- $do : SyncActions|array<string|int, mixed>|null = null
-
'create'- create table, update table attributes'primary'- update primary key'column'- create/modify columns'index'- update indexes'foreign_key'- update constraints'remove'- remove columns'views'- process views
Tags
migrateTables()
Prepare a set of ALTER TABLE statements which will sync the database to the provided layout.
public
migrateTables(array<string|int, PdbTable> $tables[, SyncActions|array<string|int, mixed>|null $do = null ]) : void
Parameters
- $tables : array<string|int, PdbTable>
- $do : SyncActions|array<string|int, mixed>|null = null
-
'create'- create table, update table attributes'primary'- update primary key'column'- create/modify columns'index'- update indexes'foreign_key'- update constraints'remove'- remove columns'views'- process views
Tags
migrateViews()
Prepare queries for creating (or replacing) views.
public
migrateViews(array<string|int, string|PdbView> $views) : void
Parameters
- $views : array<string|int, string|PdbView>
-
[name => SQL]
Tags
updateDatabase()
Migrate and execute the sync - all in one.
public
updateDatabase(PdbSchemaInterface $schema[, SyncActions|array<string|int, mixed>|null $do = null ]) : PdbLog
This is shorthand for migrate() and execute().
This returns a 'pdb log' that can be converted to whichever format you please. The PdbLog::print method has a sample implementation.
Parameters
- $schema : PdbSchemaInterface
- $do : SyncActions|array<string|int, mixed>|null = null
-
'create'- create table, update table attributes'primary'- update primary key'column'- create/modify columns'index'- update indexes'foreign_key'- update constraints'remove'- remove columns'views'- process views
Tags
Return values
PdbLogchangePrimary()
Changes the primary key of a table
private
changePrimary(PdbTable $table) : bool
Parameters
- $table : PdbTable
Return values
boolcheckColumnMatches()
Checks that the specified column exists in the specified table If the column is incorrect, it will be altered If the column does not exist, it will be created
private
checkColumnMatches(PdbTable $table, PdbColumn $column, string $prev_column) : bool
Parameters
- $table : PdbTable
-
The table to check
- $column : PdbColumn
-
The column definition to check
- $prev_column : string
-
The name of the previous column, for column positioning
Return values
boolcheckForeignKeyMatches()
Checks that the specified foreign key exists in the table
private
checkForeignKeyMatches(string $table_name, PdbForeignKey $foreign_key, bool $check_fixes) : bool
- If the foreign key is incorrect, it will be altered
- If the foreign key does not exist, it will be created
Parameters
- $table_name : string
-
The name of the table to check
- $foreign_key : PdbForeignKey
-
The foreign key definition to check
- $check_fixes : bool
Return values
boolcheckIndexMatches()
Checks that the specified index exists in the table.
private
checkIndexMatches(string $table_name, PdbIndex $index) : bool
- If the index is incorrect, it will be altered
- If the index does not exist, it will be created
Parameters
- $table_name : string
-
The name of the table to check
- $index : PdbIndex
-
The index definition to check
Return values
boolcheckPrimaryMatches()
Checks that the specified primary key is correct for the table
private
checkPrimaryMatches(PdbTable $table) : bool
Parameters
- $table : PdbTable
Return values
boolcheckRemovedColumns()
Find and remove unused columns.
private
checkRemovedColumns(string $table_name, array<string|int, PdbColumn> $defined) : void
Parameters
- $table_name : string
- $defined : array<string|int, PdbColumn>
checkRemovedForeignKeys()
Find and remove unused foreign keys
private
checkRemovedForeignKeys(string $table_name, array<string|int, PdbForeignKey> $defined) : void
Parameters
- $table_name : string
- $defined : array<string|int, PdbForeignKey>
checkRemovedIndexes()
Find and remove unused indexes.
private
checkRemovedIndexes(string $table_name, array<string|int, PdbIndex> $defined) : void
Parameters
- $table_name : string
- $defined : array<string|int, PdbIndex>
checkTableMatches()
Checks that the specified table exists If the table does not exist, it will be created.
private
checkTableMatches(PdbTable $table) : bool
Parameters
- $table : PdbTable
-
The table definition
Return values
bool —False if the table did not exist and was just then created, true otherwise
createSqlColumnSpec()
Generates the SQL that should be used for a column spec
private
createSqlColumnSpec(PdbTable $table, PdbColumn $column) : string
The spec goes in the following location in the query:
ALTER TABLE pizzas ADD COLUMN toppings
Parameters
Return values
stringcreateTable()
Creates a table which matches the specified definition
private
createTable(PdbTable $table) : bool
Parameters
- $table : PdbTable
Return values
boolcreateView()
Create or update a view
private
createView(PdbView $view) : void
Parameters
- $view : PdbView
storeFix()
Store a 'fix query'.
private
storeFix(string $key, SyncFix|array<string|int, mixed> $fix) : void
These are queries that require user input.
TODO Hey we could actually execute these automatically. With like a 'force' option. Some would have to be marked 'before' and 'after' actions. For the FK delete/null - there would need to be a default.
Parameters
- $key : string
- $fix : SyncFix|array<string|int, mixed>
storeQuery()
Stores a query in a list of queries to run.
private
storeQuery(string $type, string $query[, string $message = '' ]) : void
TODO $message isn't used anywhere. Perhaps we could fold in fixes and warnings here. Like, temp store fixes/warnings, copy into the stored query and clear the temp.
TODO Should throw if the 'heading' in missing.
Parameters
- $type : string
-
One of QUERY_TYPES
- $query : string
- $message : string = ''
Tags
storeWarning()
User messages.
private
storeWarning(string|array<string|int, string> $warnings) : void
Display these after the SQL log.
Parameters
- $warnings : string|array<string|int, string>