2016-05-05 32 views
0

我們正在以每個租戶一個數據庫的方式設計一個SaaS系統。當新的租戶註冊時,會創建一個新的數據庫,並需要設置數據庫結構(方案,掛鉤,功能和系統數據)。如何將數據庫結構從模型數據庫複製到新數據庫?

現在,我想到了兩種解決方案:從模型DB複製/複製到新數據庫或基於SQL腳本設置新數據庫。我對OrientDB很新鮮,所以我重視你的任何建議。

在此先感謝。

回答

0

我勸你這些選項:

  1. 創建的第一個數據庫(我用下面的OSQL腳本):

    connect remote:localhost/BatchTest root root 
    
    CREATE CLASS Customer EXTENDS V 
    CREATE PROPERTY Customer.customerID INTEGER 
    CREATE PROPERTY Customer.State STRING 
    
    CREATE CLASS Store EXTENDS V 
    CREATE PROPERTY Store.storeID INTEGER 
    CREATE PROPERTY Store.State STRING 
    CREATE PROPERTY Store.Zip INTEGER 
    
    CREATE CLASS transaction EXTENDS E 
    CREATE PROPERTY transaction.amt DOUBLE 
    CREATE PROPERTY transaction.storeID INTEGER 
    CREATE PROPERTY transaction.customerID INTEGER 
    

    數據集

    orientdb {db=BatchTest}> select from v 
    
    ----+-----+--------+----------+-----+---------------+-------+----+-------------- 
    # |@RID |@CLASS |customerID|State|out_transaction|storeID|Zip |in_transaction 
    ----+-----+--------+----------+-----+---------------+-------+----+-------------- 
    0 |#12:0|Customer|1   |NY |[size=4]  |null |null|null 
    1 |#12:1|Customer|2   |NJ |[size=2]  |null |null|null 
    2 |#12:2|Customer|3   |PA |[size=2]  |null |null|null 
    3 |#12:3|Customer|4   |NY |[size=4]  |null |null|null 
    4 |#12:4|Customer|5   |NY |[size=2]  |null |null|null 
    5 |#13:0|Store |null  |NY |null   |1  |1 |[size=4] 
    6 |#13:1|Store |null  |NJ |null   |2  |3 |[size=6] 
    7 |#13:2|Store |null  |NY |null   |3  |2 |[size=4] 
    ----+-----+--------+----------+-----+---------------+-------+----+-------------- 
    

    類/記錄

    orientdb {db=BatchTest}> list classes 
    
    
    CLASSES 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    NAME           | SUPERCLASS       | CLUSTERS | RECORDS  | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    _studio          |         | 11   |    1 | 
    Customer          | [V]        | 12   |    5 | 
    E           |         | 10   |    0 | 
    OFunction         |         | 6   |    0 | 
    OIdentity         |         | -   |    0 | 
    ORestricted         |         | -   |    0 | 
    ORIDs          |         | 8   |    0 | 
    ORole          | [OIdentity]      | 4   |    3 | 
    OSchedule         |         | 7   |    0 | 
    OTriggered         |         | -   |    0 | 
    OUser          | [OIdentity]      | 5   |    3 | 
    Store          | [V]        | 13   |    3 | 
    transaction         | [E]        | 14   |    14 | 
    V           |         | 9   |    0 | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    TOTAL = 14                         29 | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    

我們只結構不復制數據集,您可以:

  • 創建一些新的空數據庫(每個用戶,組,...)和執行OSQL腳本每個數據庫只創建類/屬性;
  • 導出沒有數據集的數據庫。

如果您選擇第二種情況,可以使用帶有相關參數的EXPORTIMPORT命令。

  1. 連接到數據庫導出不記錄的結構(-includeRecords=false):

    orientdb {db=BatchTest}> export database C:/export/destination/path/exportTest.gz -includeRecords=false 
    Exporting current database to: database C:/export/destination/path/exportTest.gz -includeRecords=false in GZipped JSON format ... 
    
    Started export of database 'BatchTest' to C:/destination/path/exportTest.gz... 
    Exporting database info...OK 
    Exporting clusters...OK (15 clusters) 
    Exporting schema...OK (14 classes) 
    Exporting index info... 
    - Index OUser.name...OK 
    - Index dictionary...OK 
    - Index ORole.name...OK 
    OK (3 indexes) 
    Exporting manual indexes content... 
    - Exporting index dictionary ...OK (entries=0) 
    OK (1 manual indexes) 
    
    Database export completed in 55ms 
    
  2. 創建一個新的空數據庫:

    orientdb> create database remote:localhost/importTest root root plocal 
    
    Creating database [remote:localhost/importTest] using the storage type [plocal]... 
    Connecting to database [remote:localhost/importTest] with user 'admin'...OK 
    Database created successfully. 
    
    Current database is: remote:localhost/importTest 
    
  3. 導入導出的結構:

    orientdb {db=importTest}> import database C:/path/to/exportTest.gz 
    
    Importing database database C:/path/to/exportTest.gz... 
    Started import of database 'remote:localhost/importTest' from C:/path/to/exportTest.gz... 
    Non merge mode (-merge=false): removing all default non security classes 
    - Class E was removed. 
    - Class V was removed. 
    - Class ORestricted was removed. 
    - Class OTriggered was removed. 
    - Class OSchedule was removed. 
    - Class ORIDs was removed. 
    - Class OFunction was removed. 
    Removed 7 classes. 
    Importing database info...OK 
    Importing clusters... 
    - Creating cluster 'internal'...OK, assigned id=0 
    - Creating cluster 'default'...OK, assigned id=3 
    - Creating cluster 'orole'...OK, assigned id=4 
    - Creating cluster 'ouser'...OK, assigned id=5 
    - Creating cluster 'ofunction'...OK, assigned id=6 
    - Creating cluster 'oschedule'...OK, assigned id=7 
    - Creating cluster 'orids'...OK, assigned id=8 
    - Creating cluster 'v'...OK, assigned id=9 
    - Creating cluster 'e'...OK, assigned id=10 
    - Creating cluster '_studio'...OK, assigned id=11 
    - Creating cluster 'customer'...OK, assigned id=12 
    - Creating cluster 'store'...OK, assigned id=13 
    - Creating cluster 'transaction'...OK, assigned id=14 
    Rebuilding indexes of truncated clusters ... 
    Done 2 indexes were rebuilt. 
    Done. Imported 13 clusters 
    Importing database schema...OK (14 classes) 
    
    Importing indexes ... 
    - Index 'OUser.name'...OK 
    - Index 'dictionary'...OK 
    - Index 'ORole.name'...OK 
    Done. Created 3 indexes. 
    Importing manual index entries... 
    - Index 'dictionary'...OK (0 entries) 
    Done. Imported 1 indexes. 
    Rebuild of stale indexes... 
    Stale indexes were rebuilt... 
    Deleting RID Mapping table...OK 
    
    
    Database import completed in 7501 ms 
    
    orientdb {db=importTest}> list classes 
    

    結構:

    CLASSES 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    NAME           | SUPERCLASS       | CLUSTERS | RECORDS  | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    _studio          |         | 11   |    0 | 
    Customer          | [V]        | 12   |    0 | 
    E           |         | 10   |    0 | 
    OFunction         |         | 6   |    0 | 
    OIdentity         |         | -   |    0 | 
    ORestricted         |         | -   |    0 | 
    ORIDs          |         | 8   |    0 | 
    ORole          | [OIdentity]      | 4   |    0 | 
    OSchedule         |         | 7   |    0 | 
    OTriggered         |         | -   |    0 | 
    OUser          | [OIdentity]      | 5   |    0 | 
    Store          | [V]        | 13   |    0 | 
    transaction         | [E]        | 14   |    0 | 
    V           |         | 9   |    0 | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    TOTAL = 14                         0 | 
    ----------------------------------------------+------------------------------------+------------+----------------+ 
    

    數據集:

    orientdb {db=importTest}> select from v 
    
    
    0 item(s) found. Query executed in 0.004 sec(s). 
    
  4. 你可以有很多的參數(official OrientDB documentation)自定義導出的數據庫。

希望它可以幫助

+0

嗨@LucaS。謝謝。如果數據庫複製必須在運行時自動完成(而新用戶在線註冊),則必須選擇CASE 1 - 「OSQL腳本」。你的意思是?您知道,我們不希望爲每個新用戶進行離線和手動操作,這會導致錯誤的UE和巨大的成本。 – Sunrise

相關問題