Skip to content
Snippets Groups Projects
Commit 73f84a39 authored by Félix  Sidokhine's avatar Félix Sidokhine Committed by Adrien Béraud
Browse files

Replaced in-code inits with Flyaway Migrations

Change-Id: I382ea5cad8e296e8406079746d46f904f7be68a4
parent 1a07527e
Branches
No related tags found
No related merge requests found
Showing
with 221 additions and 293 deletions
...@@ -23,10 +23,9 @@ ...@@ -23,10 +23,9 @@
<version>${debry.version}</version> <version>${debry.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.jami</groupId> <groupId>org.flywaydb</groupId>
<artifactId>jams-common</artifactId> <artifactId>flyway-core</artifactId>
<version>${revision}</version> <version>${flyway.version}</version>
<scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -36,24 +36,8 @@ import java.util.List; ...@@ -36,24 +36,8 @@ import java.util.List;
public class ContactDao extends AbstractDao<Contact> { public class ContactDao extends AbstractDao<Contact> {
public ContactDao() { public ContactDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("contacts"); this.setTableName("contacts");
this.setTClass(Contact.class); this.setTClass(Contact.class);
String createTable = "CREATE TABLE contacts (" +
"owner varchar(255), " +
"uri varchar(255)," +
"displayName varchar(255)," +
"`timestamp` bigint," +
"status int," +
"PRIMARY KEY (owner,uri))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
} catch (SQLException e) {
log.error("Could not create the contacts table with error " + e.getMessage());
} finally {
DataStore.connectionPool.returnConnection(connection);
}
} }
//Not used because the strategy here is different. //Not used because the strategy here is different.
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -35,26 +36,8 @@ import java.sql.SQLException; ...@@ -35,26 +36,8 @@ import java.sql.SQLException;
public class DeviceDao extends AbstractDao<Device> { public class DeviceDao extends AbstractDao<Device> {
public DeviceDao() { public DeviceDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("devices"); this.setTableName("devices");
this.setTClass(Device.class); this.setTClass(Device.class);
String createTable = "CREATE TABLE devices (" +
"deviceId varchar(255), " +
"owner varchar(255)," +
"displayName varchar(255)," +
"certificate varchar(5000), "+
"privatekey varchar(5000)," +
"PRIMARY KEY (deviceId))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the device table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
}
} }
@Override @Override
...@@ -67,12 +50,10 @@ public class DeviceDao extends AbstractDao<Device> { ...@@ -67,12 +50,10 @@ public class DeviceDao extends AbstractDao<Device> {
"(?, ?, ?, ?, ?)"); "(?, ?, ?, ?, ?)");
ps = object.getInsert(ps); ps = object.getInsert(ps);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to store a user: " + e.toString()); log.error("An error has occurred while trying to store a user: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
...@@ -91,12 +72,10 @@ public class DeviceDao extends AbstractDao<Device> { ...@@ -91,12 +72,10 @@ public class DeviceDao extends AbstractDao<Device> {
ps.setString(2, user); ps.setString(2, user);
ps.setString(3, deviceId); ps.setString(3, deviceId);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to update a user: " + e.toString()); log.error("An error has occurred while trying to update a user: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import com.nimbusds.jwt.SignedJWT; import com.nimbusds.jwt.SignedJWT;
...@@ -40,26 +41,9 @@ public class JwtDao extends AbstractDao<SignedJWT> { ...@@ -40,26 +41,9 @@ public class JwtDao extends AbstractDao<SignedJWT> {
private static final String SQL_GET_TOKEN = "SELECT COUNT(token) FROM tokens WHERE token = ?"; private static final String SQL_GET_TOKEN = "SELECT COUNT(token) FROM tokens WHERE token = ?";
public JwtDao() { public JwtDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("tokens"); this.setTableName("tokens");
this.setTClass(SignedJWT.class); this.setTClass(SignedJWT.class);
String createTable = "CREATE TABLE tokens (" +
"userid varchar(255), " +
"deviceId varchar(255)," +
"token varchar(255)," +
"PRIMARY KEY (userid, deviceId))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the device table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
} }
}
@Override @Override
public boolean storeObject(SignedJWT object) { public boolean storeObject(SignedJWT object) {
...@@ -72,7 +56,6 @@ public class JwtDao extends AbstractDao<SignedJWT> { ...@@ -72,7 +56,6 @@ public class JwtDao extends AbstractDao<SignedJWT> {
return true; return true;
} }
//This method is not needed because we are only concerned with the existence of a token, //This method is not needed because we are only concerned with the existence of a token,
//we never actually look them up. //we never actually look them up.
@Override @Override
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -35,24 +36,8 @@ import java.sql.SQLException; ...@@ -35,24 +36,8 @@ import java.sql.SQLException;
public class SystemDao extends AbstractDao<SystemAccount> { public class SystemDao extends AbstractDao<SystemAccount> {
public SystemDao() { public SystemDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("system"); this.setTableName("system");
this.setTClass(SystemAccount.class); this.setTClass(SystemAccount.class);
String createTable = "CREATE TABLE "+ this.getTableName() + " (" +
"entity varchar(255), " +
"certificate varchar(5000), "+
"privatekey varchar(5000)," +
"PRIMARY KEY (entity))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the device table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
}
} }
@Override @Override
...@@ -65,12 +50,10 @@ public class SystemDao extends AbstractDao<SystemAccount> { ...@@ -65,12 +50,10 @@ public class SystemDao extends AbstractDao<SystemAccount> {
"(?, ?, ?)"); "(?, ?, ?)");
ps = object.getInsert(ps); ps = object.getInsert(ps);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to store a system entity: " + e.toString()); log.error("An error has occurred while trying to store a system entity: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -35,32 +36,8 @@ import java.sql.SQLException; ...@@ -35,32 +36,8 @@ import java.sql.SQLException;
public class UserDao extends AbstractDao<User> { public class UserDao extends AbstractDao<User> {
public UserDao() { public UserDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("users"); this.setTableName("users");
this.setTClass(User.class); this.setTClass(User.class);
String createTable = "CREATE TABLE users (" +
"username varchar(255), " +
"password varchar(255)," +
"userType varchar(10)," +
"realm varchar(255)," +
"ethAddress varchar(255)," +
"ethKey varchar(255)," +
"jamiId varchar(255)," +
"certificate varchar(5000), "+
"privatekey varchar(5000)," +
"accessLevel varchar(10),"+
"needsPasswordReset varchar(10),"+
"PRIMARY KEY (username))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the device table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
}
} }
@Override @Override
...@@ -74,12 +51,10 @@ public class UserDao extends AbstractDao<User> { ...@@ -74,12 +51,10 @@ public class UserDao extends AbstractDao<User> {
"(?, ?, ?, ?, ?, ?, ?,?, ?,?, ?)"); "(?, ?, ?, ?, ?, ?, ?,?, ?,?, ?)");
ps = object.getInsert(ps); ps = object.getInsert(ps);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to store a user: " + e.toString()); log.error("An error has occurred while trying to store a user: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
...@@ -91,8 +66,9 @@ public class UserDao extends AbstractDao<User> { ...@@ -91,8 +66,9 @@ public class UserDao extends AbstractDao<User> {
String user = constraints.getStatements().get(0).getValue(); String user = constraints.getStatements().get(0).getValue();
String pwReset = "false"; String pwReset = "false";
if (update.getStatements().size() > 1) if (update.getStatements().size() > 1) {
pwReset = update.getStatements().get(1).getValue(); pwReset = update.getStatements().get(1).getValue();
}
SQLConnection connection = DataStore.connectionPool.getConnection(); SQLConnection connection = DataStore.connectionPool.getConnection();
...@@ -107,12 +83,10 @@ public class UserDao extends AbstractDao<User> { ...@@ -107,12 +83,10 @@ public class UserDao extends AbstractDao<User> {
ps.setString(2, user); ps.setString(2, user);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to update a user: " + e.toString()); log.error("An error has occurred while trying to update a user: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -37,34 +38,8 @@ public class UserProfileDao extends AbstractDao<UserProfile> { ...@@ -37,34 +38,8 @@ public class UserProfileDao extends AbstractDao<UserProfile> {
//Fis this to include the fields from AD/LDAP. //Fis this to include the fields from AD/LDAP.
public UserProfileDao() { public UserProfileDao() {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
this.setTableName("local_directory"); this.setTableName("local_directory");
this.setTClass(UserProfile.class); this.setTClass(UserProfile.class);
String createTable = "CREATE TABLE local_directory (" +
"username varchar(255), " +
"firstName varchar(255), " +
"lastName varchar(255), " +
"email varchar(255), " +
"profilePicture CLOB, " +
"organization varchar(255), " +
"phoneNumber varchar(255), " +
"phoneNumberExtension varchar(255), " +
"faxNumber varchar(255), " +
"mobileNumber varchar(255), " +
"PRIMARY KEY (username))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the user profile table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
}
} }
@Override @Override
...@@ -77,12 +52,10 @@ public class UserProfileDao extends AbstractDao<UserProfile> { ...@@ -77,12 +52,10 @@ public class UserProfileDao extends AbstractDao<UserProfile> {
" VALUES " + "(?,?,?,?,?,?,?,?,?,?)"); " VALUES " + "(?,?,?,?,?,?,?,?,?,?)");
ps = object.getInsert(ps); ps = object.getInsert(ps);
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch (Exception e){
log.error("An error has occurred while trying to store a user profile: " + e.toString()); log.error("An error has occurred while trying to store a user profile: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
...@@ -98,12 +71,10 @@ public class UserProfileDao extends AbstractDao<UserProfile> { ...@@ -98,12 +71,10 @@ public class UserProfileDao extends AbstractDao<UserProfile> {
} }
ps.setString(update.getStatements().size(), update.getStatements().get(0).getValue()); ps.setString(update.getStatements().size(), update.getStatements().get(0).getValue());
return ps.executeUpdate() != 0; return ps.executeUpdate() != 0;
} } catch (Exception e) {
catch(Exception e){
log.error("An error has occurred while trying to update a user profile: " + e.toString()); log.error("An error has occurred while trying to update a user profile: " + e.toString());
return false; return false;
} } finally {
finally {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
......
...@@ -38,6 +38,7 @@ import net.jami.jams.common.dao.StatementList; ...@@ -38,6 +38,7 @@ import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.dao.connectivity.ConnectionPool; import net.jami.jams.common.dao.connectivity.ConnectionPool;
import net.jami.jams.common.objects.user.User; import net.jami.jams.common.objects.user.User;
import net.jami.jams.common.objects.user.UserProfile; import net.jami.jams.common.objects.user.UserProfile;
import org.flywaydb.core.Flyway;
import java.util.List; import java.util.List;
...@@ -55,6 +56,8 @@ public class DataStore implements AuthenticationSource { ...@@ -55,6 +56,8 @@ public class DataStore implements AuthenticationSource {
//Implicitly connect to derby. //Implicitly connect to derby.
public DataStore(String connectionString) { public DataStore(String connectionString) {
Flyway flyway = Flyway.configure().dataSource(connectionString,"", "").load();
flyway.migrate();
connectionPool = new ConnectionPool(connectionString); connectionPool = new ConnectionPool(connectionString);
userDao = new UserDao(); userDao = new UserDao();
deviceDao = new DeviceDao(); deviceDao = new DeviceDao();
......
CREATE TABLE contacts (owner varchar(255),
uri varchar(255), displayName varchar(255) ,
timestamp bigint, status int,
PRIMARY KEY (owner,uri));
\ No newline at end of file
CREATE TABLE devices (deviceId varchar(255),
owner varchar(255), displayName varchar(255),
certificate varchar(5000), privatekey varchar(5000),
PRIMARY KEY (deviceId));
\ No newline at end of file
CREATE TABLE system (entity varchar(255),
certificate varchar(5000), privatekey varchar(5000),
PRIMARY KEY (entity));
\ No newline at end of file
CREATE TABLE users ( username varchar(255),
password varchar(255),userType varchar(10),
realm varchar(255),ethAddress varchar(255),
ethKey varchar(255),jamiId varchar(255),
certificate varchar(5000),privatekey varchar(5000),
accessLevel varchar(10),needsPasswordReset varchar(10),
PRIMARY KEY (username));
\ No newline at end of file
CREATE TABLE local_directory ( username varchar(255),
firstName varchar(255),lastName varchar(255),
email varchar(255),profilePicture CLOB,
organization varchar(255),phoneNumber varchar(255),
phoneNumberExtension varchar(255),
faxNumber varchar(255),mobileNumber varchar(255),
PRIMARY KEY (username));
\ No newline at end of file
...@@ -30,6 +30,7 @@ import net.jami.jams.common.objects.devices.Device; ...@@ -30,6 +30,7 @@ import net.jami.jams.common.objects.devices.Device;
import net.jami.jams.common.objects.user.AccessLevel; import net.jami.jams.common.objects.user.AccessLevel;
import net.jami.jams.common.objects.user.User; import net.jami.jams.common.objects.user.User;
import net.jami.jams.common.utils.X509Utils; import net.jami.jams.common.utils.X509Utils;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
......
CREATE TABLE contacts (owner varchar(255),
uri varchar(255), displayName varchar(255) ,
timestamp bigint, status int,
PRIMARY KEY (owner,uri));
\ No newline at end of file
CREATE TABLE devices (deviceId varchar(255),
owner varchar(255), displayName varchar(255),
certificate varchar(5000), privatekey varchar(5000),
PRIMARY KEY (deviceId));
\ No newline at end of file
CREATE TABLE system (entity varchar(255),
certificate varchar(5000), privatekey varchar(5000),
PRIMARY KEY (entity));
\ No newline at end of file
CREATE TABLE users ( username varchar(255),
password varchar(255),userType varchar(10),
realm varchar(255),ethAddress varchar(255),
ethKey varchar(255),jamiId varchar(255),
certificate varchar(5000),privatekey varchar(5000),
accessLevel varchar(10),needsPasswordReset varchar(10),
PRIMARY KEY (username));
\ No newline at end of file
CREATE TABLE local_directory ( username varchar(255),
firstName varchar(255),lastName varchar(255),
email varchar(255),profilePicture CLOB,
organization varchar(255),phoneNumber varchar(255),
phoneNumberExtension varchar(255),
faxNumber varchar(255),mobileNumber varchar(255),
PRIMARY KEY (username));
\ No newline at end of file
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<junit.surefire.version>1.1.0</junit.surefire.version> <junit.surefire.version>1.1.0</junit.surefire.version>
<jsoniter.version>0.9.23</jsoniter.version> <jsoniter.version>0.9.23</jsoniter.version>
<javassist.version>3.27.0-GA</javassist.version> <javassist.version>3.27.0-GA</javassist.version>
<debry.version>10.11.1.1</debry.version> <debry.version>10.14.2.0</debry.version>
<msgpack.version>0.8.16</msgpack.version> <msgpack.version>0.8.16</msgpack.version>
<commons.codec.version>1.11</commons.codec.version> <commons.codec.version>1.11</commons.codec.version>
<xbean.version>4.17</xbean.version> <xbean.version>4.17</xbean.version>
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
<embedded.ldap.unit>0.8.1</embedded.ldap.unit> <embedded.ldap.unit>0.8.1</embedded.ldap.unit>
<maven.exec.version>1.1.1</maven.exec.version> <maven.exec.version>1.1.1</maven.exec.version>
<zmq.version>0.5.2</zmq.version> <zmq.version>0.5.2</zmq.version>
<flyway.version>6.5.1</flyway.version>
</properties> </properties>
<dependencies> <dependencies>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment