config-to-laravel-migrations 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +230 -0
- data/bin/configurator +8 -0
- data/lib/configurator/cli.rb +16 -0
- data/lib/configurator/generator.rb +122 -0
- data/lib/configurator/version.rb +3 -0
- data/lib/configurator.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 445b9a57f87e670eaecebe7422575d32b999097a84baf4e675cad5e69f629fd1
|
4
|
+
data.tar.gz: 0352ec7a738ae9c6f08a512ccce501b0727c87ce5c19d290ef637428d1fda4a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45068758acf80f56a4c4e04b26ec60a43da633acc94fa794857c5fc9b9a84e511edd7d14d0d99ef5fdf0f5eac0529e329fb67fefe08ca89fac8e252e085eec6a
|
7
|
+
data.tar.gz: 37d0a7127ea892b784496955ff0b09309f7f3295c6d250f99a7de6c3ed8333573ab7e99c9a10640b20c6867693dc334bb1a655d5d646891ebb50afd009482af3
|
data/README.md
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# Configurator
|
2
|
+
|
3
|
+
### Available for hire email | suwilanji@inongo.space. Linked In Suwilanji Chipofya
|
4
|
+
|
5
|
+
**Configurator** is a command-line tool that generates Laravel Eloquent migrations from YAML or JSON configuration files. It simplifies the process of defining database schemas and automates the creation of migration files for Laravel applications.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- **Supports Multiple Formats**: Works with YAML and JSON configuration files.
|
12
|
+
- **Customizable**: Define table names, columns, data types, and constraints in a simple configuration file.
|
13
|
+
- **Scalable**: Generates migrations for multiple tables and relationships.
|
14
|
+
- **Error Handling**: Provides clear warnings and skips invalid configurations.
|
15
|
+
|
16
|
+
---
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
### 1. Install Ruby
|
21
|
+
|
22
|
+
Ensure you have Ruby installed on your system. You can check your Ruby version by running:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
ruby -v
|
26
|
+
```
|
27
|
+
|
28
|
+
If Ruby is not installed, follow the official [Ruby installation guide](https://www.ruby-lang.org/en/documentation/installation/).
|
29
|
+
|
30
|
+
### 2. Install the Gem
|
31
|
+
|
32
|
+
You can install the `configurator` gem directly from the source:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
gem install configurator
|
36
|
+
```
|
37
|
+
|
38
|
+
Alternatively, clone the repository and build the gem locally:
|
39
|
+
|
40
|
+
```bash
|
41
|
+
git clone https://github.com/suwi-lanji/configurator.git
|
42
|
+
cd configurator
|
43
|
+
gem build configurator.gemspec
|
44
|
+
gem install configurator-1.0.0.gem
|
45
|
+
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
### 1. Create a Configuration File
|
52
|
+
|
53
|
+
Define your database schema in a YAML or JSON file. Here’s an example `school_managementsystem.yml` file:
|
54
|
+
|
55
|
+
```yaml
|
56
|
+
User:
|
57
|
+
table_name: users
|
58
|
+
attributes:
|
59
|
+
user_id: { type: int, primary_key: true }
|
60
|
+
username: { type: string, unique: true }
|
61
|
+
password_hash: { type: string }
|
62
|
+
email: { type: string, unique: true }
|
63
|
+
role_id: { type: int, foreign_key: roles.role_id }
|
64
|
+
institution_id: { type: int, foreign_key: institutions.institution_id }
|
65
|
+
|
66
|
+
Role:
|
67
|
+
table_name: roles
|
68
|
+
attributes:
|
69
|
+
role_id: { type: int, primary_key: true }
|
70
|
+
role_name: { type: string, unique: true }
|
71
|
+
```
|
72
|
+
|
73
|
+
### 2. Generate Migrations
|
74
|
+
|
75
|
+
Run the `configurator` command with your configuration file:
|
76
|
+
|
77
|
+
```bash
|
78
|
+
configurator generate school_managementsystem.yml migrations
|
79
|
+
```
|
80
|
+
|
81
|
+
This will generate Laravel migration files in the `migrations` directory.
|
82
|
+
|
83
|
+
---
|
84
|
+
|
85
|
+
## Example Output
|
86
|
+
|
87
|
+
For the above configuration, the tool will generate the following files:
|
88
|
+
|
89
|
+
#### `migrations/20250210120000_create_users_table.php`
|
90
|
+
|
91
|
+
```php
|
92
|
+
<?php
|
93
|
+
|
94
|
+
use Illuminate\Database\Migrations\Migration;
|
95
|
+
use Illuminate\Database\Schema\Blueprint;
|
96
|
+
use Illuminate\Support\Facades\Schema;
|
97
|
+
|
98
|
+
class CreateUserTable extends Migration
|
99
|
+
{
|
100
|
+
public function up()
|
101
|
+
{
|
102
|
+
Schema::create('users', function (Blueprint $table) {
|
103
|
+
$table->id();
|
104
|
+
$table->integer('user_id');
|
105
|
+
$table->string('username')->unique();
|
106
|
+
$table->string('password_hash');
|
107
|
+
$table->string('email')->unique();
|
108
|
+
$table->integer('role_id');
|
109
|
+
$table->foreignId('institution_id');
|
110
|
+
$table->timestamps();
|
111
|
+
});
|
112
|
+
}
|
113
|
+
|
114
|
+
public function down()
|
115
|
+
{
|
116
|
+
Schema::dropIfExists('users');
|
117
|
+
}
|
118
|
+
}
|
119
|
+
```
|
120
|
+
|
121
|
+
#### `migrations/20250210120001_create_roles_table.php`
|
122
|
+
|
123
|
+
```php
|
124
|
+
<?php
|
125
|
+
|
126
|
+
use Illuminate\Database\Migrations\Migration;
|
127
|
+
use Illuminate\Database\Schema\Blueprint;
|
128
|
+
use Illuminate\Support\Facades\Schema;
|
129
|
+
|
130
|
+
class CreateRoleTable extends Migration
|
131
|
+
{
|
132
|
+
public function up()
|
133
|
+
{
|
134
|
+
Schema::create('roles', function (Blueprint $table) {
|
135
|
+
$table->id();
|
136
|
+
$table->integer('role_id');
|
137
|
+
$table->string('role_name')->unique();
|
138
|
+
$table->timestamps();
|
139
|
+
});
|
140
|
+
}
|
141
|
+
|
142
|
+
public function down()
|
143
|
+
{
|
144
|
+
Schema::dropIfExists('roles');
|
145
|
+
}
|
146
|
+
}
|
147
|
+
```
|
148
|
+
|
149
|
+
---
|
150
|
+
|
151
|
+
## Configuration File Format
|
152
|
+
|
153
|
+
### YAML Format
|
154
|
+
|
155
|
+
```yaml
|
156
|
+
ModelName:
|
157
|
+
table_name: table_name
|
158
|
+
attributes:
|
159
|
+
column_name:
|
160
|
+
type: data_type
|
161
|
+
primary_key: true/false
|
162
|
+
unique: true/false
|
163
|
+
foreign_key: referenced_table.referenced_column
|
164
|
+
```
|
165
|
+
|
166
|
+
### JSON Format
|
167
|
+
|
168
|
+
```json
|
169
|
+
{
|
170
|
+
"ModelName": {
|
171
|
+
"table_name": "table_name",
|
172
|
+
"attributes": {
|
173
|
+
"column_name": {
|
174
|
+
"type": "data_type",
|
175
|
+
"primary_key": true/false,
|
176
|
+
"unique": true/false,
|
177
|
+
"foreign_key": "referenced_table.referenced_column"
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
```
|
183
|
+
|
184
|
+
---
|
185
|
+
|
186
|
+
## Supported Data Types
|
187
|
+
|
188
|
+
The following data types are supported:
|
189
|
+
|
190
|
+
- `int` → `integer`
|
191
|
+
- `string` → `string`
|
192
|
+
- `date` → `date`
|
193
|
+
- `datetime` → `datetime`
|
194
|
+
- `float` → `float`
|
195
|
+
- `enum` → `enum`
|
196
|
+
|
197
|
+
---
|
198
|
+
|
199
|
+
## Contributing
|
200
|
+
|
201
|
+
Contributions are welcome! Follow these steps to contribute:
|
202
|
+
|
203
|
+
1. Fork the repository.
|
204
|
+
2. Create a new branch for your feature or bugfix.
|
205
|
+
3. Commit your changes and push to the branch.
|
206
|
+
4. Submit a pull request.
|
207
|
+
|
208
|
+
---
|
209
|
+
|
210
|
+
## License
|
211
|
+
|
212
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
213
|
+
|
214
|
+
---
|
215
|
+
|
216
|
+
## Support
|
217
|
+
|
218
|
+
If you encounter any issues or have questions, please open an issue on the [GitHub repository](https://github.com/suwi-lanji/configurator/issues).
|
219
|
+
|
220
|
+
---
|
221
|
+
|
222
|
+
## Author
|
223
|
+
|
224
|
+
- **Suwilanji Jack Chipofya**
|
225
|
+
GitHub: [suwi-lanji](https://github.com/suwi-lanji)
|
226
|
+
Email: suwilanji@inongo.space
|
227
|
+
|
228
|
+
---
|
229
|
+
|
230
|
+
Enjoy using **Configurator**! 🚀
|
data/bin/configurator
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'configurator/generator'
|
3
|
+
require 'configurator/version'
|
4
|
+
module Configurator
|
5
|
+
class CLI < Thor
|
6
|
+
desc "generate FILE OUTPUT_DIR", "Generate Laravel migrations from a YAML or JSON file"
|
7
|
+
def generate(file, output_dir)
|
8
|
+
Generator.new(file, output_dir).generate_migrations
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "version", "Print the version"
|
12
|
+
def version
|
13
|
+
puts Configurator::VERSION
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Configurator
|
6
|
+
class Generator
|
7
|
+
DATA_TYPE_MAPPING = {
|
8
|
+
'int' => 'integer',
|
9
|
+
'string' => 'string',
|
10
|
+
'date' => 'date',
|
11
|
+
'datetime' => 'datetime',
|
12
|
+
'float' => 'float',
|
13
|
+
'enum' => 'enum'
|
14
|
+
}
|
15
|
+
|
16
|
+
def initialize(file, output_dir)
|
17
|
+
@file = file
|
18
|
+
@config = load_config(file)
|
19
|
+
@migrations_dir = output_dir
|
20
|
+
validate_config!
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_migrations
|
24
|
+
FileUtils.mkdir_p(@migrations_dir)
|
25
|
+
|
26
|
+
@config.each do |model, details|
|
27
|
+
table_name = details["table_name"]
|
28
|
+
attributes = details["attributes"]
|
29
|
+
|
30
|
+
unless attributes
|
31
|
+
puts "Warning: No attributes found for model '#{model}'. Skipping."
|
32
|
+
next
|
33
|
+
end
|
34
|
+
|
35
|
+
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
|
36
|
+
migration_file = "#{@migrations_dir}/#{timestamp}_create_#{table_name}_table.php"
|
37
|
+
|
38
|
+
File.open(migration_file, 'w') do |file|
|
39
|
+
file.write <<~PHP
|
40
|
+
<?php
|
41
|
+
|
42
|
+
use Illuminate\\Database\\Migrations\\Migration;
|
43
|
+
use Illuminate\\Database\\Schema\\Blueprint;
|
44
|
+
use Illuminate\\Support\\Facades\\Schema;
|
45
|
+
|
46
|
+
class Create#{model.capitalize}Table extends Migration
|
47
|
+
{
|
48
|
+
public function up()
|
49
|
+
{
|
50
|
+
Schema::create('#{table_name}', function (Blueprint $table) {
|
51
|
+
$table->id();
|
52
|
+
#{generate_columns(attributes).map { |col| "\t\t\t#{col};" }.join("\n")}
|
53
|
+
$table->timestamps();
|
54
|
+
});
|
55
|
+
}
|
56
|
+
|
57
|
+
public function down()
|
58
|
+
{
|
59
|
+
Schema::dropIfExists('#{table_name}');
|
60
|
+
}
|
61
|
+
}
|
62
|
+
PHP
|
63
|
+
end
|
64
|
+
|
65
|
+
puts "Generated migration: #{migration_file}"
|
66
|
+
end
|
67
|
+
|
68
|
+
puts "All migrations generated in '#{@migrations_dir}' directory."
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def load_config(file)
|
74
|
+
case File.extname(file)
|
75
|
+
when '.yaml', '.yml'
|
76
|
+
YAML.load_file(file)
|
77
|
+
when '.json'
|
78
|
+
JSON.parse(File.read(file), symbolize_names: true)
|
79
|
+
else
|
80
|
+
raise "Unsupported file format: #{File.extname(file)}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def validate_config!
|
85
|
+
unless @config.is_a?(Hash)
|
86
|
+
raise "Invalid configuration: Expected a hash, got #{@config.class}"
|
87
|
+
end
|
88
|
+
|
89
|
+
@config.each do |model, details|
|
90
|
+
unless details.is_a?(Hash)
|
91
|
+
raise "Invalid configuration for model '#{model}': Expected a hash, got #{details.class}"
|
92
|
+
end
|
93
|
+
|
94
|
+
unless details["table_name"]
|
95
|
+
raise "Missing 'table_name' for model '#{model}'"
|
96
|
+
end
|
97
|
+
|
98
|
+
unless details["attributes"]
|
99
|
+
puts "Warning: No 'attributes' found for model '#{model}'"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def generate_columns(attributes)
|
105
|
+
columns = []
|
106
|
+
attributes.each do |attr, config|
|
107
|
+
type = DATA_TYPE_MAPPING[config["type"]] || 'string'
|
108
|
+
if type == 'enum'
|
109
|
+
columns << "\$table->#{type}('#{attr}', [#{config["enum"].map { |v| "'#{v}'" }.join(', ')}])"
|
110
|
+
elsif config["foreign_key"]
|
111
|
+
columns << "\$table->foreignId('#{attr}')"
|
112
|
+
else
|
113
|
+
column_def = "\$table->#{type}('#{attr}')"
|
114
|
+
column_def += '->unique()' if config["unique"]
|
115
|
+
column_def += '->nullable()' unless config["primary_key"]
|
116
|
+
columns << column_def
|
117
|
+
end
|
118
|
+
end
|
119
|
+
columns
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/lib/configurator.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config-to-laravel-migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Suwilanji Jack Chipofya
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-02-16 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: thor
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: yaml
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.1'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.1'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: json
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.5'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.5'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rspec
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.10'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.10'
|
68
|
+
description: A command-line tool to generate Laravel Eloquent migrations from YAML
|
69
|
+
or JSON configuration files.
|
70
|
+
email:
|
71
|
+
- suwilanji@inongo.space
|
72
|
+
executables:
|
73
|
+
- configurator
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- README.md
|
78
|
+
- bin/configurator
|
79
|
+
- lib/configurator.rb
|
80
|
+
- lib/configurator/cli.rb
|
81
|
+
- lib/configurator/generator.rb
|
82
|
+
- lib/configurator/version.rb
|
83
|
+
homepage: https://github.com/suwi-lanji/configurator
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.6.2
|
102
|
+
specification_version: 4
|
103
|
+
summary: Generate Laravel migrations from YAML or JSON config files.
|
104
|
+
test_files: []
|