arfy 0.2 → 0.2.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.
- data/Changelog.md +21 -1
- data/Readme.md +101 -0
- data/Readme.pt_br.md +105 -0
- data/bin/arfy +1 -0
- data/lib/arfy.rb +6 -18
- data/lib/arfy/environment.rb +200 -0
- data/lib/arfy/generator.rb +4 -38
- data/lib/arfy/migration_builder.rb +105 -0
- data/lib/arfy/migration_builder/builders/add_column.rb +17 -0
- data/lib/arfy/migration_builder/builders/add_index.rb +41 -0
- data/lib/arfy/migration_builder/builders/change_column.rb +16 -0
- data/lib/arfy/migration_builder/builders/column.rb +147 -0
- data/lib/arfy/migration_builder/builders/create_table.rb +30 -0
- data/lib/arfy/migration_builder/builders/drop_table.rb +16 -0
- data/lib/arfy/migration_builder/builders/remove_column.rb +14 -0
- data/lib/arfy/migration_builder/builders/remove_index.rb +44 -0
- data/lib/arfy/migration_builder/builders/rename_column.rb +26 -0
- data/lib/arfy/migration_builder/builders/rename_table.rb +28 -0
- data/lib/arfy/migration_builder/generic_migration.rb +98 -0
- data/lib/arfy/migration_builder/template_handler.rb +47 -0
- data/lib/arfy/string_extensions.rb +20 -0
- data/lib/arfy/version.rb +1 -1
- data/lib/arfy_tasks.rb +2 -0
- data/lib/tasks/all.rb +34 -30
- metadata +36 -11
- data/README.md +0 -64
- data/lib/arfy/application_faker.rb +0 -22
- data/lib/arfy/config_faker.rb +0 -13
- data/lib/arfy/environment_faker.rb +0 -13
- data/lib/arfy/migration_template +0 -7
- data/lib/arfy/rails_faker.rb +0 -25
data/Changelog.md
CHANGED
@@ -1,8 +1,28 @@
|
|
1
|
+
## 0.2.3
|
2
|
+
|
3
|
+
Features:
|
4
|
+
|
5
|
+
- Specs to guarantee the support to all db: tasks
|
6
|
+
- Adds api to create migration files programatically
|
7
|
+
- Introduced the arfy executable which allow create predefined
|
8
|
+
migrations, here are some examples:
|
9
|
+
|
10
|
+
* arfy create_table people name:string birth:date add_index:name
|
11
|
+
* arfy add_column people country:string limit:50 null:false
|
12
|
+
* arfy remove_index column:name
|
13
|
+
* arfy remove_index name:idx_awswome
|
14
|
+
|
15
|
+
## 0.2.1
|
16
|
+
|
17
|
+
Features:
|
18
|
+
|
19
|
+
- Configurable workdir
|
20
|
+
|
1
21
|
## 0.2
|
2
22
|
|
3
23
|
Features:
|
4
24
|
|
5
|
-
- Support to all tasks
|
25
|
+
- Support to all db: tasks
|
6
26
|
- LICENSE file created
|
7
27
|
|
8
28
|
## 0.1.5
|
data/Readme.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Arfy
|
2
|
+
## Allow use Rails migrations, but without Rails ;)
|
3
|
+
|
4
|
+
Are you working on a Ruby (non Rails) project, using relational database
|
5
|
+
and missing the greateness of Rails Migrations? No more, arfy do the
|
6
|
+
job!
|
7
|
+
|
8
|
+
This gem main goal is to allow the use of Rails rake tasks on the db:
|
9
|
+
namespace such as: migrations tasks, seed.rb loading, schema dump... You
|
10
|
+
just need install arfy gem.
|
11
|
+
|
12
|
+
gem install arfy
|
13
|
+
|
14
|
+
On your project dir, create a Rakefile and put this require on it:
|
15
|
+
|
16
|
+
require 'arfy_tasks'
|
17
|
+
|
18
|
+
And... you are ready to go. Just like that, boom! You can check your
|
19
|
+
available tasks, on you project dir, execute:
|
20
|
+
|
21
|
+
rake -T
|
22
|
+
|
23
|
+
You should see some familiar tasks with some arfy tasks:
|
24
|
+
|
25
|
+
rake arfy:generate:migration # Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)
|
26
|
+
rake arfy:rails_env # Changes the environment (options TARGET=development) - use your's configuration name on database.yml
|
27
|
+
rake arfy:workdir # Change dir where arfy searches for db and config dir (options DBDIR=db, MIGRATIONDIR=DBDIR/migrate, CONFIGDIR=d...
|
28
|
+
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in ...
|
29
|
+
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
|
30
|
+
rake db:fixtures:load # Load fixtures into the current environment's database.
|
31
|
+
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
|
32
|
+
rake db:migrate:status # Display status of migrations
|
33
|
+
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
|
34
|
+
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
|
35
|
+
rake db:schema:load # Load a schema.rb file into the database
|
36
|
+
rake db:seed # Load the seed data from db/seeds.rb
|
37
|
+
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
|
38
|
+
rake db:structure:dump # Dump the database structure to an SQL file
|
39
|
+
rake db:version # Retrieves the current schema version number
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
## Configuring
|
44
|
+
|
45
|
+
You need to create your database.yml, just like when you are using a
|
46
|
+
[Rails Project](http://guides.rubyonrails.org/getting_started.html).
|
47
|
+
Create some migrations and run the tasks:
|
48
|
+
|
49
|
+
rake db:create:all
|
50
|
+
rake db:migrate
|
51
|
+
|
52
|
+
By default, the directories used to configure and store your migrations
|
53
|
+
are the same as the ones used by Rails:
|
54
|
+
|
55
|
+
your_project/
|
56
|
+
db/migrate/
|
57
|
+
config/
|
58
|
+
|
59
|
+
But you can use any directory structure you want. There are two main
|
60
|
+
ways to achieve this:
|
61
|
+
|
62
|
+
* rake task workdir:
|
63
|
+
|
64
|
+
rake arfy:workdir MIGRATIONDIR=db/migrate CONFIGDIR=db/config CONFIGNAME=database.yml
|
65
|
+
(no exemplo foram usados os caminhos/nome padrão do Rails para facilitar o entendimento)
|
66
|
+
|
67
|
+
* configuration file:
|
68
|
+
|
69
|
+
- generate an .arfy file on your project root
|
70
|
+
- arfy expects this file in yml format with the following optional
|
71
|
+
parameters (any ommited parameter will be replaced by a default
|
72
|
+
value):
|
73
|
+
|
74
|
+
db:
|
75
|
+
seeds: db/seeds.rb
|
76
|
+
migrate: db/migrate
|
77
|
+
schema: db/schema.rb
|
78
|
+
config:
|
79
|
+
database: config/database.yml
|
80
|
+
|
81
|
+
## Generating Migrations
|
82
|
+
|
83
|
+
In order to create your migrations, follow the oficial
|
84
|
+
[Rails docs](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html).
|
85
|
+
To generate a new empty migrations, run the following task:
|
86
|
+
|
87
|
+
rake arfy:generate:migration NAME=my-MigrationName
|
88
|
+
|
89
|
+
(The awkward migration name passed as NAME parameter is to ilustrate
|
90
|
+
that this tasks will transform this string in a pretty file name,
|
91
|
+
something like: 20110920000101_my_migration_name.rb).
|
92
|
+
|
93
|
+
(In a future version, arfy is gonna give you an capable of do this kind
|
94
|
+
of repetitive jobs)
|
95
|
+
|
96
|
+
---
|
97
|
+
|
98
|
+
Enjoy arfy, and, if you like it an can, help make it better reporting
|
99
|
+
bugs, suggesting fetaures and even forking the project to kill some
|
100
|
+
bugs registered on github issues.
|
101
|
+
|
data/Readme.pt_br.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# ATENÇÃO
|
2
|
+
Essa é uma documentação relativa a versão "edge" dessa gem, se você
|
3
|
+
precisa saber sobre a gem de produção, verifique o [Readme.md](https://github.com/ricardovaleriano/arfy)
|
4
|
+
|
5
|
+
# Arfy
|
6
|
+
## Possibilita o uso das migrations do Rails, só que sem o Rails ;)
|
7
|
+
|
8
|
+
Está trabalhando um projeto Ruby (não Rails) usando banco de dados relacional e andou
|
9
|
+
sentindo falta das migrations do Rails? Arfy vai te ajudar a resolver isso.
|
10
|
+
|
11
|
+
O objetivo principal dessa gem é possibilitar o uso das rake tasks do Rails
|
12
|
+
presentes no namespace db:, isto é: migrations, carregamento do arquivo
|
13
|
+
seeds.rb, schema dump... Basta instalar a gem do Arfy.
|
14
|
+
|
15
|
+
gem install arfy
|
16
|
+
|
17
|
+
No diretório do seu projeto crie um Rakefile com o seguinte require:
|
18
|
+
|
19
|
+
require 'arfy_tasks'
|
20
|
+
|
21
|
+
E... pronto. É só isso, você pode verificar todas as tarefas disponíveis
|
22
|
+
usando:
|
23
|
+
|
24
|
+
rake -T
|
25
|
+
|
26
|
+
Você deve ver algumas tarefas bem familiares e outras adicionadas pelo
|
27
|
+
Arfy:
|
28
|
+
|
29
|
+
rake arfy:generate:migration # Generate migration (options NAME=migration_file_name, OUTDIR=db/migrate, TARGET=environment)
|
30
|
+
rake arfy:rails_env # Changes the environment (options TARGET=development) - use your's configuration name on database.yml
|
31
|
+
rake arfy:workdir # Change dir where arfy searches for db and config dir (options DBDIR=db, MIGRATIONDIR=DBDIR/migrate, CONFIGDIR=d...
|
32
|
+
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in ...
|
33
|
+
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
|
34
|
+
rake db:fixtures:load # Load fixtures into the current environment's database.
|
35
|
+
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
|
36
|
+
rake db:migrate:status # Display status of migrations
|
37
|
+
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
|
38
|
+
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
|
39
|
+
rake db:schema:load # Load a schema.rb file into the database
|
40
|
+
rake db:seed # Load the seed data from db/seeds.rb
|
41
|
+
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
|
42
|
+
rake db:structure:dump # Dump the database structure to an SQL file
|
43
|
+
rake db:version # Retrieves the current schema version number
|
44
|
+
|
45
|
+
---
|
46
|
+
|
47
|
+
## Configuração
|
48
|
+
|
49
|
+
Lembre-se de criar o seu arquivo database.yml, assim como você faria em
|
50
|
+
seu projeto [Rails](http://guides.rubyonrails.org/getting_started.html).
|
51
|
+
Crie suas migrations e rode as tarefas:
|
52
|
+
|
53
|
+
rake db:create:all
|
54
|
+
rake db:migrate
|
55
|
+
|
56
|
+
Por convenção, os diretórios usados para configurar e armazenar as migrações
|
57
|
+
são os mesmos da convenção utilizada pelo Rails:
|
58
|
+
|
59
|
+
your_project/
|
60
|
+
db/migrate/
|
61
|
+
config/
|
62
|
+
|
63
|
+
Porém você pode usar qualquer estrutura de diretórios que desejar. Existem
|
64
|
+
duas formas de configurar o caminho onde suas migrações serão salvas, onde
|
65
|
+
o arfy precisa buscar as configurações (e afins):
|
66
|
+
|
67
|
+
* rake task workdir:
|
68
|
+
|
69
|
+
rake arfy:workdir MIGRATIONDIR=db/migrate CONFIGDIR=db/config CONFIGNAME=database.yml
|
70
|
+
(no exemplo foram usados os caminhos/nome padrão do Rails para facilitar o entendimento)
|
71
|
+
|
72
|
+
* arquivo de configuração:
|
73
|
+
|
74
|
+
- crie um arquivo .arfy na raiz do seu projeto
|
75
|
+
- arfy espera que esse arquivo esteja no formato yml e tenha os
|
76
|
+
seguintes parâmetros (todos opcionais, valores padrão serão
|
77
|
+
utilizados caso algum deles não seja encontrado):
|
78
|
+
|
79
|
+
db:
|
80
|
+
seeds: db/seeds.rb
|
81
|
+
migrate: db/migrate
|
82
|
+
schema: db/schema.rb
|
83
|
+
config:
|
84
|
+
database: config/database.yml
|
85
|
+
|
86
|
+
## Gerando Migrations
|
87
|
+
|
88
|
+
Para criar suas migrações siga a documentação oficial do Rails:
|
89
|
+
[Rails docs](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html).
|
90
|
+
Para gerar uma migration vazia, use a rake task a seguir:
|
91
|
+
|
92
|
+
rake arfy:generate:migration NAME=my-MigrationName
|
93
|
+
|
94
|
+
(O nome estranho da migração passado como parâmetro aqui é só para ilustrar que a task vai
|
95
|
+
criar um arquivo com o nome bonito para você, algo como: 20110920000101_my_migration_name.rb).
|
96
|
+
|
97
|
+
(Numa próxima versão o arfy vai disponibilizar um executável para
|
98
|
+
realizar esse tipo de tarefa)
|
99
|
+
|
100
|
+
---
|
101
|
+
|
102
|
+
Aproveite e, se puder, me ajude a melhorar essa ferramenta reportando
|
103
|
+
bugs, sugerindo funcionalidades ou mesmo forkando o projeto caso queria
|
104
|
+
solucionar algum bug reportado no issues do github.
|
105
|
+
|
data/bin/arfy
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
data/lib/arfy.rb
CHANGED
@@ -1,26 +1,14 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'erb'
|
3
3
|
|
4
|
-
require 'arfy/
|
5
|
-
require 'arfy/
|
6
|
-
require 'arfy/environment_faker'
|
7
|
-
require 'arfy/rails_faker'
|
4
|
+
require 'arfy/string_extensions'
|
5
|
+
require 'arfy/environment'
|
8
6
|
require 'arfy/generator'
|
7
|
+
require 'arfy/migration_builder'
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
paths["db/seeds"] = "db/seeds.rb"
|
14
|
-
migrate_path = String.new("db/migrate")
|
15
|
-
def migrate_path.to_a
|
16
|
-
[self]
|
9
|
+
unless defined? Rails
|
10
|
+
class Rails
|
11
|
+
extend Arfy::RailsFaker
|
17
12
|
end
|
18
|
-
paths["db/migrate"] = migrate_path
|
19
|
-
paths
|
20
13
|
end
|
21
14
|
|
22
|
-
class Rails
|
23
|
-
extend RailsFaker
|
24
|
-
end
|
25
|
-
|
26
|
-
require "tasks/all"
|
@@ -0,0 +1,200 @@
|
|
1
|
+
module Arfy
|
2
|
+
class NoObjectDefinedError < Exception
|
3
|
+
end
|
4
|
+
|
5
|
+
class Environment
|
6
|
+
class EnvWorkdirConfiguration
|
7
|
+
def configuration_data
|
8
|
+
return @data unless @data.nil?
|
9
|
+
|
10
|
+
@data = {"db" => {}, "config" => {}}
|
11
|
+
if File.exists? ".arfy"
|
12
|
+
@data = YAML.load( IO.read(".arfy") )
|
13
|
+
end
|
14
|
+
@data
|
15
|
+
end
|
16
|
+
|
17
|
+
def migration_dirpath
|
18
|
+
configuration_data["db"]["migrate"] ||
|
19
|
+
ENV["db/migrate"] || "db/migrate"
|
20
|
+
end
|
21
|
+
|
22
|
+
def config_path
|
23
|
+
ENV["db/config"] || "config"
|
24
|
+
end
|
25
|
+
|
26
|
+
def database_config_path
|
27
|
+
configuration_data["config"]["database"] ||
|
28
|
+
"#{config_path}/#{ENV["db/config/database.yml"] || "database.yml"}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def seed_rb_path
|
32
|
+
configuration_data["db"]["seeds"] ||
|
33
|
+
"#{ENV["db/seeds_dir"] || "db"}/#{ENV["db/seeds.rb"] || "seeds.rb"}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def schema_path
|
37
|
+
configuration_data["db"]["schema"] ||
|
38
|
+
ENV["db/schema.rb"] || "db/schema.rb"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# TODO:should be possible change the result of this method to
|
43
|
+
# configure the paths
|
44
|
+
def configured_paths
|
45
|
+
workdir = EnvWorkdirConfiguration.new
|
46
|
+
|
47
|
+
paths = {}
|
48
|
+
paths["config/database"] = workdir.database_config_path
|
49
|
+
paths["db/seeds"] = workdir.seed_rb_path
|
50
|
+
migrate_path = String.new(workdir.migration_dirpath)
|
51
|
+
def migrate_path.to_a
|
52
|
+
[self]
|
53
|
+
end
|
54
|
+
paths["db/migrate"] = migrate_path
|
55
|
+
paths["db/schema"] = workdir.schema_path
|
56
|
+
ENV["SCHEMA"] = paths["db/schema"]
|
57
|
+
if paths["db/schema"].index("/") == 0
|
58
|
+
path = paths["db/schema"]
|
59
|
+
else
|
60
|
+
path = File.join(Dir.pwd, paths["db/schema"])
|
61
|
+
end
|
62
|
+
FileUtils.mkdir_p(File.dirname(path))
|
63
|
+
paths
|
64
|
+
end
|
65
|
+
|
66
|
+
def augment(object_or_module_name, module_name=nil)
|
67
|
+
object, module_name = initialize_module_name(object_or_module_name, module_name)
|
68
|
+
unless object
|
69
|
+
if block_given?
|
70
|
+
object = yield
|
71
|
+
else
|
72
|
+
object = Object.new
|
73
|
+
end
|
74
|
+
end
|
75
|
+
pimp_object(object, module_name)
|
76
|
+
end
|
77
|
+
|
78
|
+
def prepare_active_record_module
|
79
|
+
class << ActiveRecord::Base
|
80
|
+
def establish_connection(configuration = nil)
|
81
|
+
#forget about it, we already have a connection XD
|
82
|
+
end
|
83
|
+
|
84
|
+
def configurations
|
85
|
+
Rails.application.config.database_configuration
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def set_rails_env(target)
|
91
|
+
ENV['RAILS_ENV'] = (target || ENV["RAILS_ENV"]) || "development"
|
92
|
+
puts "working on environment: #{ENV['RAILS_ENV']}" if ENV["RAILS_ENV"]
|
93
|
+
ENV["RAILS_ENV"]
|
94
|
+
end
|
95
|
+
|
96
|
+
def database_connect
|
97
|
+
database_configurations = Rails.application.config.database_configuration
|
98
|
+
env_db_configuration = database_configurations[Rails.env]
|
99
|
+
ActiveRecord::Base.establish_connection(env_db_configuration)
|
100
|
+
end
|
101
|
+
|
102
|
+
def create_config(options_param={})
|
103
|
+
config = {
|
104
|
+
"config/database" => "config/database.yml",
|
105
|
+
"db/seeds" => "db/seeds.rb",
|
106
|
+
"db/migrate" => "db/migrate"}.merge(options_param)
|
107
|
+
|
108
|
+
new_hash = {
|
109
|
+
"config" => {
|
110
|
+
"database" => config["config/database"]
|
111
|
+
},
|
112
|
+
"db" => {
|
113
|
+
"seeds" => config["db/seeds"],
|
114
|
+
"migrate" => config["db/migrate"],
|
115
|
+
"schema" => config["db/schema"]}
|
116
|
+
}
|
117
|
+
|
118
|
+
File.open(".arfy", "w") do |f|
|
119
|
+
f << new_hash.to_yaml
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
def initialize_module_name(object_or_module_name, module_name)
|
125
|
+
if module_name.nil?
|
126
|
+
module_name = object_or_module_name
|
127
|
+
object = nil
|
128
|
+
else
|
129
|
+
object = object_or_module_name
|
130
|
+
end
|
131
|
+
[object, module_name]
|
132
|
+
end
|
133
|
+
|
134
|
+
def pimp_object(object, module_name)
|
135
|
+
raise NoObjectDefinedError unless object
|
136
|
+
object.class.send(:include, module_name)
|
137
|
+
object
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
module ConfigFaker
|
143
|
+
#https://github.com/rails/rails/blob/master/railties/lib/rails/application/configuration.rb
|
144
|
+
def paths
|
145
|
+
Environment.new.configured_paths
|
146
|
+
end
|
147
|
+
|
148
|
+
def database_configuration
|
149
|
+
@configuration ||= YAML::load(ERB.new(IO.read(paths["config/database"])).result)
|
150
|
+
@configuration
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
module ApplicationFaker
|
155
|
+
def config
|
156
|
+
@fake_config = Environment.new.augment(@fake_config, Arfy::ConfigFaker)
|
157
|
+
@fake_config
|
158
|
+
end
|
159
|
+
|
160
|
+
def paths
|
161
|
+
Environment.new.configured_paths
|
162
|
+
end
|
163
|
+
|
164
|
+
def load_seed
|
165
|
+
seed_file = paths["db/seeds"]
|
166
|
+
load(seed_file) if File.exist?(seed_file)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
module EnvironmentFaker
|
171
|
+
def method_missing(who_ami, *params)
|
172
|
+
method = who_ami.to_s
|
173
|
+
ends_with_questionmark = method.index("?") == method.length - 1
|
174
|
+
if ends_with_questionmark
|
175
|
+
env_name_asked = method.slice(0, method.length-1)
|
176
|
+
return env_name_asked == ENV['RAILS_ENV'] || self.env
|
177
|
+
end
|
178
|
+
super
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
module RailsFaker
|
183
|
+
def root
|
184
|
+
"."
|
185
|
+
end
|
186
|
+
|
187
|
+
def env
|
188
|
+
@fake_env = Environment.new.augment(@fake_env, Arfy::EnvironmentFaker) do
|
189
|
+
String.new(ENV['RAILS_ENV'] ||= "development")
|
190
|
+
end
|
191
|
+
@fake_env
|
192
|
+
end
|
193
|
+
|
194
|
+
def application
|
195
|
+
@fake_application = Environment.new.augment(@fake_application, Arfy::ApplicationFaker)
|
196
|
+
@fake_application
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|