make model extendable #191

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
6 participants

cbou commented Oct 9, 2011

Hi,

I tried to write a way to have extendable model. I wanted to do such a config possible:

Backup::Model.new(:db_backup, 'My Backup', '') do

  database MySQL do |db|
    db.name               = "firstdb"
    db.username           = "firstdb"
    db.password           = "firstdb"
    db.host               = "localhost"
    db.port               = 3306
    db.socket             = "/var/run/mysqld/mysqld.sock"
  end

  database MySQL do |db|
    db.name               = "seconddb"
    db.username           = "seconddb"
    db.password           = "seconddb"
    db.host               = "localhost"
    db.port               = 3306
    db.socket             = "/var/run/mysqld/mysqld.sock"
  end

  compress_with Gzip do |compression|
    compression.best = true
    compression.fast = false
  end

end

Backup::Model.new(:daily, 'My Backup', :db_backup) do

  store_with S3 do |s3|
    s3.access_key_id      = 'my_access_key_id'
    s3.secret_access_key  = 'my_secret_access_key'
    s3.region             = 'us-east-1'
    s3.bucket             = 'bucket-name'
    s3.path               = '/path/to/my/daily-backups'
    s3.keep               = 10
  end

end

Backup::Model.new(:weekly, 'My Backup', :db_backup) do

  store_with S3 do |s3|
    s3.access_key_id      = 'my_access_key_id'
    s3.secret_access_key  = 'my_secret_access_key'
    s3.region             = 'us-east-1'
    s3.bucket             = 'bucket-name'
    s3.path               = '/path/to/my/weekly-backups'
    s3.keep               = 10
  end

end

Backup::Model.new(:monthly, 'My Backup', :db_backup) do

  store_with S3 do |s3|
    s3.access_key_id      = 'my_access_key_id'
    s3.secret_access_key  = 'my_secret_access_key'
    s3.region             = 'us-east-1'
    s3.bucket             = 'bucket-name'
    s3.path               = '/path/to/my/monthly-backups'
    s3.keep               = 10
  end

end

I'm very new in ruby, so feel free to make a better implementation if you want to keep this feature.

Contributor

tomash commented Oct 9, 2011

single pipe should be replaced with double pipe
there should be test

and i'm not sure this is a good approach. why not just clone the parent model and call store_with again within the context of the new/cloned one?

cbou commented Oct 13, 2011

Sorry, I don't know what do you mean with double pipe. I try to change

@databases   = self.databases   | model.databases

by

@databases   = self.databases   || model.databases

It does not work, but again, I'm a newbie in Ruby :p

I also try to make a clone of the parent and also to call store_with. I try something like that:

base = Backup::Model.new(:db_backup, 'My Backup', '') do
   ....
end

weekly = base.clone
weekly.trigger = :weekly
weekly.store_with S3 do |s3|
    s3.access_key_id      = 'my_access_key_id'
    s3.secret_access_key  = 'my_secret_access_key'
    s3.region             = 'us-east-1'
    s3.bucket             = 'bucket-name'
    s3.path               = '/path/to/my/backups'
    s3.keep               = 10
end
Backup::Model.all << weekly

And it works. I think it's a bit tricky because it needs to take a look at the backup's code but it's ok for my purpose.

Thanks :)

Contributor

tomash commented Oct 15, 2011

Aaah, ok, you use single pipe to combine arrays, not to choose the non-empty one.

Owner

mrrooijen commented Oct 20, 2011

Thanks! I hadn't thought of inheritance, your example seems like an interesting use case. I will leave this pull request open and consider such functionality.

cerw commented Nov 2, 2011

I am also looking for "right" way of storing dayly/weekly/monlty backups using your gem. This seems like great idea. Or is there any other functions that allows me to reorder backup based on time/date?

👍

timurb commented Nov 5, 2012

How did you make the @cbou's example work?
trigger is an attr_reader and therefore throws the NoMethodError: undefined method 'trigger=' for #<Backup::Model:0xb6abb8e4> for me.

I could use weekly.instance_variable_set(:@trigger, "weekly") even though it looks hackish but it throws undefined method 'basename' for nil:NilClass at /var/lib/gems/1.8/gems/backup-3.0.25/lib/backup/splitter.rb:39

@ghost ghost closed this Feb 24, 2013

cbou commented Jan 4, 2014

Same issue here with the new version of backup. Sorry I do not have the time to make some investigation.

To solve it I just install an old version of backup:

gem install backup -v 3.0.18
@ghost

ghost commented Jan 9, 2014

See the docs on Preconfigured Models, as well as the daily/weekly/monthy example under Storages.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment