Merge pull request #545 from mposolda/master

Fix MySQL and PostgreSQL. More DB schema refactoring
This commit is contained in:
Stian Thorgersen 2014-07-22 09:10:52 +01:00
commit bfd62b0807
15 changed files with 83 additions and 46 deletions

View file

@ -3,34 +3,44 @@ package org.keycloak.audit.jpa;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@Entity @Entity
@Table(name="EVENT_ENTITY")
public class EventEntity { public class EventEntity {
@Id @Id
@Column(length = 36) @Column(name="ID", length = 36)
private String id; private String id;
@Column(name="TIME")
private long time; private long time;
@Column(name="EVENT")
private String event; private String event;
@Column(name="REALM_ID")
private String realmId; private String realmId;
@Column(name="CLIENT_ID")
private String clientId; private String clientId;
@Column(name="USER_ID")
private String userId; private String userId;
@Column(name="SESSION_ID")
private String sessionId; private String sessionId;
@Column(name="IP_ADDRESS")
private String ipAddress; private String ipAddress;
@Column(name="ERROR")
private String error; private String error;
@Column(length = 2550) @Column(name="DETAILS_JSON", length = 2550)
private String detailsJson; private String detailsJson;
public String getId() { public String getId() {

View file

@ -4,6 +4,7 @@ import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
@ -33,7 +34,7 @@ public class ApplicationEntity extends ClientEntity {
Collection<RoleEntity> roles = new ArrayList<RoleEntity>(); Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true) @OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="APPLICATION_DEFAULT_ROLES") @JoinTable(name="APPLICATION_DEFAULT_ROLES", joinColumns = { @JoinColumn(name="APPLICATION_ID")}, inverseJoinColumns = { @JoinColumn(name="ROLE_ID")})
Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>(); Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
public boolean isSurrogateAuthRequired() { public boolean isSurrogateAuthRequired() {

View file

@ -18,7 +18,7 @@ import java.util.Map;
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
@Entity @Entity
@Table(name="AUTH_PROVIDER_ENTITY") @Table(name="AUTH_PROVIDER")
@IdClass(AuthenticationProviderEntity.Key.class) @IdClass(AuthenticationProviderEntity.Key.class)
public class AuthenticationProviderEntity { public class AuthenticationProviderEntity {
@ -36,9 +36,11 @@ public class AuthenticationProviderEntity {
private int priority; private int priority;
@ElementCollection @ElementCollection
@MapKeyColumn(name="name") @MapKeyColumn(name="NAME")
@Column(name="value") @Column(name="VALUE")
@CollectionTable(name="AUTH_PROVIDER_CONFIG") @CollectionTable(name="AUTH_PROVIDER_CONFIG", joinColumns = {
@JoinColumn(name="REALM_ID", referencedColumnName = "REALM_ID"),
@JoinColumn(name="AUTH_PROVIDER_NAME", referencedColumnName = "PROVIDER_NAME")})
private Map<String, String> config; private Map<String, String> config;
public RealmEntity getRealm() { public RealmEntity getRealm() {

View file

@ -44,11 +44,13 @@ public abstract class ClientEntity {
protected RealmEntity realm; protected RealmEntity realm;
@ElementCollection @ElementCollection
@CollectionTable(name = "WEB_ORIGINS") @Column(name="VALUE")
@CollectionTable(name = "WEB_ORIGINS", joinColumns={ @JoinColumn(name="CLIENT_ID") })
protected Set<String> webOrigins = new HashSet<String>(); protected Set<String> webOrigins = new HashSet<String>();
@ElementCollection @ElementCollection
@CollectionTable(name = "REDIRECT_URIS") @Column(name="VALUE")
@CollectionTable(name = "REDIRECT_URIS", joinColumns={ @JoinColumn(name="CLIENT_ID") })
protected Set<String> redirectUris = new HashSet<String>(); protected Set<String> redirectUris = new HashSet<String>();
public RealmEntity getRealm() { public RealmEntity getRealm() {

View file

@ -25,7 +25,7 @@ import java.io.Serializable;
@Entity @Entity
public class CredentialEntity { public class CredentialEntity {
@Id @Id
@Column(length = 36) @Column(name="ID", length = 36)
protected String id; protected String id;
@Column(name="TYPE") @Column(name="TYPE")

View file

@ -108,41 +108,40 @@ public class RealmEntity {
@Column(name="EMAIL_THEME") @Column(name="EMAIL_THEME")
protected String emailTheme; protected String emailTheme;
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true) @OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
@JoinTable(name="USER_REQUIRED_CREDS")
Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>(); Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>();
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true) @OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
@JoinTable(name="AUTH_PROVIDERS")
List<AuthenticationProviderEntity> authenticationProviders = new ArrayList<AuthenticationProviderEntity>(); List<AuthenticationProviderEntity> authenticationProviders = new ArrayList<AuthenticationProviderEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true) @OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="REALM_APPLICATION", joinColumns={ @JoinColumn(name="APPLICATION_ID") }, inverseJoinColumns={ @JoinColumn(name="REALM_ID") })
Collection<ApplicationEntity> applications = new ArrayList<ApplicationEntity>(); Collection<ApplicationEntity> applications = new ArrayList<ApplicationEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm") @OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
Collection<RoleEntity> roles = new ArrayList<RoleEntity>(); Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
@ElementCollection @ElementCollection
@MapKeyColumn(name="name") @MapKeyColumn(name="NAME")
@Column(name="value") @Column(name="VALUE")
@CollectionTable(name="REALM_SMTP_CONFIG") @CollectionTable(name="REALM_SMTP_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> smtpConfig = new HashMap<String, String>(); protected Map<String, String> smtpConfig = new HashMap<String, String>();
@ElementCollection @ElementCollection
@MapKeyColumn(name="name") @MapKeyColumn(name="NAME")
@Column(name="value") @Column(name="VALUE")
@CollectionTable(name="REALM_SOCIAL_CONFIG") @CollectionTable(name="REALM_SOCIAL_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> socialConfig = new HashMap<String, String>(); protected Map<String, String> socialConfig = new HashMap<String, String>();
@ElementCollection @ElementCollection
@MapKeyColumn(name="name") @MapKeyColumn(name="NAME")
@Column(name="value") @Column(name="VALUE")
@CollectionTable(name="REALM_LDAP_CONFIG") @CollectionTable(name="REALM_LDAP_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> ldapServerConfig = new HashMap<String, String>(); protected Map<String, String> ldapServerConfig = new HashMap<String, String>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true) @OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="REALM_DEFAULT_ROLES") @JoinTable(name="REALM_DEFAULT_ROLES", joinColumns = { @JoinColumn(name="REALM_ID")}, inverseJoinColumns = { @JoinColumn(name="ROLE_ID")})
protected Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>(); protected Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
@Column(name="AUDIT_ENABLED") @Column(name="AUDIT_ENABLED")
@ -151,7 +150,8 @@ public class RealmEntity {
protected long auditExpiration; protected long auditExpiration;
@ElementCollection @ElementCollection
@CollectionTable(name="REALM_AUDIT_LISTENERS") @Column(name="VALUE")
@CollectionTable(name="REALM_AUDIT_LISTENERS", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Set<String> auditListeners= new HashSet<String>(); protected Set<String> auditListeners= new HashSet<String>();
@OneToOne @OneToOne

View file

@ -42,7 +42,7 @@ import java.util.Set;
@NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId") @NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId")
}) })
@Entity @Entity
@Table(name="USER", uniqueConstraints = { @Table(name="USER_ENTITY", uniqueConstraints = {
@UniqueConstraint(columnNames = { "REALM_ID", "USERNAME" }), @UniqueConstraint(columnNames = { "REALM_ID", "USERNAME" }),
@UniqueConstraint(columnNames = { "REALM_ID", "EMAIL_CONSTRAINT" }) @UniqueConstraint(columnNames = { "REALM_ID", "EMAIL_CONSTRAINT" })
}) })

View file

@ -2,6 +2,7 @@ package org.keycloak.models.jpa.entities;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
@ -10,6 +11,8 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -20,15 +23,17 @@ import java.io.Serializable;
@NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where realm=:realm)") @NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where realm=:realm)")
}) })
@Entity @Entity
@Table(name="USER_REQUIRED_ACTION")
@IdClass(UserRequiredActionEntity.Key.class) @IdClass(UserRequiredActionEntity.Key.class)
public class UserRequiredActionEntity { public class UserRequiredActionEntity {
@Id @Id
@ManyToOne(fetch= FetchType.LAZY) @ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="userId") @JoinColumn(name="USER_ID")
protected UserEntity user; protected UserEntity user;
@Id @Id
@Column(name="ACTION")
protected UserModel.RequiredAction action; protected UserModel.RequiredAction action;
public UserModel.RequiredAction getAction() { public UserModel.RequiredAction getAction() {

View file

@ -5,6 +5,7 @@ import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.IdClass; import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
@ -16,7 +17,7 @@ import java.io.Serializable;
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@Entity @Entity
@Table(name = "ClientUserSessionAscEntity") @Table(name = "CLIENT_USERSESSION")
@NamedQueries({ @NamedQueries({
@NamedQuery(name = "removeClientUserSessionByRealm", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId)"), @NamedQuery(name = "removeClientUserSessionByRealm", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId)"),
@NamedQuery(name = "removeClientUserSessionByUser", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId)"), @NamedQuery(name = "removeClientUserSessionByUser", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId)"),
@ -28,10 +29,11 @@ public class ClientUserSessionAssociationEntity {
@Id @Id
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SESSION_ID")
protected UserSessionEntity session; protected UserSessionEntity session;
@Id @Id
@Column(length = 36) @Column(name="CLIENT_ID",length = 36)
protected String clientId; protected String clientId;
public UserSessionEntity getSession() { public UserSessionEntity getSession() {

View file

@ -1,16 +1,15 @@
package org.keycloak.models.sessions.jpa.entities; package org.keycloak.models.sessions.jpa.entities;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -18,6 +17,7 @@ import java.util.Collection;
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@Entity @Entity
@Table(name = "USER_SESSION")
@NamedQueries({ @NamedQueries({
@NamedQuery(name = "getUserSessionByUser", query = "select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId order by s.started, s.id"), @NamedQuery(name = "getUserSessionByUser", query = "select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId order by s.started, s.id"),
@NamedQuery(name = "getUserSessionByClient", query = "select s from UserSessionEntity s join s.clients c where s.realmId = :realmId and c.clientId = :clientId order by s.started, s.id"), @NamedQuery(name = "getUserSessionByClient", query = "select s from UserSessionEntity s join s.clients c where s.realmId = :realmId and c.clientId = :clientId order by s.started, s.id"),
@ -29,16 +29,22 @@ import java.util.Collection;
public class UserSessionEntity { public class UserSessionEntity {
@Id @Id
@Column(length = 36) @Column(name="ID",length = 36)
protected String id; protected String id;
@Column(name="USER_ID")
protected String userId; protected String userId;
@Column(name="REALM_ID")
protected String realmId; protected String realmId;
@Column(name="IP_ADDRESS")
protected String ipAddress; protected String ipAddress;
@Column(name="STARTED")
protected int started; protected int started;
@Column(name="LAST_SESSION_REFRESH")
protected int lastSessionRefresh; protected int lastSessionRefresh;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="session") @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="session")

View file

@ -6,6 +6,8 @@ import javax.persistence.Id;
import javax.persistence.IdClass; import javax.persistence.IdClass;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -13,6 +15,7 @@ import java.io.Serializable;
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@Entity @Entity
@Table(name="USERNAME_LOGIN_FAILURE")
@NamedQueries({ @NamedQueries({
@NamedQuery(name="getAllFailures", query="select failure from UsernameLoginFailureEntity failure"), @NamedQuery(name="getAllFailures", query="select failure from UsernameLoginFailureEntity failure"),
@NamedQuery(name = "removeLoginFailuresByRealm", query = "delete from UsernameLoginFailureEntity f where f.realmId = :realmId"), @NamedQuery(name = "removeLoginFailuresByRealm", query = "delete from UsernameLoginFailureEntity f where f.realmId = :realmId"),
@ -22,16 +25,23 @@ import java.io.Serializable;
public class UsernameLoginFailureEntity { public class UsernameLoginFailureEntity {
@Id @Id
@Column(length = 200) @Column(name="USERNAME",length = 200)
protected String username; protected String username;
@Id @Id
@Column(length = 36) @Column(name="REALM_ID",length = 36)
protected String realmId; protected String realmId;
@Column(name="FAILED_LOGIN_NOT_BEFORE")
protected int failedLoginNotBefore; protected int failedLoginNotBefore;
@Column(name="NUM_FAILURES")
protected int numFailures; protected int numFailures;
@Column(name="LAST_FAILURE")
protected long lastFailure; protected long lastFailure;
@Column(name="LAST_IP_FAILURE")
protected String lastIPFailure; protected String lastIPFailure;
public String getUsername() { public String getUsername() {

View file

@ -402,13 +402,6 @@
<artifactId>keycloak-model-tests</artifactId> <artifactId>keycloak-model-tests</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View file

@ -66,7 +66,9 @@
"driverDialect": "${keycloak.connectionsJpa.driverDialect:}", "driverDialect": "${keycloak.connectionsJpa.driverDialect:}",
"user": "${keycloak.connectionsJpa.user:sa}", "user": "${keycloak.connectionsJpa.user:sa}",
"password": "${keycloak.connectionsJpa.password:}", "password": "${keycloak.connectionsJpa.password:}",
"databaseSchema": "${keycloak.connectionsJpa.databaseSchema:create-drop}" "databaseSchema": "${keycloak.connectionsJpa.databaseSchema:create-drop}",
"showSql": "${keycloak.connectionsJpa.showSql:false}",
"formatSql": "${keycloak.connectionsJpa.formatSql:true}"
} }
}, },

View file

@ -363,7 +363,7 @@ public class ExportImportTest {
private void pushProperty(String name, String value) { private void pushProperty(String name, String value) {
String currentValue = System.getProperty(name); String currentValue = System.getProperty(name);
if (currentValue != null) { if (currentValue != null) {
previousValues.put(name, value); previousValues.put(name, currentValue);
} }
System.setProperty(name, value); System.setProperty(name, value);
} }

View file

@ -45,7 +45,9 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
try { try {
RealmModel realmByName = session.realms().getRealmByName(realm); RealmModel realmByName = session.realms().getRealmByName(realm);
UserModel user = session.users().getUserByUsername(name, realmByName); UserModel user = session.users().getUserByUsername(name, realmByName);
return user != null ? ModelToRepresentation.toRepresentation(user) : null; UserRepresentation userRep = user != null ? ModelToRepresentation.toRepresentation(user) : null;
session.getTransaction().commit();
return userRep;
} finally { } finally {
session.close(); session.close();
} }
@ -56,7 +58,9 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
session.getTransaction().begin(); session.getTransaction().begin();
try { try {
RealmModel realmByName = session.realms().getRealmByName(realm); RealmModel realmByName = session.realms().getRealmByName(realm);
return ModelToRepresentation.toRepresentation(session.users().getUserById(id, realmByName)); UserRepresentation userRep = ModelToRepresentation.toRepresentation(session.users().getUserById(id, realmByName));
session.getTransaction().commit();
return userRep;
} finally { } finally {
session.close(); session.close();
} }