Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
15c0e70
Remove unsupported spatial column types
kitloong Feb 27, 2025
f6a162b
Fix version compare
kitloong Feb 27, 2025
b0d526a
Merge pull request #263 from kitloong/feature/spatial
kitloong Feb 27, 2025
6bb1f52
Remove unused methods
kitloong Feb 27, 2025
ae63d60
Use `defineEnvironment`
kitloong Feb 27, 2025
0a55612
Add tests
kitloong Feb 27, 2025
f30badc
Merge pull request #264 from kitloong/feature/improve
kitloong Feb 27, 2025
f955e64
Create documentation
kitloong Dec 4, 2024
d47f6bc
Ignore mkdocs.yml
kitloong Mar 1, 2025
698e053
Update README.md
kitloong Mar 1, 2025
0422fb2
Merge pull request #265 from kitloong/feature/docs
kitloong Mar 1, 2025
34f696c
Update gtag
kitloong Mar 1, 2025
d185218
Merge pull request #266 from kitloong/feature/mkdoc
kitloong Mar 1, 2025
4a95566
Add meta description
kitloong Mar 3, 2025
c2d4887
Merge pull request #267 from kitloong/feature/docs
kitloong Mar 3, 2025
0824550
Add stale action
kitloong Mar 3, 2025
3012ddd
Merge pull request #268 from kitloong/feature/stale
kitloong Mar 4, 2025
cd787e2
Add sponsor
kitloong Mar 11, 2025
a2f0c45
Merge pull request #270 from kitloong/feature/sponsor
kitloong Mar 11, 2025
660d64d
Default value is now escaped properly in Laravel v12.20.0 https://git…
kitloong Jul 21, 2025
f592594
Merge pull request #279 from kitloong/feature/default
kitloong Jul 21, 2025
0b99e76
Fix #276 Parse enum constraint correctly
kitloong Aug 5, 2025
37f29a9
Merge pull request #280 from kitloong/feature/enum
kitloong Aug 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/docs export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.phpmd.xml export-ignore
/check_migrations.sh export-ignore
/mkdocs.yml export-ignore
/phpcs.xml export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Generate documentation"

on:
push:
branches:
- 7.x

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: write
pages: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mkdocs

- name: Deploy
run: mkdocs gh-deploy --force
15 changes: 15 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "Mark stale issues and pull requests"

on:
schedule:
- cron: '0 0 * * *'

permissions:
issues: write
pull-requests: write

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v8
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/migrations
/storage
/vendor
/venv
/site
.php-cs-fixer.cache
.php_cs.cache
.phpstorm.meta.php
Expand Down
84 changes: 34 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

Generate Laravel Migrations from an existing database, including indexes and foreign keys!

This package is a modified version of https://github.com/Xethron/migrations-generator that has been updated to support Laravel 5.6 and beyond, along with additional features.
## Documentation

