sql-migrations
Simple standalone migrations gem you can use with plain SQL.
You can execute migrations, seed datebase on production and on test environment with fixtures in non-Ruby pojects.
sql-migrations can work with multiple different databases.
Why ?
This is particularly useful in old projects that doesn't have migrations support, and you really want to use Continues Delivery strategy. Without migrations you wouldn't be able to setup your test environment for automated testing (functional tests, unit tests, integration tests).
For example, if you work in old Zend1 project, and you want to take benefit from using Continues Deployment/Continues Integration mechanisms - you may find this project useful.
Install
sql-migrations are created using Ruby.
- First - install Ruby environment, with
rbenvorrvm. - If your project is not created using Ruby, create your Gemfile:
source 'https://rubygems.org'
gem 'mysql2'
gem 'sql_migrations'
You can use all database adapters, that are supported by Sequel.
Adapters supported by Sequel, by now, are:
ADO, Amalgalite, CUBRID, DataObjects, DB2, DBI, Firebird,
FoundationDB SQL Layer, IBM_DB, Informix, JDBC, MySQL, Mysql2,
ODBC, OpenBase, Oracle, PostgreSQL, SQLAnywhere, SQLite3,
Swift, and TinyTDS
If you are using PostgreSQL use
gem 'pg'
Run
bundle installCreate database config file, for example in
config/databases.ymldefault: development: adapter: mysql2 encoding: utf8 database: test_db_dev username: test_user password: test_pass host: 192.168.1.1 test: adapter: mysql2 encoding: utf8 database: test_db_test username: test_user password: test_pass host: 192.168.1.1 production: adapter: mysql2 encoding: utf8 database: test_db_prod username: test_user password: test_pass host: 192.168.1.100 second_db: development: adapter: mysql2 encoding: utf8 database: second_db_dev username: test_user password: test_pass host: 127.0.0.1 test: adapter: mysql2 encoding: utf8 database: second_db_test username: test_user password: test_pass host: 127.0.0.1
Note that you need to define default database set.
- Migrations/seed/fixtures are executed using rake tasks. So you will need to create
Rakefile. ExampleRakefile:
require 'bundler'
Bundler.require
SqlMigrations.load!('db/config/databases.yml')
SqlMigrations.load_tasks
- It's ready !
Usage
- Valid migration/seed/fixture file names match agains regexp
/(\d{8})_(\d{6})_(.*)?\.sql/. So valid filenames would be:
20150303_180100_test_migration.sql
20150303_180100_whatever_description_of_seed.sql
20150303_180100_fixture1.sql
You can put plain SQL into that files.
- You can create migrations files, seed files and fixtures in directories like this:
db/
migrations/
fixtures/
seed/
If you want to use multiple databases, create database directories:
db/
migrations/
default/
second_db/
fixtures/
default/
second_db/
seed/
default/
second_db/
default/ directory is mandatory, you can put migrations/seed data/fixtures for default database in base directories:
db/
migrations/
20150303_180100_test_migration.sql
second_db/
20150303_180101_test_migration_for_second_db.sql
- In every database that is specified in YAML config,
sql-migrationswill create tablesqlmigrations_schema If everything is set up properly, you should see
sqlmigrationstasks after typingrake -TRun tasks:
rake sqlmigrations:db:migrate # this will execute migrations rake sqlmigrations:db:seed # this will seed database with initial data rake sqlmigration:files:list # this will list all migrations/seed files/fixtures that where foundEnviroment variables
If you want to run migration on different database (for example test) specify ENV:
ENV=test rake sqlmigrations:db:migrate
ENV=test rake sqlmigrations:db:test:seed
or in production:
ENV=production rake sqlmigrations:db:migrate
ENV=production rake sqlmigrations:db:seed
TODO
- Tests
- Generator for
databases.yml - Generator for migrations
License
This is free sofware licensed under MIT license, see LICENSE file