Sunrise
Sunrise

Reputation: 71

how to copy database structure from the model database to a new database?

We are designing a SaaS system in a way of one database per tenant. When a new tenant signs up, a new database is created and the database structure(schemes, hooks, functions and system data) need to be setup.

Now, two solutions come to my mind: copying/replicating from the model DB to the new DB or setup the new DB based on SQL scripts. I'm fresh to OrientDB, so I valued any of your advice.

Thanks in advance.

Upvotes: 0

Views: 719

Answers (1)

LucaS
LucaS

Reputation: 1418

I advise you these options:

  1. Create the first database (I used the following OSQL script):

    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
    

    Dataset:

    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]
    ----+-----+--------+----------+-----+---------------+-------+----+--------------
    

    Classes/records:

    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 |
    ----------------------------------------------+------------------------------------+------------+----------------+
    

Now to copy only the structure without dataset you can:

  • Create some new empty databases (per user, group,...) and execute the OSQL script on each database to create only classes/properties;
  • Export the database without dataset.

If you choose the second case, you can use the EXPORT and IMPORT commands with relative parameters.

  1. Connect to the database to export the structure without records (-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. Create a new empty database:

    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. Import the structure exported:

    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
    

    Structure:

    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 |
    ----------------------------------------------+------------------------------------+------------+----------------+
    

    Dataset:

    orientdb {db=importTest}> select from v
    
    
    0 item(s) found. Query executed in 0.004 sec(s).
    
  4. You can customize the exported database with a lot of parameters (official OrientDB documentation).

Hope it helps

Upvotes: 0

Related Questions