Module: Migr8

Defined in:
lib/migr8.rb

Defined Under Namespace

Modules: Actions, DBMS, Util Classes: Application, BaseSkeleton, CommandSetupError, HistoryFileError, Migr8Error, Migration, MigrationError, MigrationFileError, Repository, RepositoryError, RepositoryOperation, SQLExecutionError

Constant Summary collapse

RELEASE =
"$Release: 0.4.4 $".split()[1]
DEBUG =
false
README =
"Migr8.rb\n========\n\nMigr8.rb is a database schema version management tool.\n\n* Easy to install, easy to setup, and easy to start\n* No configuration file; instead, only two environment variables\n* Designed carefully to suit Git or Mercurial\n* Supports SQLite3, PostgreSQL, and MySQL\n* Written in Ruby (>= 1.8)\n\n\nQuick Start\n-----------\n\n1. Donwload migr8.rb.\n\n    $ curl -Lo migr8.rb http://bit.ly/migr8_rb\n    $ chmod a+x migr8.rb\n    ### or\n    $ gem install migr8\n\n2. Set environment variables: $MIGR8_COMMAND and $MIGR8_EDITOR.\n\n    $ export MIGR8_COMMAND=\"sqlite3 dbfile1\"            # for SQLite3\n    $ export MIGR8_COMMAND=\"psql -q -U user1 dbname1\"   # for PostgreSQL\n    $ export MIGR8_COMMAND=\"mysql -s -u user1 dbname1\"  # for MySQL\n\n    $ export MIGR8_EDITOR=\"open -a TextMate\"     # for TextMate (MacOSX)\n    $ export MIGR8_EDITOR=\"emacsclient\"          # for Emacs\n    $ export MIGR8_EDITOR=\"vim\"                  # for Vim\n\n3. Create managiment files and table.\n\n    $ ./migr8.rb init         # create files in current directory,\n                              # and create a table in DB.\n\n4. Now you can manage DB schema versions.\n\n    $ ./migr8.rb                                 # show current status\n    $ ./migr8.rb new -m \"create 'users' table\"   # create a migration\n           # or  ./migr8.rb new --table=users\n    $ ./migr8.rb                                 # show status again\n    $ ./migr8.rb up                              # apply migration\n    $ ./migr8.rb                                 # show status again\n    $ ./migr8.rb hist                            # list history\n\n5. You may got confliction error when `git rebase` or `git pull`.\n   In this case, you must resolve it by hand.\n   (This is intended design.)\n\n    $ git rebase master         # confliction!\n    $ ./migr8.rb hist -o        # open 'migr8/history.txt', and\n                                # resolve confliction manually\n    $ ./migr8.rb hist           # check whether history file is valid\n    $ git add migr8/history.txt\n    $ git rebase --continue\n\n\nTemplating\n----------\n\n(!!Attention!! this is experimental feature and may be changed in the future.)\n\nIt is possible to embed eRuby code into `up` and `down` scripts.\n\nSyntax:\n\n* `<% ... %>`  : Ruby statement\n* `<%= ... %>` : Ruby expression, escaping `'` into `''` (or `\\'` on MySQL)\n* `<%== ... %>` : Ruby expression, no escaping\n\nFor example:\n\nvars:\n  - table: users\n\nup: |\n  insert into ${table}(name) values\n  <% comma = \"  \" %>\n  <% for name in [\"Haruhi\", \"Mikuru\", \"Yuki\"] %>\n    <%= comma %>('<%= name %>')\n    <% comma = \", \" %>\n  <% end %>\n  ;\n\ndown: |\n  <% for name in [\"Haruhi\", \"Mikuru\", \"Yuki\"] %>\n  delete from ${table} where name = '<%= name %>';\n  <% end %>\n\nThe above is the same as the following:\n\nup: |\n  insert into users(name) values\n      ('Haruhi')\n    , ('Mikuru')\n    , ('Yuki')\n  ;\n\ndown: |\n  delete from users where name = 'Haruhi';\n  delete from users where name = 'Mikuru';\n  delete from users where name = 'Yuki';\n\nIn eRuby code, values in `vars` are available as instance variables.\nFor example:\n\nversion:     uhtu4853\ndesc:        register members\nauthor:      kyon\nvars:\n  - table:   users\n  - members: [Haruhi, Mikuru, Yuki]\n\nup: |\n  <% for member in @members %>\n  insert into ${table}(name) values ('<%= member %>');\n  <% end %>\n\ndown: |\n  <% for member in @members %>\n  delete from ${table} where name = '<%= member %>';\n  <% end %>\n\nIf you want to see up and down scripts rendered, run `migr8.rb show` action.\nFor example:\n\n$ ./migr8.rb show uhtu4853\nversion:     uhtu4853\ndesc:        register members\nauthor:      kyon\nvars:\n  - table:     \"users\"\n  - members:   [\"Haruhi\", \"Mikuru\", \"Yuki\"]\n\nup: |\n  insert into users(name) values ('Haruhi');\n  insert into users(name) values ('Mikuru');\n  insert into users(name) values ('Yuki');\n\ndown: |\n  delete from users where name = 'Haruhi';\n  delete from users where name = 'Mikuru';\n  delete from users where name = 'Yuki';\n\n\nNotice that migration file using eRuby code is not compatible with other\nMigr8 implemtation.\n\n\nTips\n----\n\n* `migr8.rb up -a` applys all migrations, while `migr8.rb up` applys a\n  migration.\n\n* `migr8.rb -D up` saves SQL executed into `migr8/history.txt` file.\n\n* `migr8.rb redo` is equivarent to `migr8.rb down; migr8.rb up`.\n\n* `migr8.rb new -p` generates migration file with plain skeleton, and\n  `migr8.rb new --table=name` generates with table name.\n\n* `migr8.rb unapply -x` unapplies migration which is applied in DB but\n  corresponding migration file doesn't exist.\n  (Describing in detail, `migr8.rb unapply -x abcd1234` runs `down` script\n  in `_migr_history` table, while `migr8.rb unapply abcd1234` runs `down`\n  script in `migr8/migrations/abcd1234.yaml` file.)\n  This may help you when switching Git/Hg branch.\n\n* `migr8.rb` generates sql file and run it with sql command such as `psql`\n  (PostgreSQL), `sqlite3` (SQLite3) or `mysql` (MySQL). Therefore you can\n  use non-sql command in migration file.\n  For example:\n\n    up: |\n      -- read data from CSV file and insert into DB (PostgreSQL)\n      \\copy table1 from 'file1.csv' with csv;\n\n* **MySQL doesn't support transactional DDL**.\n  It will cause troubles when you have errors in migration script\n  (See https://www.google.com/search?q=transactional+DDL for details).\n  On the other hand, SQLite3 and PostgreSQL support transactional DDL,\n  and DDL will be rollbacked when error occurred in migration script.\n  Very good.\n\n\nUsage and Actions\n-----------------\n\nUsage: migr8.rb [global-options] [action [options] [...]]\n  -h, --help          : show help\n  -v, --version       : show version\n  -D, --debug         : not remove sql file ('migr8/tmp.sql') for debug\n\nActions:  (default: status)\n  readme              : !!READ ME AT FIRST!!\n  help [action]       : show help message of action, or list action names\n  init                : create necessary files and a table\n  hist                : list history of versions\n    -o                :   open history file with $MIGR8_EDITOR\n    -b                :   rebuild history file from migration files\n  new                 : create new migration file and open it by $MIGR8_EDITOR\n    -m text           :   description message (mandatory)\n    -u user           :   author name (default: current user)\n    -v version        :   specify version number instead of random string\n    -p                :   plain skeleton\n    -e editor         :   editr command (such as 'emacsclient', 'open', ...)\n    --table=table     :   skeleton to create table\n    --column=tbl.col  :   skeleton to add column\n    --index=tbl.col   :   skeleton to create index\n    --unique=tbl.col  :   skeleton to add unique constraint\n  show [version]      : show migration file with expanding variables\n    -x                :   load values of migration from history table in DB\n  edit [version]      : open migration file by $MIGR8_EDITOR\n    -r N              :   edit N-th file from latest version\n    -e editor         :   editr command (such as 'emacsclient', 'open', ...)\n  status              : show status\n  up                  : apply next migration\n    -n N              :   apply N migrations\n    -a                :   apply all migrations\n  down                : unapply current migration\n    -n N              :   unapply N migrations\n    --ALL             :   unapply all migrations\n  redo                : do migration down, and up it again\n    -n N              :   redo N migrations\n    --ALL             :   redo all migrations\n  apply version ...   : apply specified migrations\n  unapply version ... : unapply specified migrations\n    -x                :   unapply versions with down-script in DB, not in file\n  delete version ...  : delete unapplied migration file\n    --Imsure          :   you must specify this option to delete migration\n\n\nTODO\n----\n\n* [_] write more tests\n* [_] test on windows\n* [_] implement in Python\n* [_] implement in JavaScript\n\n\nChanges\n-------\n\n### Release 0.4.4 (2014-07-19) ###\n\n* [bugfix] fix `redo` action to run `down` first internally.\n\n\n### Release 0.4.3 (2014-02-28) ###\n\n* [bugfix] fix reporting error when there is a migration which is\n  applied but migration file doesn't exist.\n\n\n### Release 0.4.2 (2014-02-05) ###\n\n* [bugfix] re-packaging gem file\n\n\n### Release 0.4.1 (2014-02-05) ###\n\n* [bugfix] Fix to allow migration file which contains no vars.\n\n\n### Release 0.4.0 (2013-11-28) ###\n\n* [enhance] RubyGems package available.\n  You can install migr8.rb by `gem install migr8`.\n* [enhance] eRuby templating `up` and `down` script.\n  See 'Templating' section of README file for details.\n* [enhance] Add new action 'show' which shows migration attributes\n  with expanding variables (ex: `${table}`) and renderting template.\n* [enhance] Add new action 'delete' which deletes unapplied migration file.\n  Note: this action can't delete migration which is already applied.\n* [enhance] Add new option 'new -v version' in order to specify version\n  number by yourself instead of auto-generated random string.\n* [bufix] Action 'edit version' now can open migration file even when\n  version number in migration file is wrong.\n\n\n### Release 0.3.1 (2013-11-24) ###\n\n* [bugfix] Fix 'hist' action not to raise error.\n\n\n### Release 0.3.0 (2013-11-22) ###\n\n* [enhance] Add `-x` option to `unapply` action which unapplies migrations\n  by down-script in DB, not in migration file.\n  You can unapply migrations which files are missing in some reason.\n* [change] Eliminate indentation from output of 'readme' action.\n\n\n### Release 0.2.1 (2013-11-20) ###\n\n* [bugfix] Fix `new --table=name` action to set table name correctly\n\n\n### Release 0.2.0 (2013-11-14) ###\n\n* [enhance] Add new options to `new` action for some skeletons\n  * `new --table=table` : create table\n  * `new --column=tbl.col` : add column to table\n  * `new --index=tbl.col` : create index on column\n  * `new --unique=tbl.col` : add unique constraint on column\n* [enhance] Add new option `hist -b` action which re-generate history file.\n* [change] Change several error messages\n* [change] Tweak SQL generated on SQLite3\n\n\n### Release 0.1.1 (2013-11-12) ###\n\n* [IMPORTANT] Change history table schema: SORRY, YOU MUST RE-CREATE HISTORY TABLE.\n* [enhance] Fix 'up' action to save both up and down script into history table.\n\n\n### Release 0.1.0 (2013-11-11) ###\n\n* Public release\n\n\nLicense\n-------\n\n$License: MIT License $\n\n\nCopyright\n---------\n\n$Copyright: copyright(c) 2013-2014 kuwata-lab.com all rights reserved $\n"

Class Method Summary collapse

Class Method Details

.DEBUG=(flag) ⇒ Object



23
24
25
26
# File 'lib/migr8.rb', line 23

def self.DEBUG=(flag)
  remove_const(:DEBUG)
  return const_set(:DEBUG, flag)
end