@@ -1628,7 +1628,16 @@ export class CoreSitesProvider {
16281628 * Register a site schema.
16291629 */
16301630 registerSiteSchema ( schema : CoreSiteSchema ) : void {
1631- this . siteSchemas [ schema . name ] = schema ;
1631+
1632+ if ( this . currentSite ) {
1633+ // Site has already been created, it's a schema probably added by site plugins. Add it only to current site.
1634+ const schemas : { [ name : string ] : CoreSiteSchema } = { } ;
1635+ schemas [ schema . name ] = schema ;
1636+
1637+ this . applySiteSchemas ( this . currentSite , schemas ) ;
1638+ } else {
1639+ this . siteSchemas [ schema . name ] = schema ;
1640+ }
16321641 }
16331642
16341643 /**
@@ -1638,7 +1647,6 @@ export class CoreSitesProvider {
16381647 * @return Promise resolved when done.
16391648 */
16401649 migrateSiteSchemas ( site : CoreSite ) : Promise < any > {
1641- const db = site . getDb ( ) ;
16421650
16431651 if ( this . siteSchemasMigration [ site . id ] ) {
16441652 return this . siteSchemasMigration [ site . id ] ;
@@ -1647,46 +1655,59 @@ export class CoreSitesProvider {
16471655 this . logger . debug ( `Migrating all schemas of ${ site . id } ` ) ;
16481656
16491657 // First create tables not registerd with name/version.
1650- const promise = db . createTablesFromSchema ( this . siteTablesSchemas ) . then ( ( ) => {
1651- // Fetch installed versions of the schema.
1652- return db . getAllRecords ( this . SCHEMA_VERSIONS_TABLE ) . then ( ( records ) => {
1653- const versions = { } ;
1654- records . forEach ( ( record ) => {
1655- versions [ record . name ] = record . version ;
1656- } ) ;
1658+ const promise = site . getDb ( ) . createTablesFromSchema ( this . siteTablesSchemas ) . then ( ( ) => {
1659+ return this . applySiteSchemas ( site , this . siteSchemas ) ;
1660+ } ) ;
16571661
1658- const promises = [ ] ;
1659- for ( const name in this . siteSchemas ) {
1660- const schema = this . siteSchemas [ name ] ;
1661- const oldVersion = versions [ name ] || 0 ;
1662- if ( oldVersion >= schema . version ) {
1663- continue ;
1664- }
1662+ this . siteSchemasMigration [ site . id ] = promise ;
16651663
1666- this . logger . debug ( `Migrating schema '${ name } ' of ${ site . id } from version ${ oldVersion } to ${ schema . version } ` ) ;
1664+ return promise . finally ( ( ) => {
1665+ delete this . siteSchemasMigration [ site . id ] ;
1666+ } ) ;
1667+ }
16671668
1668- let promise : Promise < any > = Promise . resolve ( ) ;
1669- if ( schema . tables ) {
1670- promise = promise . then ( ( ) => db . createTablesFromSchema ( schema . tables ) ) ;
1671- }
1672- if ( schema . migrate ) {
1673- promise = promise . then ( ( ) => schema . migrate ( db , oldVersion , site . id ) ) ;
1674- }
1669+ /**
1670+ * Install and upgrade the supplied schemas for a certain site.
1671+ *
1672+ * @param site Site.
1673+ * @param schemas Schemas to migrate.
1674+ * @return Promise resolved when done.
1675+ */
1676+ protected applySiteSchemas ( site : CoreSite , schemas : { [ name : string ] : CoreSiteSchema } ) : Promise < any > {
1677+ const db = site . getDb ( ) ;
16751678
1676- // Set installed version.
1677- promise = promise . then ( ( ) => db . insertRecord ( this . SCHEMA_VERSIONS_TABLE , { name, version : schema . version } ) ) ;
1679+ // Fetch installed versions of the schema.
1680+ return db . getAllRecords ( this . SCHEMA_VERSIONS_TABLE ) . then ( ( records ) => {
1681+ const versions = { } ;
1682+ records . forEach ( ( record ) => {
1683+ versions [ record . name ] = record . version ;
1684+ } ) ;
16781685
1679- promises . push ( promise ) ;
1686+ const promises = [ ] ;
1687+ for ( const name in schemas ) {
1688+ const schema = schemas [ name ] ;
1689+ const oldVersion = versions [ name ] || 0 ;
1690+ if ( oldVersion >= schema . version ) {
1691+ continue ;
16801692 }
16811693
1682- return Promise . all ( promises ) ;
1683- } ) ;
1684- } ) ;
1694+ this . logger . debug ( `Migrating schema '${ name } ' of ${ site . id } from version ${ oldVersion } to ${ schema . version } ` ) ;
16851695
1686- this . siteSchemasMigration [ site . id ] = promise ;
1696+ let promise : Promise < any > = Promise . resolve ( ) ;
1697+ if ( schema . tables ) {
1698+ promise = promise . then ( ( ) => db . createTablesFromSchema ( schema . tables ) ) ;
1699+ }
1700+ if ( schema . migrate ) {
1701+ promise = promise . then ( ( ) => schema . migrate ( db , oldVersion , site . id ) ) ;
1702+ }
16871703
1688- return promise . finally ( ( ) => {
1689- delete this . siteSchemasMigration [ site . id ] ;
1704+ // Set installed version.
1705+ promise = promise . then ( ( ) => db . insertRecord ( this . SCHEMA_VERSIONS_TABLE , { name, version : schema . version } ) ) ;
1706+
1707+ promises . push ( promise ) ;
1708+ }
1709+
1710+ return Promise . all ( promises ) ;
16901711 } ) ;
16911712 }
16921713
0 commit comments