Mercurial Hosting > nabble
view src/nabble/model/DbSiteCreator.java @ 21:aba8ed4c8a06
semiprivate
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 13 Jun 2020 22:30:48 -0600 |
parents | abe0694e9849 |
children | b0e75dfe1853 |
line wrap: on
line source
package nabble.model; import fschmidt.db.DbDatabase; import fschmidt.util.java.IoUtils; import jdbcpgbackup.ZipBackup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.util.List; import java.util.ArrayList; public final class DbSiteCreator { private static final Logger logger = LoggerFactory.getLogger(DbSiteCreator.class); private static final File siteSchemaFile; static { String homeDir = (String)Init.get("home_dir"); siteSchemaFile = new File(homeDir+"src/nabble/data/site.schema"); } static Site restoreSiteToOldId(File file) { SiteKey siteKey; DbDatabase db = Db.dbPostgres(); db.beginTransaction(); try { ZipBackup backup = new ZipBackup( file, Db.completeUrl ); List<String> schemasInBackup = backup.schemasInBackup(); if( schemasInBackup.size() != 1 ) throw new RuntimeException("bad backup: "+schemasInBackup); String oldSchema = schemasInBackup.get(0); if( !oldSchema.startsWith("s") ) throw new RuntimeException("invalid schema: "+oldSchema); long siteId = Long.parseLong(oldSchema.substring(1)); SiteGlobal siteGlobal = new SiteGlobal(siteId); siteKey = siteGlobal.siteKey; restoreSiteSchema( file, siteKey.schema() ); db.commitTransaction(); } finally { db.endTransaction(); } return siteKey.site(); } static Site restoreSiteToNewId(File file) { SiteKey siteKey; DbDatabase db = Db.dbPostgres(); db.beginTransaction(); try { SiteGlobal siteGlobal = new SiteGlobal(); siteKey = siteGlobal.siteKey; restoreSiteSchema( file, siteKey.schema() ); db.commitTransaction(); } finally { db.endTransaction(); } return siteKey.site(); } static Site newSite(String type,String subject,String message,Message.Format msgFmt,String email,String username) throws ModelException { DbDatabase db = Db.dbPostgres(); if( !db.isInTransaction() ) { db.beginTransaction(); try { Site site = newSite(type,subject,message,msgFmt,email,username); db.commitTransaction(); return site; } finally { db.endTransaction(); } } SiteGlobal siteGlobal = new SiteGlobal(); SiteKey siteKey = siteGlobal.siteKey; String schema = siteKey.schema(); restoreSiteSchema( siteSchemaFile, schema ); if( DbUpdater.databaseVersion(Db.pooledDb(schema),schema) != DbSiteUpdater.version ) throw new RuntimeException("site.schema has wrong version"); SiteImpl site = new SiteImpl(siteKey); site.getDbRecord().insert(); UserImpl user = UserImpl.getOrCreateUnregisteredUser(site,email,username); NodeImpl rootNode = NodeImpl.newRootNode(Node.Kind.APP,user,subject,message,msgFmt); rootNode.setType(type); rootNode.insert(false); site.setRoot(rootNode); site.setSemiprivate(true); return site; } private static void restoreSiteSchema(File file,String schema) { try { restoreSchema(file,schema); } catch(SQLException e) { throw new RuntimeException("couldn't create schema "+schema,e); } } static void restoreSchema(File file,String schema) throws SQLException { ZipBackup backup = new ZipBackup( file, Db.completeUrl ); List<String> schemasInBackup = backup.schemasInBackup(); if( schemasInBackup.size() != 1 ) throw new RuntimeException("bad backup: "+schemasInBackup); String oldSchema = schemasInBackup.get(0); Db.checkUser(schema); Connection con = Db.dbPostgres().getConnection(); try { Statement stmt = con.createStatement(); stmt.executeUpdate( "CREATE SCHEMA AUTHORIZATION " + schema ); stmt.close(); } finally { con.close(); } backup.restoreSchemaTo(oldSchema,schema,Db.getNativeConnection()); } public static boolean isValid(String schema) { ZipBackup backup = new ZipBackup( siteSchemaFile, Db.completeUrl ); return backup.compareTo(schema); } private DbSiteCreator() {} // never }