view src/nabble/model/DbSiteCreator.java @ 35:5ea557eece1f

remove site.isNew()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 07 Jul 2020 09:57:53 -0600
parents b0e75dfe1853
children
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);
		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
}