From 56ef67ec6d6cdc4c09dc7147d82c5dbe1400ef41 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 24 Apr 2008 15:00:42 +0000 Subject: [PATCH] auto table creation for nhibernate backends --- .../Data/NHibernate/NHibernateAssetData.cs | 29 ++++++++++++++++++- .../NHibernate/NHibernateInventoryData.cs | 29 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs index beac6934e2..740868bab5 100644 --- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs +++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs @@ -28,11 +28,13 @@ using System; using System.IO; using System.Reflection; +using System.Text.RegularExpressions; using libsecondlife; using log4net; using NHibernate; using NHibernate.Cfg; using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -58,6 +60,10 @@ namespace OpenSim.Data.NHibernate // Split out the dialect, driver, and connect string char[] split = {';'}; string[] parts = connect.Split(split); + if (parts.Length != 3) { + // TODO: make this a real exception type + throw new Exception("Malformed Inventory connection string '" + connect + "'"); + } // NHibernate setup cfg = new Configuration(); @@ -75,13 +81,34 @@ namespace OpenSim.Data.NHibernate HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) cfg.AddInputStream(stream); + factory = cfg.BuildSessionFactory(); + // If uncommented this will auto create tables, but it // does drops of the old tables, so we need a smarter way // to acturally manage this. // new SchemaExport(cfg).Create(true, true); - factory = cfg.BuildSessionFactory(); + InitDB(); + } + + private void InitDB() + { + string regex = @"no such table: Assets"; + Regex RE = new Regex(regex, RegexOptions.Multiline); + try { + using(ISession session = factory.OpenSession()) { + session.Load(typeof(AssetBase), LLUUID.Zero); + } + } catch (ObjectNotFoundException e) { + // yes, we know it's not there, but that's ok + } catch (ADOException e) { + Match m = RE.Match(e.ToString()); + if(m.Success) { + // We don't have this table, so create it. + new SchemaExport(cfg).Create(true, true); + } + } } override public AssetBase FetchAsset(LLUUID uuid) diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs index 938f47c2b4..08d9dc157b 100644 --- a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs +++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs @@ -29,12 +29,14 @@ using System; using System.Collections.Generic; using System.IO; using System.Reflection; +using System.Text.RegularExpressions; using libsecondlife; using log4net; using NHibernate; using NHibernate.Cfg; using NHibernate.Expression; using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -55,6 +57,10 @@ namespace OpenSim.Data.NHibernate // Split out the dialect, driver, and connect string char[] split = {';'}; string[] parts = connect.Split(split); + if (parts.Length != 3) { + // TODO: make this a real exception type + throw new Exception("Malformed Inventory connection string '" + connect + "'"); + } // Establish NHibernate Connection cfg = new Configuration(); @@ -79,8 +85,31 @@ namespace OpenSim.Data.NHibernate // new SchemaExport(cfg).Create(true, true); factory = cfg.BuildSessionFactory(); + + InitDB(); } + + private void InitDB() + { + string regex = @"no such table: Inventory"; + Regex RE = new Regex(regex, RegexOptions.Multiline); + try { + using(ISession session = factory.OpenSession()) { + session.Load(typeof(InventoryItemBase), LLUUID.Zero); + } + } catch (ObjectNotFoundException e) { + // yes, we know it's not there, but that's ok + } catch (ADOException e) { + Match m = RE.Match(e.ToString()); + if(m.Success) { + // We don't have this table, so create it. + new SchemaExport(cfg).Create(true, true); + } + } + } + + /***************************************************************** * * Basic CRUD operations on Data