001/*
002 * Copyright 2011 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014package org.atteo.moonshine.database;
015
016import java.util.ArrayList;
017import java.util.List;
018
019import javax.sql.DataSource;
020
021import org.atteo.moonshine.TopLevelService;
022
023/**
024 * Database service.
025 * <p>
026 * Database services should bind {@link DataSource}.
027 * </p>
028 */
029public abstract class DatabaseService extends TopLevelService {
030    protected List<DatabaseMigration> migrations = new ArrayList<>();
031
032    /**
033     * Register database migration.
034     */
035    public void registerMigration(DatabaseMigration migration) {
036        migrations.add(migration);
037    }
038
039    /**
040     * Execute registered database migrations.
041     */
042    protected void executeMigrations(DataSource dataSource) {
043        for (DatabaseMigration migration : migrations) {
044            migration.execute(dataSource);
045        }
046        migrations = null;
047    }
048}