diff src/nabble/model/DbSiteCreator.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children abe0694e9849
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/model/DbSiteCreator.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,130 @@
+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 = new File("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);
+		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
+}