Skip to content

Commit

Permalink
Added migrations table config value
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsonjohn committed Jan 10, 2025
1 parent c50411f commit 77f88be
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 30 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities

## [5.4.0]- 2025.01.10

### Added

- Added ability to define the name of the database migrations table.

### Changed

- Updated all dependencies.
- Updated documentation.

## [5.3.2]- 2024.12.23

### Added
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ This project is open source and available under the [MIT License](LICENSE).
[Bones App](https://github.com/bayfrontmedia/bones-app) is a simple boilerplate to begin building an application
using the Bones framework.

### Bones API

[Bones API](https://github.com/bayfrontmedia/bones-api) is a starter kit to begin building an API using the Bones framework.

### Bones Web
[Bones Web](https://github.com/bayfrontmedia/bones-web) is a boilerplate to begin building a web application
using the Bones framework.

[Bones Web](https://github.com/bayfrontmedia/bones-web) is a starter kit to begin building a web application
with [Tailwind](https://tailwindcss.com/) using the Bones framework.

## Documentation

Expand Down
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/services/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class ExampleAppFilters extends FilterSubscriber implements FilterSubscriberInte
}
```

The required `migrations` database table will be created the first time the `php bones migrate:up` command is used.
The name of the table used for migrations can be defined using a `migrations_table` [config value](../usage/config.md).
If not defined, the table name `migrations` will be used by default.

> **NOTE:** Be sure to back up your database before running any migrations
Expand Down
9 changes: 6 additions & 3 deletions src/Application/Kernel/Console/Commands/MigrateDown.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bayfront\Bones\Application\Kernel\Console\Commands;

use Bayfront\Bones\Application\Services\Filters\FilterService;
use Bayfront\Bones\Application\Utilities\App;
use Bayfront\Bones\Interfaces\MigrationInterface;
use Bayfront\Container\Container;
use Bayfront\SimplePdo\Db;
Expand Down Expand Up @@ -55,6 +56,8 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{

$migrations_table = App::getConfig('app.migrations_table', 'migrations');

$db = (string)$input->getOption('db');

if ($db !== '') {
Expand All @@ -67,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// No batch provided. Get last batch number.

$batch = $this->db->single("SELECT MAX(batch) AS max FROM `migrations`");
$batch = $this->db->single("SELECT MAX(batch) AS max FROM $migrations_table");

if (!$batch) {

Expand All @@ -78,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

}

$ran_migrations = $this->db->select("SELECT id, name, batch FROM `migrations` WHERE batch >= :batch ORDER BY id DESC", [
$ran_migrations = $this->db->select("SELECT id, name, batch FROM $migrations_table WHERE batch >= :batch ORDER BY id DESC", [
'batch' => $batch
]);

Expand Down Expand Up @@ -159,7 +162,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Remove from migrations table

$this->db->delete('migrations', [
$this->db->delete($migrations_table, [
'id' => $ran['id']
]);

Expand Down
11 changes: 7 additions & 4 deletions src/Application/Kernel/Console/Commands/MigrateUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bayfront\Bones\Application\Kernel\Console\Commands;

use Bayfront\Bones\Application\Services\Filters\FilterService;
use Bayfront\Bones\Application\Utilities\App;
use Bayfront\Bones\Interfaces\MigrationInterface;
use Bayfront\Container\Container;
use Bayfront\SimplePdo\Db;
Expand Down Expand Up @@ -54,6 +55,8 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{

$migrations_table = App::getConfig('app.migrations_table', 'migrations');

$migrations = $this->filters->doFilter('bones.migrations', []);

if (empty($migrations)) {
Expand All @@ -71,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Ensure database table exists.

$this->db->query("CREATE TABLE IF NOT EXISTS `migrations` (
$this->db->query("CREATE TABLE IF NOT EXISTS $migrations_table (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(255) NOT NULL,
`batch` int NOT NULL,
Expand All @@ -90,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

if ($this->db->exists('migrations', [
if ($this->db->exists($migrations_table, [
'name' => $migration->getName()
])) {
unset($migrations[$k]);
Expand All @@ -107,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Get next batch number

$batch_num = $this->db->single("SELECT MAX(batch) AS max FROM `migrations`");
$batch_num = $this->db->single("SELECT MAX(batch) AS max FROM $migrations_table");

if (!$batch_num) {
$batch_num = 1;
Expand Down Expand Up @@ -155,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Add to migrations table

$this->db->insert('migrations', [
$this->db->insert($migrations_table, [
'name' => $migration->getName(),
'batch' => $batch_num
]);
Expand Down

0 comments on commit 77f88be

Please sign in to comment.