def test_custom_templates_and_themes(self):
        """Users can define their own templates with themes"""
        new_templates_dir = os.path.join(self.temp_usable_dir, 'templates')
        manage_tmpl_file = os.path.join(new_templates_dir,
                                        'manage/custom.py_tmpl')
        repository_tmpl_file = os.path.join(new_templates_dir,
                                            'repository/custom/README')
        script_tmpl_file = os.path.join(new_templates_dir,
                                        'script/custom.py_tmpl')
        sql_script_tmpl_file = os.path.join(new_templates_dir,
                                            'sql_script/custom.py_tmpl')

        MANAGE_CONTENTS = 'print "manage.py"'
        README_CONTENTS = 'MIGRATE README!'
        SCRIPT_FILE_CONTENTS = 'print "script.py"'
        new_repo_dest = self.tmp_repos()
        new_manage_dest = self.tmp_py()

        # make new templates dir
        shutil.copytree(migrate.versioning.templates.__path__[0],
                        new_templates_dir)
        shutil.copytree(os.path.join(new_templates_dir, 'repository/default'),
                        os.path.join(new_templates_dir, 'repository/custom'))

        # edit templates
        f = open(manage_tmpl_file, 'w').write(MANAGE_CONTENTS)
        f = open(repository_tmpl_file, 'w').write(README_CONTENTS)
        f = open(script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)
        f = open(sql_script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)

        # create repository, manage file and python script
        kw = {}
        kw['templates_path'] = new_templates_dir
        kw['templates_theme'] = 'custom'
        api.create(new_repo_dest, 'repo_name', **kw)
        api.script('test', new_repo_dest, **kw)
        api.script_sql('postgres', new_repo_dest, **kw)
        api.manage(new_manage_dest, **kw)

        # assert changes
        self.assertEqual(open(new_manage_dest).read(), MANAGE_CONTENTS)
        self.assertEqual(
            open(os.path.join(new_repo_dest, 'manage.py')).read(),
            MANAGE_CONTENTS)
        self.assertEqual(
            open(os.path.join(new_repo_dest, 'README')).read(),
            README_CONTENTS)
        self.assertEqual(
            open(os.path.join(new_repo_dest, 'versions/001_test.py')).read(),
            SCRIPT_FILE_CONTENTS)
        self.assertEqual(
            open(
                os.path.join(new_repo_dest,
                             'versions/002_postgres_downgrade.sql')).read(),
            SCRIPT_FILE_CONTENTS)
        self.assertEqual(
            open(
                os.path.join(new_repo_dest,
                             'versions/002_postgres_upgrade.sql')).read(),
            SCRIPT_FILE_CONTENTS)
Esempio n. 2
0
    def test_workflow(self):
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.script('First Version', self.repo)
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.upgrade(self.url, self.repo, 1)
        self.assertEqual(api.db_version(self.url, self.repo), 1)
        api.downgrade(self.url, self.repo, 0)
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.test(self.url, self.repo)
        self.assertEqual(api.db_version(self.url, self.repo), 0)

        # preview
        # TODO: test output
        out = api.upgrade(self.url, self.repo, preview_py=True)
        out = api.upgrade(self.url, self.repo, preview_sql=True)

        api.upgrade(self.url, self.repo, 1)
        api.script_sql('default', 'desc', self.repo)
        self.assertRaises(UsageError,
                          api.upgrade,
                          self.url,
                          self.repo,
                          2,
                          preview_py=True)
        out = api.upgrade(self.url, self.repo, 2, preview_sql=True)

        # cant upgrade to version 1, already at version 1
        self.assertEqual(api.db_version(self.url, self.repo), 1)
        self.assertRaises(KnownError, api.upgrade, self.url, self.repo, 0)
Esempio n. 3
0
    def test_source(self):
        repo = self.tmp_repos()
        api.create(repo, 'temp')
        api.script('first version', repo)
        api.script_sql('default', repo)

        # no repository
        self.assertRaises(UsageError, api.source, 1)

        # stdout
        out = api.source(1, dest=None, repository=repo)
        self.assertTrue(out)

        # file
        out = api.source(1, dest=self.tmp_repos(), repository=repo)
        self.assertFalse(out)
