Mercurial Hosting > nabble
annotate src/nabble/model/DbSiteCreator.java @ 2:abe0694e9849
replace local_dir with home_dir
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 25 Mar 2019 13:59:13 -0600 |
| parents | 7ecd1a4ef557 |
| children | aba8ed4c8a06 |
| rev | line source |
|---|---|
| 0 | 1 package nabble.model; |
| 2 | |
| 3 import fschmidt.db.DbDatabase; | |
| 4 import fschmidt.util.java.IoUtils; | |
| 5 import jdbcpgbackup.ZipBackup; | |
| 6 import org.slf4j.Logger; | |
| 7 import org.slf4j.LoggerFactory; | |
| 8 | |
| 9 import java.io.File; | |
| 10 import java.io.InputStream; | |
| 11 import java.io.OutputStream; | |
| 12 import java.io.FileOutputStream; | |
| 13 import java.io.IOException; | |
| 14 import java.net.URL; | |
| 15 import java.sql.Connection; | |
| 16 import java.sql.SQLException; | |
| 17 import java.sql.Statement; | |
| 18 import java.sql.ResultSet; | |
| 19 import java.util.List; | |
| 20 import java.util.ArrayList; | |
| 21 | |
| 22 | |
| 23 public final class DbSiteCreator { | |
| 24 private static final Logger logger = LoggerFactory.getLogger(DbSiteCreator.class); | |
| 25 | |
|
2
abe0694e9849
replace local_dir with home_dir
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
26 private static final File siteSchemaFile; |
|
abe0694e9849
replace local_dir with home_dir
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
27 static { |
|
abe0694e9849
replace local_dir with home_dir
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
28 String homeDir = (String)Init.get("home_dir"); |
|
abe0694e9849
replace local_dir with home_dir
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
29 siteSchemaFile = new File(homeDir+"src/nabble/data/site.schema"); |
|
abe0694e9849
replace local_dir with home_dir
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
30 } |
| 0 | 31 |
| 32 static Site restoreSiteToOldId(File file) { | |
| 33 SiteKey siteKey; | |
| 34 DbDatabase db = Db.dbPostgres(); | |
| 35 db.beginTransaction(); | |
| 36 try { | |
| 37 ZipBackup backup = new ZipBackup( file, Db.completeUrl ); | |
| 38 List<String> schemasInBackup = backup.schemasInBackup(); | |
| 39 if( schemasInBackup.size() != 1 ) | |
| 40 throw new RuntimeException("bad backup: "+schemasInBackup); | |
| 41 String oldSchema = schemasInBackup.get(0); | |
| 42 if( !oldSchema.startsWith("s") ) | |
| 43 throw new RuntimeException("invalid schema: "+oldSchema); | |
| 44 long siteId = Long.parseLong(oldSchema.substring(1)); | |
| 45 SiteGlobal siteGlobal = new SiteGlobal(siteId); | |
| 46 siteKey = siteGlobal.siteKey; | |
| 47 restoreSiteSchema( file, siteKey.schema() ); | |
| 48 db.commitTransaction(); | |
| 49 } finally { | |
| 50 db.endTransaction(); | |
| 51 } | |
| 52 return siteKey.site(); | |
| 53 } | |
| 54 | |
| 55 static Site restoreSiteToNewId(File file) { | |
| 56 SiteKey siteKey; | |
| 57 DbDatabase db = Db.dbPostgres(); | |
| 58 db.beginTransaction(); | |
| 59 try { | |
| 60 SiteGlobal siteGlobal = new SiteGlobal(); | |
| 61 siteKey = siteGlobal.siteKey; | |
| 62 restoreSiteSchema( file, siteKey.schema() ); | |
| 63 db.commitTransaction(); | |
| 64 } finally { | |
| 65 db.endTransaction(); | |
| 66 } | |
| 67 return siteKey.site(); | |
| 68 } | |
| 69 | |
| 70 static Site newSite(String type,String subject,String message,Message.Format msgFmt,String email,String username) | |
| 71 throws ModelException | |
| 72 { | |
| 73 DbDatabase db = Db.dbPostgres(); | |
| 74 if( !db.isInTransaction() ) { | |
| 75 db.beginTransaction(); | |
| 76 try { | |
| 77 Site site = newSite(type,subject,message,msgFmt,email,username); | |
| 78 db.commitTransaction(); | |
| 79 return site; | |
| 80 } finally { | |
| 81 db.endTransaction(); | |
| 82 } | |
| 83 } | |
| 84 SiteGlobal siteGlobal = new SiteGlobal(); | |
| 85 SiteKey siteKey = siteGlobal.siteKey; | |
| 86 String schema = siteKey.schema(); | |
| 87 restoreSiteSchema( siteSchemaFile, schema ); | |
| 88 if( DbUpdater.databaseVersion(Db.pooledDb(schema),schema) != DbSiteUpdater.version ) | |
| 89 throw new RuntimeException("site.schema has wrong version"); | |
| 90 SiteImpl site = new SiteImpl(siteKey); | |
| 91 site.getDbRecord().insert(); | |
| 92 UserImpl user = UserImpl.getOrCreateUnregisteredUser(site,email,username); | |
| 93 NodeImpl rootNode = NodeImpl.newRootNode(Node.Kind.APP,user,subject,message,msgFmt); | |
| 94 rootNode.setType(type); | |
| 95 rootNode.insert(false); | |
| 96 site.setRoot(rootNode); | |
| 97 return site; | |
| 98 } | |
| 99 | |
| 100 private static void restoreSiteSchema(File file,String schema) { | |
| 101 try { | |
| 102 restoreSchema(file,schema); | |
| 103 } catch(SQLException e) { | |
| 104 throw new RuntimeException("couldn't create schema "+schema,e); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 static void restoreSchema(File file,String schema) throws SQLException { | |
| 109 ZipBackup backup = new ZipBackup( file, Db.completeUrl ); | |
| 110 List<String> schemasInBackup = backup.schemasInBackup(); | |
| 111 if( schemasInBackup.size() != 1 ) | |
| 112 throw new RuntimeException("bad backup: "+schemasInBackup); | |
| 113 String oldSchema = schemasInBackup.get(0); | |
| 114 Db.checkUser(schema); | |
| 115 Connection con = Db.dbPostgres().getConnection(); | |
| 116 try { | |
| 117 Statement stmt = con.createStatement(); | |
| 118 stmt.executeUpdate( | |
| 119 "CREATE SCHEMA AUTHORIZATION " + schema | |
| 120 ); | |
| 121 stmt.close(); | |
| 122 } finally { | |
| 123 con.close(); | |
| 124 } | |
| 125 backup.restoreSchemaTo(oldSchema,schema,Db.getNativeConnection()); | |
| 126 } | |
| 127 | |
| 128 public static boolean isValid(String schema) { | |
| 129 ZipBackup backup = new ZipBackup( siteSchemaFile, Db.completeUrl ); | |
| 130 return backup.compareTo(schema); | |
| 131 } | |
| 132 | |
| 133 private DbSiteCreator() {} // never | |
| 134 } |
