
학교에서 들었던 콘텐츠데이터베이스 수업 이후로 데이터베이스 관련 공부를 한건 정말 오랜만이었다! 그래도 나름 데이터베이스 수업을 재밌게 들었던터라(+ 가르쳐주신 교수님 강의력이 짱이었음 ,,) 오늘 수업이 되게 즐거웠다(?)
구구절절 tmi와 함께 정리해보는 DB, DBMS
DBMS(DataBase Management System)라고 부른다.관계형 데이터베이스(RDBMS)가 가장 많이 사용된다.Pk는 아니지만 중복이 되지 않고 값을 비워두는 것은 가능한 경우
Realm은 다중 기본키를 허용하지 않음
데이터베이스 스키마 버전을 관리하기 위한 방법(해당 링크를 참고하여 정리해보았다.)
☑️ 기본값으로 채워야하는 필수 프로퍼티 추가
☑️ 필드 결합
☑️ 필드 이름 변경
☑️ 필드 타입 변경
☑️ 객체를 embedded object로 변환시키기
// 새로운 버전을 사용하겠다고 config를 선언해준다.
let config = Realm.Configuration(
schemaVersion: 2)
// 새로 선언해준 config를 realm을 열 때 사용한다고 선언해준다.
Realm.Configuration.defaultConfiguration = config
let realm = try! Realm()
Migration.renameProperty(onType:from:to:)를 사용해야한다.age를 yearsSinceBirth로 이름을 변경하고자 하는 경우let config = Realm.Configuration(
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 2 {
// 프로퍼티의 이름을 "name"에서 "yearsSinceBirth"로 변경해준다.
// The renaming operation should be done outside of calls to `enumerateObjects(ofType: _:)`.
migration.renameProperty(onType: Person.className(), from: "age", to: "yearsSinceBirth")
}
})
enumerateObjects(ofType:_:) 메서드를 사용하여 인스턴스를 반복하여 업데이트할 수 있다.// In application(_:didFinishLaunchingWithOptions:)
let config = Realm.Configuration(
schemaVersion: 2, // Set the new schema version.
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 2 {
// The enumerateObjects(ofType:_:) method iterates over
// every Person object stored in the Realm file to apply the migration
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// combine name fields into a single field
let firstName = oldObject!["firstName"] as? String
let lastName = oldObject!["lastName"] as? String
newObject!["fullName"] = "\(firstName!) \(lastName!)"
}
}
}
)
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()
// In application(_:didFinishLaunchingWithOptions:)
let config = Realm.Configuration(
schemaVersion: 3, // Set the new schema version.
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 2 {
// Previous Migration.
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
let firstName = oldObject!["firstName"] as? String
let lastName = oldObject!["lastName"] as? String
newObject!["fullName"] = "\(firstName!) \(lastName!)"
}
}
if oldSchemaVersion < 3 {
// New Migration.
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// Make age a String instead of an Int
newObject!["age"] = "\(oldObject!["age"] ?? 0)"
}
}
}
)
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()