Rails-Like Database Migrations

Rails-like Database Migrations?

Just use ActiveRecord and a simple Rakefile. For example, if you put your migrations in a db/migrate directory and have a database.yml file that has your db config, this simple Rakefile should work:

Rakefile:

require 'active_record'
require 'yaml'

desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
task :migrate => :environment do
ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
end

task :environment do
ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))
ActiveRecord::Base.logger = Logger.new(STDOUT)
end

database.yml:

adapter: mysql
encoding: utf8
database: test_database
username: root
password:
host: localhost

Afterwards, you'll be able to run rake migrate and have all the migration goodness without a surrounding rails app.

Alternatively, I have a set of bash scripts that perform a very similar function to ActiveRecord migrations, but they only work with Oracle. I used to use them before switching to Ruby and Rails. They are somewhat complicated and I provide no support for them, but if you are interested, feel free to contact me.

A Ruby On Rails-like migration tool

There's no reason you can't use ActiveRecord::Migration outside of a rails app. In fact, you'll find plenty of examples of people doing this, as in http://exposinggotchas.blogspot.com/2011/02/activerecord-migrations-without-rails.html

If you're using mongo as the persistent store, check out mongrations for this.

Migrations for Java

I've used Hibernate's SchemaUpdate to perform the same function as migrations. It's actually easier than migrations because every time you start up your app, it examines the database structure and syncs it up with your mappings so there's no extra rake:db:migrate step and your app can never be out of sync with the database it's running against. Hibernate mapping files are no more complex than Rails migrations so even if you didn't use Hibernate in the app, you could take advantage of it. The downside is that it's not as flexible as far as rolling back, migrating down, running DML statements. As pointed out in the comments, it also doesn't drop tables or columns. I run a separate method to do those manually as part of the Hibernate initialization process.

I don't see why you couldn't use Rails migrations though - as long as you don't mind installing the stack (Ruby, Rake, Rails), you wouldn't have to touch your app.

Rails-like database schema version upgrades for Android

Given that I didn't find anything that supported Android, actually worked, didn't require me to subscribe to an insane database world view, and didn't cost a lot (hobby project, no dice), I came up with the following bodgy hack. It's not clever, but it at least allows me to think about my schemas in a way that I'm familiar with. I don't expect it'd work real well for a large codebase/database schema, but if you've got that you can probably afford to pay for something.

public class AppDatabase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "main";
public static final int LATEST_VERSION = 4;

public static SQLiteDatabase open(Context ctx) {
AppDatabase db = new AppDatabase(ctx);
return db.getWritableDatabase();
}

public AppDatabase(Context ctx) {
super(ctx, DATABASE_NAME, null, LATEST_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, LATEST_VERSION);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (int i = oldVersion+1; i <= newVersion; i++) {
switch (i) {
case 1:
db.execSQL("CREATE TABLE blah ( " +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"start CHAR(4)," +
"end CHAR(4)" +
")");
break;
case 2:
db.execSQL("CREATE TABLE fortnights ( " +
"first_day DATE PRIMARY KEY" +
")");
break;
case 3:
db.execSQL("ALTER TABLE shifts ADD top CHAR(4)");
db.execSQL("ALTER TABLE shifts ADD bottom CHAR(4)");
db.execSQL("UPDATE shifts set top=start, bottom=end");
break;
case 4:
db.execSQL("ALTER TABLE shifts ADD callout BOOLEAN DEFAULT 0");
break;
}
}
}
}

Runtime database migration in .NET (does it exist?)

You mean something like FluentMigrator?

After more searching, I found this question:
https://stackoverflow.com/q/8033/50079

which has more valuable answers. Have a look at it as well.

Database migrations in Joomla

If you want to update your own component's DB schema you can refer to the following thread: http://forum.joomla.org/viewtopic.php?p=1607199

If you need just migrations without components, check these standalone solutions:

  • https://github.com/davejkiger/mysql-php-migrations
  • https://github.com/Billiam/MySQL-PHP-AutoMigrations


Related Topics



Leave a reply



Submit