Checkout the [documentation](https://kitloong.github.io/laravel-migrations-generator/usage/) for more details.

## Supported Database

Expand Down Expand Up @@ -80,7 +82,7 @@ You can also ignore tables with:
php artisan migrate:generate --ignore="table3,table4,table5"
```

Laravel Migrations Generator will first generate all the tables, columns and indexes, and afterwards setup all the foreign key constraints.
Laravel Migrations Generator will first generate all the tables, columns and indexes, and afterward setup all the foreign key constraints.

So make sure you include all the tables listed in the foreign keys so that they are present when the foreign keys are created.

Expand All @@ -104,60 +106,42 @@ php artisan migrate:generate --squash

Run `php artisan help migrate:generate` for a list of options.

| Options | Description |
|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -c, --connection[=CONNECTION] | The database connection to use |
| -t, --tables[=TABLES] | A list of tables or views you wish to generate migrations for separated by a comma: users,posts,comments |
| -i, --ignore[=IGNORE] | A list of tables or views you wish to ignore, separated by a comma: users,posts,comments |
| -p, --path[=PATH] | Where should the file be created? |
| -tp, --template-path[=TEMPLATE-PATH] | The location of the template for this generator |
| --date[=DATE] | Migrations will be created with specified date. Views and foreign keys will be created with + 1 second. Date should be in format supported by `Carbon::parse` |
| --table-filename[=TABLE-FILENAME] | Define table migration filename, default pattern: `[datetime]\_create_[name]_table.php` |
| --view-filename[=VIEW-FILENAME] | Define view migration filename, default pattern: `[datetime]\_create_[name]_view.php` |
| --proc-filename[=PROC-FILENAME] | Define stored procedure filename, default pattern: `[datetime]\_create_[name]_proc.php` |
| --fk-filename[=FK-FILENAME] | Define foreign key migration filename, default pattern: `[datetime]\_add_foreign_keys_to_[name]_table.php` |
| --log-with-batch[=LOG-WITH-BATCH] | Log migrations with given batch number. We recommend using batch number 0 so that it becomes the first migration |
| --default-index-names | Don\'t use DB index names for migrations |
| --default-fk-names | Don\'t use DB foreign key names for migrations |
| --use-db-collation | Generate migrations with existing DB collation |
| --skip-log | Don\'t log into migrations table |
| --skip-vendor | Don\'t generate vendor migrations |
| --skip-views | Don\'t generate views |
| --skip-proc | Don\'t generate stored procedures |
| --squash | Generate all migrations into a single file |
| --with-has-table | Check for the existence of a table using `hasTable` |

## SQLite Alter Foreign Key

The generator first generates all tables and then adds foreign keys to existing tables.

However, SQLite only supports foreign keys upon creation of the table and not when tables are altered.
*_add_foreign_keys_* migrations will still be generated, however will get omitted if migrate to SQLite type database.

## User-Defined Type Columns

The generator will recognize user-defined type from the schema, and then generate migration as
| Options | Description |
|------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [-c, --connection[=CONNECTION]](https://kitloong.github.io/laravel-migrations-generator/options/connection/) | The database connection to use |
| [-t, --tables[=TABLES]](https://kitloong.github.io/laravel-migrations-generator/options/tables/) | A list of tables or views you wish to generate migrations for separated by a comma: users,posts,comments |
| [-i, --ignore[=IGNORE]](https://kitloong.github.io/laravel-migrations-generator/options/ignore/) | A list of tables or views you wish to ignore, separated by a comma: users,posts,comments |
| [-p, --path[=PATH]](https://kitloong.github.io/laravel-migrations-generator/options/path/) | Where should the file be created? |
| [-tp, --template-path[=TEMPLATE-PATH]](https://kitloong.github.io/laravel-migrations-generator/options/template-path/) | The location of the template for this generator |
| [--date[=DATE]](https://kitloong.github.io/laravel-migrations-generator/options/date/) | Migrations will be created with specified date. Views and foreign keys will be created with + 1 second. Date should be in format supported by `Carbon::parse` |
| [--table-filename[=TABLE-FILENAME]](https://kitloong.github.io/laravel-migrations-generator/options/table-filename/) | Define table migration filename, default pattern: `[datetime]\_create_[name]_table.php` |
| [--view-filename[=VIEW-FILENAME]](https://kitloong.github.io/laravel-migrations-generator/options/table-filename/) | Define view migration filename, default pattern: `[datetime]\_create_[name]_view.php` |
| [--proc-filename[=PROC-FILENAME]](https://kitloong.github.io/laravel-migrations-generator/options/table-filename/) | Define stored procedure filename, default pattern: `[datetime]\_create_[name]_proc.php` |
| [--fk-filename[=FK-FILENAME]](https://kitloong.github.io/laravel-migrations-generator/options/table-filename/) | Define foreign key migration filename, default pattern: `[datetime]\_add_foreign_keys_to_[name]_table.php` |
| [--log-with-batch[=LOG-WITH-BATCH]](https://kitloong.github.io/laravel-migrations-generator/options/log-with-batch/) | Log migrations with given batch number. We recommend using batch number 0 so that it becomes the first migration |
| [--default-index-names](https://kitloong.github.io/laravel-migrations-generator/options/default-index-names/) | Don\'t use DB index names for migrations |
| [--default-fk-names](https://kitloong.github.io/laravel-migrations-generator/options/default-fk-names/) | Don\'t use DB foreign key names for migrations |
| [--use-db-collation](https://kitloong.github.io/laravel-migrations-generator/options/use-db-collation/) | Generate migrations with existing DB collation |
| [--skip-log](https://kitloong.github.io/laravel-migrations-generator/options/skip-log/) | Don\'t log into migrations table |
| [--skip-vendor](https://kitloong.github.io/laravel-migrations-generator/options/skip-vendor/) | Don\'t generate vendor migrations |
| [--skip-views](https://kitloong.github.io/laravel-migrations-generator/options/skip-views/) | Don\'t generate views |
| [--skip-proc](https://kitloong.github.io/laravel-migrations-generator/options/skip-proc/) | Don\'t generate stored procedures |
| [--squash](https://kitloong.github.io/laravel-migrations-generator/options/squash/) | Generate all migrations into a single file |
| [--with-has-table](https://kitloong.github.io/laravel-migrations-generator/options/with-has-table/) | Check for the existence of a table using `hasTable` |

```php
public function up()
{
Schema::create('table', function (Blueprint $table) {
...
});
DB::statement("ALTER TABLE table ADD column custom_type NOT NULL");
}
```
## Thank You

Note that the new `column` is always added at the end of the created `table` which means the ordering of the column generated in migration will differ from what we have from the schema.
Thanks to Bernhard Breytenbach for his great work. This package is based on https://github.com/Xethron/migrations-generator.

Supported database with user-defined types:
## Our Amazing Sponsor 🎉

- [x] PostgreSQL
- [x] SQL Server
A huge thank you to our sponsor for supporting this project!

## Thank You
<a href="https://vemto.app/" target="_blank">
<img alt="Vemto" width="200" src="https://vemto.app/images/logo2.png" />
</a>

Thanks to Bernhard Breytenbach for his great work. This package is based on https://github.com/Xethron/migrations-generator.
Want to support this project? [Become a sponsor](https://github.com/sponsors/kitloong) today!

## Contributors

Expand Down
62 changes: 62 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Getting Started

Generate Laravel Migrations from an existing database, including indexes and foreign keys!

## Supported Database

Laravel Migrations Generator supports all five Laravel first-party support databases:

- MariaDB
- MySQL
- PostgreSQL
- SQL Server
- SQLite

## Install

The recommended way to install this is through composer:

```bash
composer require --dev kitloong/laravel-migrations-generator
```

## Version Compatibility

| Laravel | Version |
|--------------------|-------------------------------------------------|
| 12.x | 7.x |
| 11.x | 7.x |
| \>= 10.43.x | 7.x |
| 10.x \| <= 10.42.x | 6.x |
| 9.x | 6.x |
| 8.x | 6.x |
| 7.x | 6.x |
| 6.x | 6.x |
| 5.8.x | 6.x |
| 5.7.x | 6.x |
| 5.6.x | 6.x |
| <= 5.5.x | https://github.com/Xethron/migrations-generator |

### Laravel Setup

Laravel will automatically register service provider for you.

### Lumen Setup

Auto-discovery is not available in Lumen, you need some modification on `bootstrap/app.php`.

#### Enable Facade

Uncomment the following line.

```php
$app->withFacades();
```

#### Register Provider

Add following line into the `Register Service Providers` section.

```php
$app->register(\KitLoong\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
```
36 changes: 36 additions & 0 deletions docs/options/connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Connection

```bash
-c, --connection[=CONNECTION]
```

The `--connection` option allows you to specify the database connection to use. If you don't specify a connection, the default connection as defined in your Laravel's `config/database.php` will be used.

This option is particularly useful when you have multiple database connections defined in your Laravel application and you want to generate migrations for a specific connection.

### Example

Suppose you have a connection named `secondary` defined in your `config/database.php`. You can generate migrations for this connection by running:

```bash
php artisan migrate:generate --connection="secondary"
```

This command will generate migrations for the tables in the `secondary` database connection.

```php
// Up
Schema::connection('secondary')->create('users', function (Blueprint $table) {
$table->bigIncrements('id');
...
});

// Down
Schema::connection('secondary')->dropIfExists('users');
```

If you are not sure what is the name of the connection you want to use, you can check the `connections` array in your `config/database.php` file.

```php
array_keys(config('database.connections'))
```
25 changes: 25 additions & 0 deletions docs/options/date.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Date

```bash
--date[=DATE]
```

The `--date` option allows you to specify the date for the generated migration files.

Migrations will be created with the specified date. Views, procedures, and foreign keys will be created with an additional 1 second.

The date should be in a format supported by [`Carbon::parse`](https://github.com/briannesbitt/Carbon/blob/d481d8d69a94dc00e5c1b147ec1f7e7492626b12/src/Carbon/Traits/Creator.php#L207).

### Example

```bash
php artisan migrate:generate --date="2024-10-08 12:30:00"
```

Will generate the following migrations:

```bash
2024_10_08_123000_create_comments_table.php
2024_10_08_123000_create_users_table.php
2024_10_08_123001_add_foreign_keys_to_comments_table.php
```
31 changes: 31 additions & 0 deletions docs/options/default-fk-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Default Foreign Key Names

```bash
--default-fk-names
```

Use Laravel's default naming convention for foreign keys instead of the database foreign key names.

### Example

```bash
php artisan migrate:generate --default-fk-names
```

Schema with foreign key:

```sql
CREATE TABLE `comments` (
...
CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
```

Will generate as:

```php
Schema::table('comments', function (Blueprint $table) {
...
$table->foreign('user_id')->references('id')->on('users');
});
```
31 changes: 31 additions & 0 deletions docs/options/default-index-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Default Index Names

```bash
--default-index-names
```

Use Laravel's default naming convention for indexes instead of the database index names.

### Example

```bash
php artisan migrate:generate --default-index-names
```

Schema with index:

```sql
CREATE TABLE `comments` (
...
INDEX `user_id_idx` (`user_id`)
);
```

Will generate as:

```php
Schema::create('comments', function (Blueprint $table) {
...
$table->index('user_id');
});
```
Loading
Loading