Class: Ayadn::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/migration.rb

Instance Method Summary collapse

Constructor Details

#initializeMigration

Returns a new instance of Migration.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ayadn/migration.rb', line 13

def initialize
  @still = false
  @thor = Thor::Shell::Color.new
  accounts_old = Dir.home + "/ayadn/accounts.db"
  unless File.exist?(accounts_old)
    puts "\n"
    @thor.say_status :error, "can't find the Ayadn 1.x accounts database", :red
    @thor.say_status :canceled, "migration canceled", :red
    puts "\n"
    exit
  end
  begin
    @accounts = Daybreak::DB.new(accounts_old)
    # just in case of a previous canceled migration
    # which could have left the accounts.db in place
    if @accounts.size == 1 || @accounts.size == 0
      @accounts.close
      File.delete(Dir.home + "/ayadn/accounts.db")
      @thor.say_status :delete, Dir.home + "/ayadn/accounts.db", :yellow
      @thor.say_status :stopped, "no more accounts to migrate", :green
      exit
    end
    @active_old = @accounts['ACTIVE']
    @home = @accounts[@active_old][:path]
    bookmarks_old = "#{@home}/db/bookmarks.db"
    aliases_old = "#{@home}/db/aliases.db"
    blacklist_old = "#{@home}/db/blacklist.db"
    users_old = "#{@home}/db/users.db"
    @pagination_old = "#{@home}/pagination/pagination.db"
    @index_old = "#{@home}/pagination/index.db"

    @config_path_old = "#{@home}/config/config.yml"

    @bookmarks = Daybreak::DB.new(bookmarks_old) if File.exist?(bookmarks_old)
    @aliases = Daybreak::DB.new(aliases_old) if File.exist?(aliases_old)
    @blacklist = Daybreak::DB.new(blacklist_old) if File.exist?(blacklist_old)
    @users = Daybreak::DB.new(users_old) if File.exist?(users_old)
    @pagination = Daybreak::DB.new(@pagination_old) if File.exist?(@pagination_old)
    @index = Daybreak::DB.new(@index_old) if File.exist?(@index_old)

    @sqlfile = "#{@home}/db/ayadn.sqlite"
    @sql = Amalgalite::Database.new(@sqlfile)
  rescue Exception => e
    puts "\n"
    @thor.say_status :error, "#{e}", :red
    @thor.say_status :stack, "#{caller}", :red
    @thor.say_status :canceled, "migration canceled", :red
    puts "\n"
    exit
  end
end

Instance Method Details

#accountsObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/ayadn/migration.rb', line 288

def accounts
  @thor.say_status :create, Dir.home + "/ayadn/accounts.sqlite", :blue
  sql = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
  @thor.say_status :import, "account informations", :cyan
  if sql.schema.tables.empty?
    sql.execute_batch "      CREATE TABLE Accounts (\n        username VARCHAR(20),\n        user_id INTEGER,\n        handle VARCHAR(21),\n        account_path TEXT,\n        active INTEGER,\n        token TEXT\n      );\n    SQL\n    sql.reload_schema!\n  end\n  active, active_account = \"\"\n  @accounts.each do |k,v|\n    if k == \"ACTIVE\"\n      active_account = v\n      next\n    end\n  end\n  actives = sql.execute(\"SELECT user_id FROM Accounts WHERE active=1\")\n  sql.execute(\"UPDATE Accounts SET active=0\") unless actives.empty?\n  sql.transaction do |db_in_transaction|\n    active = [@accounts[active_account]]\n    @thor.say_status :delete, \"old @\#{active_account} account\", :green\n    @accounts.delete(active_account)\n    if @accounts.size == 1 # only the 'ACTIVE' label, so it's like 0\n      @accounts.close\n      File.delete(Dir.home + \"/ayadn/accounts.db\")\n      @thor.say_status :delete, Dir.home + \"/ayadn/accounts.db\", :green\n    else\n      @accounts.each do |k,v|\n        next if k == \"ACTIVE\"\n        @accounts['ACTIVE'] = v[:username]\n        @still = v[:username]\n        break\n      end\n      @accounts.close\n    end\n    active.each do |obj|\n      insert_data = {}\n      insert_data[\":username\"] = obj[:username]\n      insert_data[\":user_id\"] = obj[:id].to_i\n      insert_data[\":handle\"] = obj[:handle]\n      insert_data[\":account_path\"] = obj[:path]\n      insert_data[\":active\"] = 1\n      template = Dir.home + \"/ayadn/\#{obj[:username]}/auth/token\"\n      insert_data[\":token\"] = File.read(template)\n      db_in_transaction.prepare(\"INSERT INTO Accounts(username, user_id, handle, account_path, active, token) VALUES(:username, :user_id, :handle, :account_path, :active, :token);\") do |insert|\n        insert.execute(insert_data)\n      end\n    end\n  end\n  @thor.say_status :imported, \"@\#{active_account} account\", :green\nend\n"

#aliasesObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ayadn/migration.rb', line 137

def aliases
  @sql.execute_batch "    CREATE TABLE Aliases (\n      channel_id INTEGER,\n      alias VARCHAR(255)\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(\"\#{@home}/db/aliases.db\")\n    @thor.say_status :import, \"Aliases database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @aliases.each do |k,v|\n        insert_data = {}\n        insert_data[\":k\"] = v.to_i\n        insert_data[\":v\"] = k\n        db_in_transaction.prepare(\"INSERT INTO Aliases(channel_id, alias) VALUES(:k, :v);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@aliases.size} objects\", :green\n    @aliases.close\n    File.delete(\"\#{@home}/db/aliases.db\")\n    @thor.say_status :delete, \"\#{@home}/db/aliases.db\", :green\n  end\nend\n"

#allObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ayadn/migration.rb', line 65

def all
  # DON'T MODIFY THE ORDER!
  puts banner()
  puts "\n"
  @thor.say_status :start, "migration", :yellow
  old_backup = "#{@home}/backup"
  if Dir.exist?(old_backup)
    if Dir.entries(old_backup).size > 2
      FileUtils.mv(Dir.glob("#{old_backup}/*"), "#{@home}/downloads")
      @thor.say_status :move, "files from 'backup' to 'downloads'", :green
    end
    Dir.rmdir(old_backup)
    @thor.say_status :delete, old_backup, :green
  end
  old_channels = "#{@home}/db/channels.db"
  if File.exist?(old_channels)
    @thor.say_status :delete, old_channels, :green
    File.delete(old_channels)
  end
  if File.exist?("#{@home}/db/ayadn_pinboard.db")
    @thor.say_status :move, "pinboard credentials", :green
    FileUtils.mv("#{@home}/db/ayadn_pinboard.db", "#{@home}/auth/pinboard.data")
  end
  bookmarks()
  aliases()
  blacklist()
  niceranks()
  users()
  pagination()
  index()
  accounts()
  config()
  @thor.say_status :done, "migration", :yellow
  puts "\n"
  if @still != false
    @thor.say_status :WARNING, "another user, @#{@still}, is still in the old database", :red
    @thor.say_status :PLEASE, "you should run `ayadn migrate` again right now", :yellow
    puts "\n"
  else
    @thor.say_status :ok, "ready to go!", :green
    @thor.say_status :thanks, "you can use Ayadn now", :cyan
    puts "\n"
  end
end


418
419
420
421
422
423
424
425
426
427
428
# File 'lib/ayadn/migration.rb', line 418

def banner
  "\n\\t\\t     _____ __ __ _____ ____  _____\n\\t\\t    |  _  |  |  |  _  |    \\\\|   | |\n\\t\\t    |     |_   _|     |  |  | | | |\n\\t\\t    |__|__| |_| |__|__|____/|_|___|\n\n\n  BANNER\nend\n"

#blacklistObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ayadn/migration.rb', line 164

def blacklist
  @sql.execute_batch "    CREATE TABLE Blacklist (\n      type VARCHAR(255),\n      content TEXT\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(\"\#{@home}/db/blacklist.db\")\n    @thor.say_status :import, \"Blacklist database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @blacklist.each do |k,v|\n        insert_data = {}\n        ks = k.dup.to_s\n        ks[0] = \"\" if ks[0] == \"-\"\n        ks[0] = \"\" if ks[0] == \"@\"\n        insert_data[\":k\"] = v.to_s\n        insert_data[\":v\"] = ks\n        db_in_transaction.prepare(\"INSERT INTO Blacklist(type, content) VALUES(:k, :v);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@blacklist.size} objects\", :green\n    @blacklist.close\n    File.delete(\"\#{@home}/db/blacklist.db\")\n    @thor.say_status :delete, \"\#{@home}/db/blacklist.db\", :green\n  end\nend\n"

#bookmarksObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ayadn/migration.rb', line 110

def bookmarks
  @sql.execute_batch "    CREATE TABLE Bookmarks (\n      post_id INTEGER,\n      bookmark TEXT\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(\"\#{@home}/db/bookmarks.db\")\n    @thor.say_status :import, \"Bookmarks database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @bookmarks.each do |k,v|\n        insert_data = {}\n        insert_data[\":k\"] = k.to_i\n        insert_data[\":v\"] = v.to_json.to_s\n        db_in_transaction.prepare(\"INSERT INTO Bookmarks(post_id, bookmark) VALUES(:k, :v);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@bookmarks.size} objects\", :green\n    @bookmarks.close\n    File.delete(\"\#{@home}/db/bookmarks.db\")\n    @thor.say_status :delete, \"\#{@home}/db/bookmarks.db\", :green\n  end\nend\n"

#configObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/ayadn/migration.rb', line 348

def config
  @thor.say_status :load, "config file", :blue
  old_conf = YAML.load(File.read(@config_path_old))
  conf = Settings.defaults
  @thor.say_status :convert, "settings", :cyan
  conf[:timeline][:source] = old_conf[:timeline][:show_source] || true
  conf[:timeline][:symbols] = old_conf[:timeline][:show_symbols] || true
  conf[:timeline][:name] = old_conf[:timeline][:show_real_name] || true
  conf[:timeline][:date] = old_conf[:timeline][:show_date] || true
  conf[:timeline][:debug] = old_conf[:timeline][:show_debug] || false
  conf[:timeline][:compact] = old_conf[:timeline][:compact] || false
  if old_conf[:timeline][:directed] == 1 || old_conf[:timeline][:directed] == true
    conf[:timeline][:directed] = true
  else
    conf[:timeline][:directed] = false
  end
  if old_conf[:marker].nil?
    conf[:marker] = { messages: true }
  else
    conf[:marker][:messages] = old_conf[:marker][:update_messages] || true  
  end
  conf[:backup][:posts] = old_conf[:backup][:auto_save_sent_posts] || false
  conf[:backup][:messages] = old_conf[:backup][:auto_save_sent_messages] || false
  conf[:backup][:lists] = old_conf[:backup][:auto_save_lists] || false
  conf[:colors][:id] = old_conf[:colors][:id] || :blue
  conf[:colors][:index] = old_conf[:colors][:index] || :red
  conf[:colors][:username] = old_conf[:colors][:username] || :green
  conf[:colors][:name] = old_conf[:colors][:name] || :magenta
  conf[:colors][:debug] = old_conf[:colors][:debug] || :red
  conf[:colors][:date] = old_conf[:colors][:date] || :cyan
  conf[:colors][:link] = old_conf[:colors][:link] || :yellow
  conf[:colors][:dots] = old_conf[:colors][:dots] || :blue
  conf[:colors][:hashtags] = old_conf[:colors][:hashtags] || :cyan
  conf[:colors][:debug] = old_conf[:colors][:debug] || :red
  conf[:colors][:mentions] = old_conf[:colors][:mentions] || :red
  conf[:colors][:source] = old_conf[:colors][:source] || :cyan
  conf[:colors][:symbols] = old_conf[:colors][:symbols] || :green
  conf[:colors][:unread] = old_conf[:colors][:unread] || :cyan
  conf[:formats][:list] = old_conf[:formats][:list] || {}
  conf[:formats][:list][:reverse] = old_conf[:formats][:list][:reverse] || true
  conf[:formats][:table] = old_conf[:formats][:table] || {}
  conf[:formats][:table][:width] = old_conf[:formats][:table][:width] || 75
  conf[:formats][:table][:borders] = old_conf[:formats][:table][:borders] || true
  conf[:scroll][:spinner] = old_conf[:timeline][:show_spinner] || true
  conf[:scroll][:timer] = old_conf[:scroll][:timer] || 3
  conf[:movie][:hashtag] = old_conf[:movie][:hashtag] || 'nowwatching'
  conf[:tvshow][:hashtag] = old_conf[:tvshow][:hashtag] || 'nowwatching'
  conf[:channels][:links] = old_conf[:timeline][:show_channel_oembed] || true
  conf[:nicerank][:threshold] = old_conf[:nicerank][:threshold] || 2.1
  conf[:nicerank][:filter] = old_conf[:nicerank][:filter] || true
  conf[:counts][:defaults] = old_conf[:counts][:defaults] || 50
  conf[:counts][:unified] = old_conf[:counts][:unified] || 50
  conf[:counts][:global] = old_conf[:counts][:global] || 50
  conf[:counts][:checkins] = old_conf[:counts][:checkins] || 50
  conf[:counts][:conversations] = old_conf[:counts][:conversations] || 50
  conf[:counts][:photos] = old_conf[:counts][:photos] || 50
  conf[:counts][:trending] = old_conf[:counts][:trending] || 50
  conf[:counts][:mentions] = old_conf[:counts][:mentions] || 50
  conf[:counts][:convo] = old_conf[:counts][:convo] || 50
  conf[:counts][:posts] = old_conf[:counts][:posts] || 50
  conf[:counts][:messages] = old_conf[:counts][:messages] || 50
  conf[:counts][:search] = old_conf[:counts][:search] || 200
  conf[:counts][:whoreposted] = old_conf[:counts][:whoreposted] || 20
  conf[:counts][:whostarred] = old_conf[:counts][:whostarred] || 20
  conf[:counts][:whatstarred] = old_conf[:counts][:whatstarred] || 100
  conf[:counts][:files] = old_conf[:counts][:files] || 50
  @thor.say_status :save, "config file", :green
  File.write(@config_path_old, conf.to_yaml)
end

#indexObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ayadn/migration.rb', line 257

def index
  @sql.execute_batch "    CREATE TABLE TLIndex (\n      count INTEGER,\n      post_id INTEGER,\n      content TEXT\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(@index_old)\n    @thor.say_status :import, \"Index database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @index.each do |k,v|\n        insert_data = {}\n        insert_data[\":post_id\"] = v[:id]\n        insert_data[\":count\"] = v[:count]\n        insert_data[\":content\"] = v.to_json.to_s\n        db_in_transaction.prepare(\"INSERT INTO TLIndex(count, post_id, content) VALUES(:count, :post_id, :content);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@index.size} objects\", :green\n    @index.close\n    File.delete(@index_old)\n    @thor.say_status :delete, @index_old, :green\n    Dir.rmdir(\"\#{@home}/pagination\")\n    @thor.say_status :delete, \"\#{@home}/pagination\", :green\n  end\nend\n"

#niceranksObject



194
195
196
197
198
199
# File 'lib/ayadn/migration.rb', line 194

def niceranks
  if File.exist?("#{@home}/db/nicerank.db")
    File.delete("#{@home}/db/nicerank.db")
    @thor.say_status :delete, "#{@home}/db/nicerank.db", :green
  end
end

#paginationObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ayadn/migration.rb', line 230

def pagination
  @sql.execute_batch "    CREATE TABLE Pagination (\n      name TEXT,\n      post_id INTEGER\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(@pagination_old)\n    @thor.say_status :import, \"Pagination database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @pagination.each do |k,v|\n        insert_data = {}\n        insert_data[\":post_id\"] = v.to_i\n        insert_data[\":name\"] = k.to_s\n        db_in_transaction.prepare(\"INSERT INTO Pagination(name, post_id) VALUES(:name, :post_id);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@pagination.size} objects\", :green\n    @pagination.close\n    File.delete(@pagination_old)\n    @thor.say_status :delete, @pagination_old, :green\n  end\nend\n"

#usersObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ayadn/migration.rb', line 201

def users
  @sql.execute_batch "    CREATE TABLE Users (\n      user_id INTEGER,\n      username VARCHAR(20),\n      name TEXT\n    );\n  SQL\n  @sql.reload_schema!\n  if File.exist?(\"\#{@home}/db/users.db\")\n    @thor.say_status :import, \"Users database\", :cyan\n    @sql.transaction do |db_in_transaction|\n      @users.each do |k,v|\n        insert_data = {}\n        insert_data[\":id\"] = k.to_i\n        insert_data[\":username\"] = v.keys[0]\n        insert_data[\":name\"] = v.values[0]\n        db_in_transaction.prepare(\"INSERT INTO Users(user_id, username, name) VALUES(:id, :username, :name);\") do |insert|\n          insert.execute(insert_data)\n        end\n      end\n    end\n    @thor.say_status :done, \"\#{@users.size} objects\", :green\n    @users.close\n    File.delete(\"\#{@home}/db/users.db\")\n    @thor.say_status :delete, \"\#{@home}/db/users.db\", :green\n  end\nend\n"