Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backup database tables in a pre defined order.
Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
- Loading branch information
Yorick Peterse
committed
Jan 2, 2012
1 parent
bcd6485
commit 795b1c3
Showing
1 changed file
with
38 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
namespace :db do | ||
table_order = [ | ||
:settings, | ||
:user_statuses, | ||
:users, | ||
:user_groups, | ||
:user_groups_users, | ||
:permissions, | ||
:sections, | ||
:section_entry_statuses, | ||
:section_entries, | ||
:comment_statuses, | ||
:comments, | ||
:category_groups, | ||
:categories, | ||
:category_groups_sections, | ||
:custom_field_methods, | ||
:custom_field_types, | ||
:custom_field_groups, | ||
:custom_fields, | ||
:custom_field_values, | ||
:custom_field_groups_sections, | ||
:menus, | ||
:menu_items, | ||
] | ||
|
||
desc 'Creates a database independent backup' | ||
task :export do | ||
data = {} | ||
|
||
Zen.database.tables.each do |table| | ||
handle = File.open( | ||
File.expand_path('../../tmp/%s' % table, __FILE__), | ||
'w' | ||
) | ||
|
||
handle.write(Marshal.dump([table, Zen.database[table].all])) | ||
handle.close | ||
table_order.each do |table| | ||
data[table] = Zen.database[table].all | ||
end | ||
|
||
handle = File.open(File.expand_path('../../tmp/backup', __FILE__), 'w') | ||
|
||
handle.write(Marshal.dump(data)) | ||
handle.close | ||
end | ||
|
||
desc 'Imports a database backup' | ||
task :import, :backup do |task, args| | ||
table, rows = Marshal.load(File.open(args[:backup], 'r').read) | ||
data = Marshal.load(File.open(args[:backup], 'r').read) | ||
|
||
Zen.database[table].delete | ||
Zen.database[table].insert_multiple(rows) | ||
table_order.each do |table| | ||
Zen.database[table].delete | ||
Zen.database[table].insert_multiple(data[table]) | ||
end | ||
end | ||
end |