Skip to content

Instantly share code, notes, and snippets.

@fujin
Created October 28, 2008 02:11
Show Gist options
  • Select an option

  • Save fujin/20280 to your computer and use it in GitHub Desktop.

Select an option

Save fujin/20280 to your computer and use it in GitHub Desktop.
irb(main):008:0> v = VhostUser.create(:username => "aj", :password => "non-crypt-password", :ssl => true, :mysql => true, :pgsql => true)
~ SELECT "id", "username" FROM "vhost_users" WHERE ("username" = 'aj') ORDER BY "id", "username" LIMIT 1
~ SELECT "id", "username" FROM "vhost_users" WHERE ("username" = 'aj') ORDER BY "id", "username" LIMIT 1
~ SELECT "id", "name", "vhost_user_id", "vhost_user_username" FROM "mysql_databases" WHERE ("vhost_user_id" IN (NULL)) AND ("vhost_user_username" IN ('aj')) ORDER BY "id"
~ SELECT COUNT(*) FROM "mysql_databases" WHERE ("vhost_user_id" IN (NULL)) AND ("vhost_user_username" IN ('aj'))
~ SELECT "id" FROM "mysql_databases" WHERE ("name" = 'aj') ORDER BY "id" LIMIT 1
~ INSERT INTO "mysql_databases" ("vhost_user_username", "name") VALUES ('aj', 'aj')
~ SELECT "id", "username", "password", "mysql_database_id" FROM "mysql_database_users" WHERE ("mysql_database_id" IN (1)) ORDER BY "id"
~ SELECT "id" FROM "mysql_database_users" WHERE ("username" = 'aj') ORDER BY "id" LIMIT 1
~ INSERT INTO "mysql_database_users" ("mysql_database_id", "username", "password") VALUES (1, 'aj', 'non-crypt-password')
~ SELECT "id", "name", "vhost_user_id", "vhost_user_username" FROM "pgsql_databases" WHERE ("vhost_user_id" IN (NULL)) AND ("vhost_user_username" IN ('aj')) ORDER BY "id"
~ SELECT COUNT(*) FROM "pgsql_databases" WHERE ("vhost_user_id" IN (NULL)) AND ("vhost_user_username" IN ('aj'))
~ SELECT "id" FROM "pgsql_databases" WHERE ("name" = 'aj') ORDER BY "id" LIMIT 1
~ INSERT INTO "pgsql_databases" ("name", "vhost_user_username") VALUES ('aj', 'aj')
~ SELECT "id", "username", "password", "pgsql_database_id" FROM "pgsql_database_users" WHERE ("pgsql_database_id" IN (1)) ORDER BY "id"
~ SELECT "id" FROM "pgsql_database_users" WHERE ("username" = 'aj') ORDER BY "id" LIMIT 1
~ INSERT INTO "pgsql_database_users" ("password", "username", "pgsql_database_id") VALUES ('non-crypt-password', 'aj', 1)
~ INSERT INTO "vhost_users" ("username", "quota", "mysql", "statistics", "ssl", "pgsql", "suspended", "password") VALUES ('aj', 0, 't', 'f', 't', 't', 't', 'non-crypt-password')
~ SELECT "id" FROM "mysql_databases" WHERE ("name" = 'aj') ORDER BY "id" LIMIT 1
~ UPDATE "mysql_databases" SET "vhost_user_id" = 1 WHERE ("id" = 1)
~ SELECT "id" FROM "mysql_database_users" WHERE ("username" = 'aj') ORDER BY "id" LIMIT 1
~ SELECT "id" FROM "pgsql_databases" WHERE ("name" = 'aj') ORDER BY "id" LIMIT 1
~ UPDATE "pgsql_databases" SET "vhost_user_id" = 1 WHERE ("id" = 1)
~ SELECT "id" FROM "pgsql_database_users" WHERE ("username" = 'aj') ORDER BY "id" LIMIT 1
=> #<VhostUser id=1 username="aj" password="non-crypt-password" quota=0 ssl=true mysql=true pgsql=true statistics=false suspended=true>
class VhostUser
include DataMapper::Resource
property :id, Serial
property :username, String, :nullable => false, :length => 8, :key => true
property :password, String, :nullable => false, :length => 32
property :quota, Integer, :nullable => false, :default => 0
property :ssl, Boolean, :default => false
property :mysql, Boolean, :default => false
property :pgsql, Boolean, :default => false
property :statistics, Boolean, :default => false
property :suspended, Boolean, :default => true
has n, :domains
has n, :mysql_databases
has n, :pgsql_databases
validates_is_unique :username
before :update, :create_default_db
before :save, :create_default_db
def create_default_db
if self.suspended == false
if self.mysql == true and self.mysql_databases.count == 0
@mysql_database = self.mysql_databases.build(:name => self.username)
@mysql_database.save
@mysql_database_user = @mysql_database.mysql_database_users.build(:username => self.username, :password => self.password)
@mysql_database_user.save
end
if self.pgsql == true and self.pgsql_databases.count == 0
@pgsql_database = self.pgsql_databases.build(:name => self.username)
@pgsql_database.save
@pgsql_database_user = @pgsql_database.pgsql_database_users.build(:username => self.username, :password => self.password)
@pgsql_database_user.save
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment