Correctif sur absence de default_user et password dans fichier de config
[bilio:bilio.git] / src / config_db.rb
1 # coding: utf-8
2
3 class ConfigDb
4
5         attr_accessor :fichier, :conf, :bases, :scanner, :chemin_config, :chemin_log, :chemin_temp
6         
7         def initialize window
8         
9                 @window = window
10                 @chemin_temp = Dir.home+"/.bilio"
11                 Dir.mkdir(@chemin_temp) unless Dir.exist?(@chemin_temp)
12                 @chemin_config = @chemin_temp + "/configuration.yaml"
13                 @chemin_log = '../db/database.log'
14                 @fichier = {"connexions" => [], "scanner" => {}}
15                 @conf = {}
16                 @scanner = {}
17                 @bases = []
18                 charge_fichier @chemin_config
19                 
20         end
21         
22         def charge_fichier chemin_config
23         
24                 if File.exist?(chemin_config)
25                         p "Ouverture du fichier de configuration #{chemin_config}."
26                         @fichier = YAML.load_file(@chemin_config) if YAML.load_file(@chemin_config)
27                         @bases = @fichier["connexions"]
28                         @scanner = @fichier["scanner"]
29                 else
30                         p "Le fichier de configuration #{chemin_config} n'existe pas, il doit être créé."
31                 end
32                 
33         end
34         
35         def connexion id
36         
37                 @conf = @bases[id]
38                 @conf["id"] = id
39                         p "Connexion à la base de donnée #{@conf['database']} sur le serveur #{@conf['adapter']} #{@conf['host']}:#{@conf['port']}."
40                         ActiveRecord::Base.logger = Logger.new(File.open(@chemin_log,'w'))
41                         ActiveRecord::Base.establish_connection(@conf)
42                         
43                         lancement = false
44                         begin
45                                 last_version = 0
46                                 versions = Schema_migration.find(:all)
47                                 versions.each { |v|
48                                         last_version = v.version.to_i>last_version ? v.version.to_i : last_version
49                                 }
50                                 
51                                 if ActiveRecord::Base.connected?
52                                         p "Connexion établie. Version de la base de donnée = #{last_version}"
53                                         last_migrate = 0
54                                         Dir.foreach("../db/migrate") {|x| 
55                                                 num = x.to_s[0..2].to_i
56                                                 last_migrate=num if num>last_migrate 
57                                         }
58                                         p "Dernier numéro de migration = #{last_migrate}"
59                                         if last_version<last_migrate.to_i
60                                                 p "Lancement de la migration"
61                                                 creation_base
62                                         elsif last_version.eql?(last_migrate.to_i)
63                                                 lancement = true
64                                         else
65                                                 @window.message_erreur "Votre logiciel est trop ancien par rapport à la base de donnée, veuillez le mettre à jour"
66                                                 Process.exit
67                                         end
68                                 else
69                                         @window.message_erreur "Connexion impossible!\nVeuillez vérifier les paramètres de la base de données."
70                                 end
71                         rescue
72                                 if versions.nil?
73                                         begin
74                                                 base = ActiveRecord::Base.connection.current_database
75                                                 p "La base de données existe mais elle est vide"
76                                                 p "Lancement de la migration ..."
77                                                 creation_base
78                                         rescue
79                                                 @window.message_erreur "Connexion impossible!\nVeuillez vérifier les paramètres de la base de données."
80                                         end
81                                 elsif versions.length.eql?(0)
82                                         p "La table Schema_migration existe mais elle est vide."
83                                         p "Lancement de la migration ..."
84                                         creation_base
85                                 end
86                                 
87                         end
88                 
89                         return lancement
90
91         end
92         
93         def creation_base
94         
95                 dial_create_db = DialCreateDb.new @window, @conf["id"]
96                 dial_create_db.run { |response| 
97                         dial_create_db.destroy                  
98                 }
99                 
100                 connexion @conf["id"]
101         
102         end
103         
104         def save_config global=false
105                 
106                 @fichier["connexions"] = @bases
107                 @fichier["scanner"] = @scanner
108                 
109                 File.open(@chemin_config, 'w') do |out|
110                         YAML.dump(@fichier, out)
111                 end
112
113         end
114         
115 end