Esempio n. 4
0
    def test_source(self):
        repo = self.tmp_repos()
        api.create(repo, 'temp')
        api.script('first version', repo)
        api.script_sql('default', 'desc', repo)

        # no repository
        self.assertRaises(UsageError, api.source, 1)

        # stdout
        out = api.source(1, dest=None, repository=repo)
        self.assertTrue(out)

        # file
        out = api.source(1, dest=self.tmp_repos(), repository=repo)
        self.assertFalse(out)
    def test_custom_templates_and_themes(self):
        """Users can define their own templates with themes"""
        new_templates_dir = os.path.join(self.temp_usable_dir, 'templates')
        manage_tmpl_file = os.path.join(new_templates_dir, 'manage/custom.py_tmpl')
        repository_tmpl_file = os.path.join(new_templates_dir, 'repository/custom/README')
        script_tmpl_file = os.path.join(new_templates_dir, 'script/custom.py_tmpl')
        sql_script_tmpl_file = os.path.join(new_templates_dir, 'sql_script/custom.py_tmpl')

        MANAGE_CONTENTS = 'print "manage.py"'
        README_CONTENTS = 'MIGRATE README!'
        SCRIPT_FILE_CONTENTS = 'print "script.py"'
        new_repo_dest = self.tmp_repos()
        new_manage_dest = self.tmp_py()

        # make new templates dir
        shutil.copytree(migrate.versioning.templates.__path__[0], new_templates_dir)
        shutil.copytree(os.path.join(new_templates_dir, 'repository/default'),
            os.path.join(new_templates_dir, 'repository/custom'))

        # edit templates
        f = open(manage_tmpl_file, 'w').write(MANAGE_CONTENTS)
        f = open(repository_tmpl_file, 'w').write(README_CONTENTS)
        f = open(script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)
        f = open(sql_script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)

        # create repository, manage file and python script
        kw = {}
        kw['templates_path'] = new_templates_dir
        kw['templates_theme'] = 'custom'
        api.create(new_repo_dest, 'repo_name', **kw)
        api.script('test', new_repo_dest, **kw)
        api.script_sql('postgres', new_repo_dest, **kw)
        api.manage(new_manage_dest, **kw)

        # assert changes
        self.assertEqual(open(new_manage_dest).read(), MANAGE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'manage.py')).read(), MANAGE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'README')).read(), README_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/001_test.py')).read(), SCRIPT_FILE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/002_postgres_downgrade.sql')).read(), SCRIPT_FILE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/002_postgres_upgrade.sql')).read(), SCRIPT_FILE_CONTENTS)
Esempio n. 6
0
    def test_workflow(self):
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.script('First Version', self.repo)
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.upgrade(self.url, self.repo, 1)
        self.assertEqual(api.db_version(self.url, self.repo), 1)
        api.downgrade(self.url, self.repo, 0)
        self.assertEqual(api.db_version(self.url, self.repo), 0)
        api.test(self.url, self.repo)
        self.assertEqual(api.db_version(self.url, self.repo), 0)

        # preview
        # TODO: test output
        out = api.upgrade(self.url, self.repo, preview_py=True)
        out = api.upgrade(self.url, self.repo, preview_sql=True)

        api.upgrade(self.url, self.repo, 1)
        api.script_sql('default', self.repo)
        self.assertRaises(UsageError, api.upgrade, self.url, self.repo, 2, preview_py=True)
        out = api.upgrade(self.url, self.repo, 2, preview_sql=True)

        # cant upgrade to version 1, already at version 1
        self.assertEqual(api.db_version(self.url, self.repo), 1)
        self.assertRaises(KnownError, api.upgrade, self.url, self.repo, 0)
Esempio n. 7
0
 def test_script_sql(self):
     repo = self.tmp_repos()
     api.create(repo, 'temp')
     api.script_sql('postgres', repo)
Esempio n. 8
0
 def test_script_sql(self):
     repo = self.tmp_repos()
     api.create(repo, 'temp')
     api.script_sql('postgres', 'desc', repo)
Esempio n. 9
0
 def test_script_sql(self):
     repo = self.tmp_repos()
     api.create(repo, "temp")
     api.script_sql("postgres", "desc", repo)
Esempio n. 10
0
 def generate_sql_file(database_type):
     # 待完善
     api.script_sql(database_type,
                    description="generate",
                    repository=SQLALCHEMY_MIGRATE_REPO)